Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / util / CoapMessageBuilder.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.accountserver.util;
23
24 import java.nio.ByteBuffer;
25 import java.nio.charset.StandardCharsets;
26
27 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
28 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
29 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
30 import org.iotivity.cloud.base.protocols.coap.enums.CoapOption;
31 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
32
33 /**
34  * 
35  * This class provides a set of APIs to build build data of CoAP request and
36  * response type.
37  * 
38  */
39 public class CoapMessageBuilder {
40
41     public static final int APPLICATION_JSON = 50;
42
43     /**
44      * API for building data of CoAP response type without payload.
45      * 
46      * @param token
47      *            token
48      * @param status
49      *            response status
50      * @return CoapResponse - data of CoAP response type
51      */
52     public CoapResponse buildCoapResponse(byte[] token, CoapStatus status) {
53
54         return buildCoapResponse(token, null, status);
55     }
56
57     /**
58      * API for building data of CoAP response type with payload.
59      * 
60      * @param token
61      *            token
62      * @param jsonString
63      *            payload data
64      * @param status
65      *            response status
66      * @return CoapResponse - data of CoAP response type
67      */
68     public CoapResponse buildCoapResponse(byte[] token, String jsonString,
69             CoapStatus status) {
70
71         CoapResponse coapResponse = new CoapResponse(status);
72
73         coapResponse.setToken(token);
74
75         byte[] bytes = ByteBuffer.allocate(4)
76                 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
77         coapResponse.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
78
79         if (jsonString != null)
80             coapResponse
81                     .setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
82
83         return coapResponse;
84     }
85
86     /**
87      * API for building data of CoAP requeset type with payload.
88      * 
89      * @param token
90      *            token
91      * @param jsonString
92      *            payload data
93      * @return CoapRequest - data of CoAP request type
94      */
95     public CoapRequest buildCoapRequest(byte[] token, String jsonString) {
96
97         CoapRequest coapRequest = new CoapRequest(CoapMethod.GET);
98
99         coapRequest.setToken(token);
100
101         byte[] bytes = ByteBuffer.allocate(4)
102                 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
103         coapRequest.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
104
105         coapRequest.setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
106
107         return coapRequest;
108     }
109
110     /*
111      * 
112      * public String getJsonAuthServerResponse() {
113      * 
114      * return "{ \"" + OAuthConstant.AUTH_SERVER + "\" : \"" +
115      * OAuthConstant.AUTH_SERVER_GITHUB + "\" }"; }
116      * 
117      * public String getJsonAuthInfoResponse() {
118      * 
119      * return "\n{\n" + " \"" + OAuthConstant.AUTH_ADDRESS + "\" : \"" +
120      * OAuthConstant.GITHUB_ADDRESS + "\",\n" + " \"" +
121      * OAuthConstant.AUTH_RESPONSE_TYPE + "\" : \"" +
122      * OAuthConstant.AUTH_CODE_VALUE + "\",\n" + " \"" +
123      * OAuthConstant.AUTH_CLIENT_ID + "\" : \"" + OAuthConstant.GITHUB_CLIENT_ID
124      * + "\",\n" + " \"" + OAuthConstant.AUTH_REDIRECT_URI + "\" : \"" +
125      * OAuthConstant.GITHUB_REDIRECT_URL + "\"" + "\n}"; }
126      * 
127      * public String getJsonAuthQueryRequest(String auth_server, String
128      * auth_code, String auth_di) {
129      * 
130      * return "\n{\n" + " \"" + OAuthConstant.AUTH_TYPE + "\" : \"" +
131      * OAuthConstant.AUTH_TYPE_QUERY + "\",\n" + " \"" +
132      * OAuthConstant.AUTH_SERVER + "\" : \"" + auth_server + "\",\n" + " \"" +
133      * OAuthConstant.AUTH_CODE + "\" : \"" + auth_code + "\",\n" + " \"" +
134      * OAuthConstant.AUTH_DEVICE_ID + "\" : \"" + auth_di + "\"" + "\n}"; }
135      */
136 }