2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.base.protocols.coap;
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;
30 import org.iotivity.cloud.base.protocols.coap.enums.CoapOption;
31 import org.iotivity.cloud.util.Cbor;
32 import org.iotivity.cloud.util.Logger;
34 public class CoapMessage {
36 private int tokenLength = 0;
37 protected int code = 0;
38 private byte[] token = null;
40 private byte[] payload = null;
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 byte[] observe = null;
60 public CoapMessage() {
63 public int getTokenLength() {
64 return this.tokenLength;
67 public int getCode() {
71 public byte[] getToken() {
75 public void setToken(byte[] token) {
77 this.tokenLength = token.length;
80 public int getPayloadSize() {
81 return payload == null ? 0 : payload.length;
84 public byte[] getPayload() {
88 public void setPayload(byte[] payload) {
89 this.payload = payload;
92 public void addOption(int optnum, byte[] value) {
96 if (if_match == null) {
97 if_match = new ArrayList<byte[]>();
110 etag = new ArrayList<byte[]>();
117 if_none_match = true;
127 if (location_path == null) {
128 location_path = new ArrayList<byte[]>();
130 location_path.add(value);
135 if (uri_path == null) {
136 uri_path = new ArrayList<byte[]>();
143 content_format = value;
153 if (uri_query == null) {
154 uri_query = new ArrayList<byte[]>();
156 uri_query.add(value);
166 if (location_query == null) {
167 location_query = new ArrayList<byte[]>();
169 location_query.add(value);
179 proxy_scheme = value;
194 public List<byte[]> getOption(int optnum) {
202 return uri_host != null ? Arrays.asList(uri_host) : null;
210 return if_none_match == true ? new ArrayList<byte[]>() : null;
214 return uri_port != null ? Arrays.asList(uri_port) : null;
218 return location_path;
226 return content_format != null ? Arrays.asList(content_format)
231 return max_age != null ? Arrays.asList(max_age) : null;
239 return accept != null ? Arrays.asList(accept) : null;
243 return location_query;
247 return proxy_uri != null ? Arrays.asList(proxy_uri) : null;
251 return proxy_scheme != null ? Arrays.asList(proxy_scheme)
256 return size1 != null ? Arrays.asList(size1) : null;
260 return observe != null ? Arrays.asList(observe) : null;
266 public String getTokenString() {
267 StringBuffer strBuffer = new StringBuffer(token == null ? "null" : "");
270 strBuffer.append(String.format("%02x", b & 0xff)); // hexadecimal(16)
271 return strBuffer.toString();
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();
288 public String getPayloadString() {
291 return new String(payload, Charset.forName("UTF-8"));
294 public String decodeDeviceId() {
295 Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
296 ArrayList<Object> decodedPayload = null;
298 if (payload == null) {
299 throw new IllegalArgumentException("payload is null");
303 decodedPayload = cbor.parsePayloadFromCbor(payload,
305 String deviceId = null;
306 if (decodedPayload != null) {
307 HashMap<Object, Object> tags = (HashMap<Object, Object>) decodedPayload
310 deviceId = tags.get("di").toString();
312 if (deviceId == null) {
313 throw new IllegalArgumentException("deviceId is null");
316 Logger.i("deviceId : " + deviceId);