[Simulator] Minor UI changes fixing the IOT-1087.
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / protocols / coap / CoapMessage.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.base.protocols.coap;
23
24 import java.nio.charset.Charset;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29
30 import org.iotivity.cloud.base.protocols.coap.enums.CoapOption;
31 import org.iotivity.cloud.util.Cbor;
32 import org.iotivity.cloud.util.Logger;
33
34 public class CoapMessage {
35
36     private int    tokenLength = 0;
37     protected int  code        = 0;
38     private byte[] token       = null;
39
40     private byte[] payload = null;
41
42     // Option fields
43     protected List<byte[]> if_match       = null;
44     protected byte[]       uri_host       = null;
45     protected List<byte[]> etag           = null;
46     protected boolean      if_none_match  = false;
47     protected byte[]       uri_port       = null;
48     protected List<byte[]> location_path  = null;
49     protected List<byte[]> uri_path       = null;
50     protected byte[]       content_format = null;
51     protected byte[]       max_age        = null;
52     protected List<byte[]> uri_query      = null;
53     protected byte[]       accept         = null;
54     protected List<byte[]> location_query = null;
55     protected byte[]       proxy_uri      = null;
56     protected byte[]       proxy_scheme   = null;
57     protected byte[]       size1          = null;
58     protected boolean      observe        = false;
59
60     public CoapMessage() {
61     }
62
63     public int getTokenLength() {
64         return this.tokenLength;
65     }
66
67     public int getCode() {
68         return code;
69     }
70
71     public byte[] getToken() {
72         return token;
73     }
74
75     public void setToken(byte[] token) {
76         this.token = token;
77         this.tokenLength = token.length;
78     }
79
80     public int getPayloadSize() {
81         return payload == null ? 0 : payload.length;
82     }
83
84     public byte[] getPayload() {
85         return payload;
86     }
87
88     public void setPayload(byte[] payload) {
89         this.payload = payload;
90     }
91
92     public void addOption(int optnum, byte[] value) {
93         switch (optnum) {
94             // IF_MATCH
95             case 1:
96                 if (if_match == null) {
97                     if_match = new ArrayList<byte[]>();
98                 }
99                 if_match.add(value);
100                 break;
101
102             // URI_HOST
103             case 3:
104                 uri_host = value;
105                 break;
106
107             // ETAG
108             case 4:
109                 if (etag == null) {
110                     etag = new ArrayList<byte[]>();
111                 }
112                 etag.add(value);
113                 break;
114
115             // IF_NONE_MATCH
116             case 5:
117                 if_none_match = true;
118                 break;
119
120             // URI_PORT
121             case 7:
122                 uri_port = value;
123                 break;
124
125             // LOCATION_PATH
126             case 8:
127                 if (location_path == null) {
128                     location_path = new ArrayList<byte[]>();
129                 }
130                 location_path.add(value);
131                 break;
132
133             // URI_PATH
134             case 11:
135                 if (uri_path == null) {
136                     uri_path = new ArrayList<byte[]>();
137                 }
138                 uri_path.add(value);
139                 break;
140
141             // CONTENT_FORMAT
142             case 12:
143                 content_format = value;
144                 break;
145
146             // MAX_AGE
147             case 14:
148                 max_age = value;
149                 break;
150
151             // URI_QUERY
152             case 15:
153                 if (uri_query == null) {
154                     uri_query = new ArrayList<byte[]>();
155                 }
156                 uri_query.add(value);
157                 break;
158
159             // ACCEPT
160             case 17:
161                 accept = value;
162                 break;
163
164             // LOCATION_QUERY
165             case 20:
166                 if (location_query == null) {
167                     location_query = new ArrayList<byte[]>();
168                 }
169                 location_query.add(value);
170                 break;
171
172             // PROXY_URI
173             case 35:
174                 proxy_uri = value;
175                 break;
176
177             // PROXY_SCHEME
178             case 39:
179                 proxy_scheme = value;
180                 break;
181
182             // SIZE1
183             case 60:
184                 size1 = value;
185                 break;
186
187             // OBSERVE
188             case 6:
189                 observe = true;
190                 break;
191         }
192     }
193
194     public List<byte[]> getOption(int optnum) {
195         switch (optnum) {
196             // IF_MATCH
197             case 1:
198                 return if_match;
199
200             // URI_HOST
201             case 3:
202                 return uri_host != null ? Arrays.asList(uri_host) : null;
203
204             // ETAG
205             case 4:
206                 return etag;
207
208             // IF_NONE_MATCH
209             case 5:
210                 return if_none_match == true ? new ArrayList<byte[]>() : null;
211
212             // URI_PORT
213             case 7:
214                 return uri_port != null ? Arrays.asList(uri_port) : null;
215
216             // LOCATION_PATH
217             case 8:
218                 return location_path;
219
220             // URI_PATH
221             case 11:
222                 return uri_path;
223
224             // CONTENT_FORMAT
225             case 12:
226                 return content_format != null ? Arrays.asList(content_format)
227                         : null;
228
229             // MAX_AGE
230             case 14:
231                 return max_age != null ? Arrays.asList(max_age) : null;
232
233             // URI_QUERY
234             case 15:
235                 return uri_query;
236
237             // ACCEPT
238             case 17:
239                 return accept != null ? Arrays.asList(content_format) : null;
240
241             // LOCATION_QUERY
242             case 20:
243                 return location_query;
244
245             // PROXY_URI
246             case 35:
247                 return proxy_uri != null ? Arrays.asList(proxy_uri) : null;
248
249             // PROXY_SCHEME
250             case 39:
251                 return proxy_scheme != null ? Arrays.asList(proxy_scheme)
252                         : null;
253
254             // SIZE1
255             case 60:
256                 return size1 != null ? Arrays.asList(size1) : null;
257
258             // OBSERVE
259             case 6:
260                 return observe == true ? new ArrayList<byte[]>() : null;
261         }
262
263         return null;
264     }
265
266     public String getTokenString() {
267         StringBuffer strBuffer = new StringBuffer(token == null ? "null" : "");
268         if (token != null)
269             for (byte b : token)
270                 strBuffer.append(String.format("%02x", b & 0xff)); // hexadecimal(16)
271         return strBuffer.toString();
272     }
273
274     public String getContentFormatString() {
275         List<byte[]> contentFormatList = getOption(
276                 CoapOption.CONTENT_FORMAT.getvalue());
277         byte[] contentFormat = null;
278         if (contentFormatList != null && !contentFormatList.isEmpty())
279             contentFormat = contentFormatList.get(0);
280         StringBuffer strBuffer = new StringBuffer(
281                 contentFormat == null ? "null" : "");
282         if (contentFormat != null)
283             for (byte b : contentFormat)
284                 strBuffer.append(String.format("%02d", b & 0xff)); // decimal(10)
285         return strBuffer.toString();
286     }
287
288     public String getPayloadString() {
289         if (payload == null)
290             return "";
291         return new String(payload, Charset.forName("UTF-8"));
292     }
293
294     public String decodeDeviceId() {
295         Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
296         ArrayList<Object> decodedPayload = null;
297
298         if (payload == null) {
299             throw new IllegalArgumentException("payload is null");
300         }
301
302         else {
303             decodedPayload = cbor.parsePayloadFromCbor(payload,
304                     ArrayList.class);
305
306             HashMap<Object, Object> tags = (HashMap<Object, Object>) decodedPayload
307                     .get(0);
308
309             String deviceId = tags.get("di").toString();
310
311             if (deviceId == null) {
312                 throw new IllegalArgumentException("deviceId is null");
313             }
314
315             Logger.i("deviceId : " + deviceId);
316
317             return deviceId;
318         }
319     }
320 }