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 utility for making CoAP request and response.
38 public class CoapMessageBuilder {
40 public static final int APPLICATION_JSON = 50;
42 public CoapResponse buildCoapResponse(byte[] token, CoapStatus status) {
44 return buildCoapResponse(token, null, status);
47 public CoapResponse buildCoapResponse(byte[] token, String jsonString,
50 CoapResponse coapResponse = new CoapResponse(status);
52 coapResponse.setToken(token);
54 byte[] bytes = ByteBuffer.allocate(4)
55 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
56 coapResponse.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
58 if (jsonString != null)
60 .setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
65 public CoapRequest buildCoapRequest(byte[] token, String jsonString) {
67 CoapRequest coapRequest = new CoapRequest(CoapMethod.GET);
69 coapRequest.setToken(token);
71 byte[] bytes = ByteBuffer.allocate(4)
72 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
73 coapRequest.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
75 coapRequest.setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
82 * public String getJsonAuthServerResponse() {
84 * return "{ \"" + OAuthConstant.AUTH_SERVER + "\" : \"" +
85 * OAuthConstant.AUTH_SERVER_GITHUB + "\" }"; }
87 * public String getJsonAuthInfoResponse() {
89 * return "\n{\n" + " \"" + OAuthConstant.AUTH_ADDRESS + "\" : \"" +
90 * OAuthConstant.GITHUB_ADDRESS + "\",\n" + " \"" +
91 * OAuthConstant.AUTH_RESPONSE_TYPE + "\" : \"" +
92 * OAuthConstant.AUTH_CODE_VALUE + "\",\n" + " \"" +
93 * OAuthConstant.AUTH_CLIENT_ID + "\" : \"" + OAuthConstant.GITHUB_CLIENT_ID
94 * + "\",\n" + " \"" + OAuthConstant.AUTH_REDIRECT_URI + "\" : \"" +
95 * OAuthConstant.GITHUB_REDIRECT_URL + "\"" + "\n}"; }
97 * public String getJsonAuthQueryRequest(String auth_server, String
98 * auth_code, String auth_di) {
100 * return "\n{\n" + " \"" + OAuthConstant.AUTH_TYPE + "\" : \"" +
101 * OAuthConstant.AUTH_TYPE_QUERY + "\",\n" + " \"" +
102 * OAuthConstant.AUTH_SERVER + "\" : \"" + auth_server + "\",\n" + " \"" +
103 * OAuthConstant.AUTH_CODE + "\" : \"" + auth_code + "\",\n" + " \"" +
104 * OAuthConstant.AUTH_DEVICE_ID + "\" : \"" + auth_di + "\"" + "\n}"; }