modified observe exception, when channel is disconnected
[platform/upstream/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             coapResponse.setObserve(coapRequest.getObserve());
50             if (payload != null) {
51                 coapResponse.setContentFormat(format);
52                 coapResponse.setPayload(payload);
53             }
54
55             if (coapRequest.getMethod() == RequestMethod.GET
56                     && responseStatus == ResponseStatus.CONTENT) {
57                 switch (coapRequest.getObserve()) {
58                     case SUBSCRIBE:
59                     case SEQUENCE_NUMBER:
60                         coapResponse.setSequenceNumber(
61                                 coapRequest.getNextSequenceNumber());
62                         break;
63                     default:
64                 }
65             }
66
67             response = coapResponse;
68         }
69
70         return response;
71     }
72
73     public static IRequest createRequest(RequestMethod requestMethod,
74             String uriPath, String uriQuery) {
75         return createRequest(requestMethod, uriPath, uriQuery,
76                 ContentFormat.NO_CONTENT, null);
77     }
78
79     public static IRequest createRequest(RequestMethod requestMethod,
80             String uriPath, String uriQuery, ContentFormat contentFormat,
81             byte[] payload) {
82
83         CoapRequest coapRequest = null;
84
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);
91         }
92         if (payload != null) {
93             coapRequest.setContentFormat(contentFormat);
94             coapRequest.setPayload(payload);
95         }
96
97         return coapRequest;
98     }
99
100     public static IRequest modifyRequest(IRequest orgRequest, String uriPath,
101             String uriQuery, ContentFormat contentFormat, byte[] payload) {
102
103         CoapRequest coapRequest = (CoapRequest) orgRequest;
104
105         if (uriPath != null) {
106             coapRequest.setUriPath(uriPath);
107         }
108         if (uriQuery != null) {
109             coapRequest.setUriQuery(uriQuery);
110         }
111         if (payload != null) {
112             coapRequest.setContentFormat(contentFormat);
113             coapRequest.setPayload(payload);
114         }
115
116         return coapRequest;
117     }
118
119     public static IResponse modifyResponse(IResponse orgResponse,
120             ContentFormat contentFormat, byte[] payload) {
121
122         return modifyResponse(orgResponse, null, contentFormat, payload);
123     }
124
125     public static IResponse modifyResponse(IResponse orgResponse,
126             String uriPath, ContentFormat contentFormat, byte[] payload) {
127
128         CoapResponse coapResponse = (CoapResponse) orgResponse;
129
130         if (uriPath != null) {
131             coapResponse.setUriPath(uriPath);
132         }
133         if (payload != null) {
134             coapResponse.setContentFormat(contentFormat);
135             coapResponse.setPayload(payload);
136         }
137
138         return coapResponse;
139     }
140 }