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;
24 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
25 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
26 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
27 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
28 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
30 public class MessageBuilder {
32 public static IResponse createResponse(IRequest request,
33 ResponseStatus responseStatus) {
34 return createResponse(request, responseStatus, ContentFormat.NO_CONTENT,
38 public static IResponse createResponse(IRequest request,
39 ResponseStatus responseStatus, ContentFormat format,
42 IResponse response = null;
44 if (request instanceof CoapRequest) {
45 CoapRequest coapRequest = (CoapRequest) request;
46 CoapResponse coapResponse = new CoapResponse(responseStatus);
47 coapResponse.setUriPath(coapRequest.getUriPath());
48 coapResponse.setToken(coapRequest.getToken());
49 coapResponse.setObserve(coapRequest.getObserve());
50 if (payload != null) {
51 coapResponse.setContentFormat(format);
52 coapResponse.setPayload(payload);
55 if (coapRequest.getMethod() == RequestMethod.GET
56 && responseStatus == ResponseStatus.CONTENT) {
57 switch (coapRequest.getObserve()) {
60 coapResponse.setSequenceNumber(
61 coapRequest.getNextSequenceNumber());
67 response = coapResponse;
73 public static IRequest createRequest(RequestMethod requestMethod,
74 String uriPath, String uriQuery) {
75 return createRequest(requestMethod, uriPath, uriQuery,
76 ContentFormat.NO_CONTENT, null);
79 public static IRequest createRequest(RequestMethod requestMethod,
80 String uriPath, String uriQuery, ContentFormat contentFormat,
83 CoapRequest coapRequest = null;
85 coapRequest = new CoapRequest(requestMethod);
86 // TODO: Random token generation required
87 coapRequest.setToken("tmptoken".getBytes());
88 coapRequest.setUriPath(uriPath);
89 if (uriQuery != null) {
90 coapRequest.setUriQuery(uriQuery);
92 if (payload != null) {
93 coapRequest.setContentFormat(contentFormat);
94 coapRequest.setPayload(payload);
100 public static IRequest modifyRequest(IRequest orgRequest, String uriPath,
101 String uriQuery, ContentFormat contentFormat, byte[] payload) {
103 CoapRequest coapRequest = (CoapRequest) orgRequest;
105 if (uriPath != null) {
106 coapRequest.setUriPath(uriPath);
108 if (uriQuery != null) {
109 coapRequest.setUriQuery(uriQuery);
111 if (payload != null) {
112 coapRequest.setContentFormat(contentFormat);
113 coapRequest.setPayload(payload);
119 public static IResponse modifyResponse(IResponse orgResponse,
120 ContentFormat contentFormat, byte[] payload) {
122 return modifyResponse(orgResponse, null, contentFormat, payload);
125 public static IResponse modifyResponse(IResponse orgResponse,
126 String uriPath, ContentFormat contentFormat, byte[] payload) {
128 CoapResponse coapResponse = (CoapResponse) orgResponse;
130 if (uriPath != null) {
131 coapResponse.setUriPath(uriPath);
133 if (payload != null) {
134 coapResponse.setContentFormat(contentFormat);
135 coapResponse.setPayload(payload);