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.accountserver.util;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.StandardCharsets;
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;
35 * This class provides a set of APIs to build build data of CoAP request and
39 public class CoapMessageBuilder {
41 public static final int APPLICATION_JSON = 50;
44 * API for building data of CoAP response type without payload.
50 * @return CoapResponse - data of CoAP response type
52 public CoapResponse buildCoapResponse(byte[] token, CoapStatus status) {
54 return buildCoapResponse(token, null, status);
58 * API for building data of CoAP response type with payload.
66 * @return CoapResponse - data of CoAP response type
68 public CoapResponse buildCoapResponse(byte[] token, String jsonString,
71 CoapResponse coapResponse = new CoapResponse(status);
73 coapResponse.setToken(token);
75 byte[] bytes = ByteBuffer.allocate(4)
76 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
77 coapResponse.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
79 if (jsonString != null)
81 .setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
87 * API for building data of CoAP requeset type with payload.
93 * @return CoapRequest - data of CoAP request type
95 public CoapRequest buildCoapRequest(byte[] token, String jsonString) {
97 CoapRequest coapRequest = new CoapRequest(CoapMethod.GET);
99 coapRequest.setToken(token);
101 byte[] bytes = ByteBuffer.allocate(4)
102 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
103 coapRequest.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
105 coapRequest.setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
112 * public String getJsonAuthServerResponse() {
114 * return "{ \"" + OAuthConstant.AUTH_SERVER + "\" : \"" +
115 * OAuthConstant.AUTH_SERVER_GITHUB + "\" }"; }
117 * public String getJsonAuthInfoResponse() {
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}"; }
127 * public String getJsonAuthQueryRequest(String auth_server, String
128 * auth_code, String auth_di) {
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}"; }