[Simulator] Minor UI changes fixing the IOT-1087.
[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 utility for making CoAP request and response.
36  * 
37  */
38 public class CoapMessageBuilder {
39
40     public static final int APPLICATION_JSON = 50;
41
42     public CoapResponse buildCoapResponse(byte[] token, CoapStatus status) {
43
44         return buildCoapResponse(token, null, status);
45     }
46
47     public CoapResponse buildCoapResponse(byte[] token, String jsonString,
48             CoapStatus status) {
49
50         CoapResponse coapResponse = new CoapResponse(status);
51
52         coapResponse.setToken(token);
53
54         byte[] bytes = ByteBuffer.allocate(4)
55                 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
56         coapResponse.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
57
58         if (jsonString != null)
59             coapResponse
60                     .setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
61
62         return coapResponse;
63     }
64
65     public CoapRequest buildCoapRequest(byte[] token, String jsonString) {
66
67         CoapRequest coapRequest = new CoapRequest(CoapMethod.GET);
68
69         coapRequest.setToken(token);
70
71         byte[] bytes = ByteBuffer.allocate(4)
72                 .putInt(CoapMessageBuilder.APPLICATION_JSON).array();
73         coapRequest.addOption(CoapOption.CONTENT_FORMAT.getvalue(), bytes);
74
75         coapRequest.setPayload(jsonString.getBytes(StandardCharsets.UTF_8));
76
77         return coapRequest;
78     }
79
80     /*
81      * 
82      * public String getJsonAuthServerResponse() {
83      * 
84      * return "{ \"" + OAuthConstant.AUTH_SERVER + "\" : \"" +
85      * OAuthConstant.AUTH_SERVER_GITHUB + "\" }"; }
86      * 
87      * public String getJsonAuthInfoResponse() {
88      * 
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}"; }
96      * 
97      * public String getJsonAuthQueryRequest(String auth_server, String
98      * auth_code, String auth_di) {
99      * 
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}"; }
105      */
106 }