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.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import org.iotivity.cloud.base.protocols.Message;
31 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
32 import org.iotivity.cloud.base.protocols.enums.Observe;
33 import org.iotivity.cloud.util.Bytes;
35 public abstract class CoapMessage extends Message {
37 protected byte[] mToken = null;
38 protected int mObserve = -1;
41 protected List<byte[]> if_match = null;
42 protected byte[] uri_host = null;
43 protected List<byte[]> etag = null;
44 protected boolean if_none_match = false;
45 protected byte[] uri_port = null;
46 protected List<byte[]> location_path = null;
48 protected byte[] max_age = null;
49 protected byte[] accept = null;
50 protected List<byte[]> location_query = null;
51 protected byte[] proxy_uri = null;
52 protected byte[] proxy_scheme = null;
53 protected byte[] size1 = null;
54 protected byte[] content_format = null;
56 public CoapMessage() {
59 public abstract int getCode();
61 public void addOption(int optnum, byte[] value) {
65 if (if_match == null) {
66 if_match = new ArrayList<>();
79 etag = new ArrayList<>();
96 if (location_path == null) {
97 location_path = new ArrayList<>();
99 location_path.add(value);
104 if (uri_path == null) {
105 uri_path = new ArrayList<>();
112 content_format = value;
122 if (uri_query == null) {
123 uri_query = new ArrayList<>();
125 uri_query.add(value);
135 if (location_query == null) {
136 location_query = new ArrayList<>();
138 location_query.add(value);
148 proxy_scheme = value;
158 mObserve = Bytes.bytesToInt(value);
163 public List<byte[]> getOption(int optnum) {
171 return uri_host != null ? Arrays.asList(uri_host) : null;
179 return if_none_match == true ? new ArrayList<byte[]>() : null;
183 return uri_port != null ? Arrays.asList(uri_port) : null;
187 return location_path;
195 return content_format != null ? Arrays.asList(content_format)
200 return max_age != null ? Arrays.asList(max_age) : null;
208 return accept != null ? Arrays.asList(accept) : null;
212 return location_query;
216 return proxy_uri != null ? Arrays.asList(proxy_uri) : null;
220 return proxy_scheme != null ? Arrays.asList(proxy_scheme)
225 return size1 != null ? Arrays.asList(size1) : null;
229 return mObserve != -1
230 ? Arrays.asList(Bytes.intToMax4Bytes(mObserve)) : null;
236 public void setPayload(byte[] payload) {
237 this.payload = payload;
240 public void setUriPath(String path) {
241 if (uri_path == null) {
242 uri_path = new ArrayList<>();
247 if (path == null || path.length() == 0) {
253 String[] pathSegments = path.split("/");
254 for (String pathSegment : pathSegments) {
255 if (pathSegment.length() == 0)
257 uri_path.add(pathSegment.getBytes(StandardCharsets.UTF_8));
261 public void setUriQuery(String query) {
262 if (uri_query == null) {
263 uri_query = new ArrayList<>();
268 if (query == null || query.length() == 0) {
274 String[] querySegments = query.split("&");
275 for (String querySegment : querySegments) {
276 uri_query.add(querySegment.getBytes(StandardCharsets.UTF_8));
280 public String getPayloadString() {
283 return new String(payload, Charset.forName("UTF-8"));
286 public void setToken(byte[] token) {
290 public byte[] getToken() {
294 public int getTokenLength() {
295 return mToken == null ? 0 : mToken.length;
298 public String getTokenString() {
299 StringBuffer strBuffer = new StringBuffer(mToken == null ? "null" : "");
301 for (byte b : mToken)
302 strBuffer.append(String.format("%02x", b & 0xff)); // hexadecimal(16)
303 return strBuffer.toString();
306 public String getRequestId() {
307 return getTokenString();
310 public void setSequenceNumber(int seqNumber) {
311 mObserve = seqNumber;
314 public int getSequenceNumber() {
318 public int getNextSequenceNumber() {
319 int ret = mObserve++;
321 if (mObserve == 1 || mObserve == 16777216) {
328 public void setObserve(Observe observe) {
347 public Observe getObserve() {
350 return Observe.NOTHING;
353 return Observe.SUBSCRIBE;
356 return Observe.UNSUBSCRIBE;
359 return Observe.SEQUENCE_NUMBER;
362 public void setContentFormat(ContentFormat format) {
367 case APPLICATION_LINK_FORMAT:
370 case APPLICATION_XML:
373 case APPLICATION_OCTET_STREAM:
376 case APPLICATION_EXI:
379 case APPLICATION_JSON:
382 case APPLICATION_CBOR:
386 content_format = new byte[] { value };
389 public ContentFormat getContentFormat() {
391 if (content_format == null) {
392 return ContentFormat.NO_CONTENT;
395 switch (content_format[0]) {
397 return ContentFormat.APPLICATION_LINK_FORMAT;
400 return ContentFormat.APPLICATION_XML;
403 return ContentFormat.APPLICATION_OCTET_STREAM;
406 return ContentFormat.APPLICATION_EXI;
409 return ContentFormat.APPLICATION_JSON;
412 return ContentFormat.APPLICATION_CBOR;
415 return ContentFormat.NO_CONTENT;