5a703b7171c5649e0e731fe7484b2b7e410a7477
[iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / protocols / MessageBuilder.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;
23
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;
29
30 public class MessageBuilder {
31
32     public static IResponse createResponse(IRequest request,
33             ResponseStatus responseStatus) {
34         return createResponse(request, responseStatus, ContentFormat.NO_CONTENT,
35                 null);
36     }
37
38     public static IResponse createResponse(IRequest request,
39             ResponseStatus responseStatus, ContentFormat format,
40             byte[] payload) {
41
42         IResponse response = null;
43
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             if (payload != null) {
50                 coapResponse.setContentFormat(format);
51                 coapResponse.setPayload(payload);
52             }
53
54             if (coapRequest.getMethod() == RequestMethod.GET
55                     && responseStatus == ResponseStatus.CONTENT) {
56                 switch (coapRequest.getObserve()) {
57                     case SUBSCRIBE:
58                     case SEQUENCE_NUMBER:
59                         coapResponse.setSequenceNumber(
60                                 coapRequest.getNextSequenceNumber());
61                         break;
62                     default:
63                 }
64             }
65
66             response = coapResponse;
67         }
68
69         return response;
70     }
71
72     public static IRequest createRequest(RequestMethod requestMethod,
73             String uriPath, String uriQuery) {
74         return createRequest(requestMethod, uriPath, uriQuery,
75                 ContentFormat.NO_CONTENT, null);
76     }
77
78     public static IRequest createRequest(RequestMethod requestMethod,
79             String uriPath, String uriQuery, ContentFormat contentFormat,
80             byte[] payload) {
81
82         CoapRequest coapRequest = null;
83
84         coapRequest = new CoapRequest(requestMethod);
85         // TODO: Random token generation required
86         coapRequest.setToken("tmptoken".getBytes());
87         coapRequest.setUriPath(uriPath);
88         if (uriQuery != null) {
89             coapRequest.setUriQuery(uriQuery);
90         }
91         if (payload != null) {
92             coapRequest.setContentFormat(contentFormat);
93             coapRequest.setPayload(payload);
94         }
95
96         return coapRequest;
97     }
98
99     public static IRequest modifyRequest(IRequest orgRequest, String uriPath,
100             String uriQuery, ContentFormat contentFormat, byte[] payload) {
101
102         CoapRequest coapRequest = (CoapRequest) orgRequest;
103
104         if (uriPath != null) {
105             coapRequest.setUriPath(uriPath);
106         }
107         if (uriQuery != null) {
108             coapRequest.setUriQuery(uriQuery);
109         }
110         if (payload != null) {
111             coapRequest.setContentFormat(contentFormat);
112             coapRequest.setPayload(payload);
113         }
114
115         return coapRequest;
116     }
117
118     public static IResponse modifyResponse(IResponse orgResponse,
119             ContentFormat contentFormat, byte[] payload) {
120
121         return modifyResponse(orgResponse, null, contentFormat, payload);
122     }
123
124     public static IResponse modifyResponse(IResponse orgResponse,
125             String uriPath, ContentFormat contentFormat, byte[] payload) {
126
127         CoapResponse coapResponse = (CoapResponse) orgResponse;
128
129         if (uriPath != null) {
130             coapResponse.setUriPath(uriPath);
131         }
132         if (payload != null) {
133             coapResponse.setContentFormat(contentFormat);
134             coapResponse.setPayload(payload);
135         }
136
137         return coapResponse;
138     }
139 }