• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.rcs.simpleclient.protocol.msrp;
18 
19 import static com.android.libraries.rcs.simpleclient.protocol.msrp.MsrpConstants.FLAG_ABORT_CHUNK;
20 import static com.android.libraries.rcs.simpleclient.protocol.msrp.MsrpConstants.FLAG_LAST_CHUNK;
21 import static com.android.libraries.rcs.simpleclient.protocol.msrp.MsrpConstants.FLAG_MORE_CHUNK;
22 
23 import com.google.auto.value.AutoValue;
24 import com.google.common.collect.ImmutableList;
25 
26 /**
27  * Single MSRP chunk containing a request or a response.
28  */
29 @AutoValue
30 public abstract class MsrpChunk {
31 
newBuilder()32     public static Builder newBuilder() {
33         return new AutoValue_MsrpChunk.Builder()
34                 .method(Method.UNKNOWN)
35                 .responseCode(0)
36                 .responseReason("")
37                 .content(new byte[0])
38                 .continuation(Continuation.UNKNOWN);
39     }
40 
method()41     public abstract Method method();
42 
transactionId()43     public abstract String transactionId();
44 
continuation()45     public abstract Continuation continuation();
46 
responseCode()47     public abstract int responseCode();
48 
responseReason()49     public abstract String responseReason();
50 
headers()51     public abstract ImmutableList<MsrpChunkHeader> headers();
52 
content()53     public abstract byte[] content();
54 
header(String headerName)55     public MsrpChunkHeader header(String headerName) {
56         for (MsrpChunkHeader header : headers()) {
57             if (header.name().equals(headerName)) {
58                 return header;
59             }
60         }
61         return null;
62     }
63 
64     /**
65      * Methods for requests
66      */
67     public enum Method {
68         UNKNOWN,
69         SEND,
70         REPORT,
71     }
72 
73 
74     /**
75      * Continuation flag for the chunk
76      */
77     public enum Continuation {
78         UNKNOWN(0),
79         COMPLETE(FLAG_LAST_CHUNK),
80         MORE(FLAG_MORE_CHUNK),
81         ABORTED(FLAG_ABORT_CHUNK);
82 
83         private final int value;
84 
Continuation(int value)85         Continuation(int value) {
86             this.value = value;
87         }
88 
valueOf(int read)89         public static Continuation valueOf(int read) {
90             if (read == COMPLETE.value) {
91                 return COMPLETE;
92             }
93             if (read == MORE.value) {
94                 return MORE;
95             }
96             if (read == ABORTED.value) {
97                 return ABORTED;
98             }
99             return UNKNOWN;
100         }
101 
toByte()102         public byte toByte() {
103             return (byte) value;
104         }
105     }
106 
107     /**
108      * Builder for new MSRP chunk.
109      */
110     @AutoValue.Builder
111     public abstract static class Builder {
112 
method(Method method)113         public abstract Builder method(Method method);
114 
transactionId(String id)115         public abstract Builder transactionId(String id);
116 
transactionId()117         public abstract String transactionId();
118 
continuation()119         public abstract Continuation continuation();
120 
continuation(Continuation continuation)121         public abstract Builder continuation(Continuation continuation);
122 
responseCode(int continuation)123         public abstract Builder responseCode(int continuation);
124 
responseReason(String reason)125         public abstract Builder responseReason(String reason);
126 
content(byte[] content)127         public abstract Builder content(byte[] content);
128 
addHeader(MsrpChunkHeader header)129         public Builder addHeader(MsrpChunkHeader header) {
130             headersBuilder().add(header);
131             return this;
132         }
133 
addHeader(String name, String value)134         public Builder addHeader(String name, String value) {
135             headersBuilder().add(MsrpChunkHeader.newBuilder().name(name).value(value).build());
136             return this;
137         }
138 
headersBuilder()139         abstract ImmutableList.Builder<MsrpChunkHeader> headersBuilder();
140 
header(String name)141         MsrpChunkHeader header(String name) {
142             for (MsrpChunkHeader header : headersBuilder().build()) {
143                 if (header.name().equals(name)) {
144                     return header;
145                 }
146             }
147             return null;
148         }
149 
build()150         public abstract MsrpChunk build();
151 
152 
153     }
154 }
155