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.resources;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.iotivity.cloud.accountserver.AccountServerManager;
29 import org.iotivity.cloud.accountserver.Const;
30 import org.iotivity.cloud.accountserver.util.CoapMessageBuilder;
31 import org.iotivity.cloud.accountserver.util.JSONUtil;
32 import org.iotivity.cloud.base.Resource;
33 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
34 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
35 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
36 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
37 import org.iotivity.cloud.util.Logger;
39 import io.netty.channel.ChannelHandlerContext;
43 * This class provides a set of APIs to handle requests for publishing and
47 public class AccountResource extends Resource {
49 public AccountResource() {
50 setUri(Const.ACCOUNT_URI);
54 public void onRequestReceived(ChannelHandlerContext ctx,
55 CoapRequest request) {
57 Logger.d("AccountResource IN");
59 if (ctx == null || request == null) {
60 Logger.d("ctx or request msg is null");
64 CoapMethod method = request.getRequestMethod();
69 handleGetRequest(ctx, request);
70 } catch (Exception e) {
77 handlePostRequest(ctx, request);
78 } catch (Exception e) {
84 Logger.w("method[" + method + "] is not supported");
91 * API for handling GET message
94 * ChannelHandlerContext of request message
96 * CoAP request message
99 private void handleGetRequest(ChannelHandlerContext ctx,
100 CoapRequest request) throws Exception {
102 String reqType = extractQuery(request, Const.REQ_TYPE);
105 throw new IllegalArgumentException(
106 "request type is null in query!");
108 CoapResponse response = null;
112 case Const.TYPE_FIND:
113 response = handleFindRequest(request);
116 Logger.w("reqType[" + reqType + "] is not supported");
124 * API for handling POST message
127 * ChannelHandlerContext of request message
129 * CoAP request message
132 private void handlePostRequest(ChannelHandlerContext ctx,
133 CoapRequest request) throws Exception {
135 String reqType = extractQuery(request, Const.REQ_TYPE);
138 throw new IllegalArgumentException(
139 "request type is null in query!");
141 CoapResponse response = null;
144 case Const.TYPE_PUBLISH:
145 response = handlePublishRequest(request);
148 throw new IllegalArgumentException(
149 "request type is not supported");
155 private CoapResponse handlePublishRequest(CoapRequest request) {
157 String payload = request.getPayloadString();
159 JSONUtil util = new JSONUtil();
160 String userId = util.parseJSON(payload, Const.REQUEST_USER_ID);
161 String deviceId = util.parseJSON(payload, Const.REQUEST_DEVICE_ID);
163 Logger.d("userId: " + userId + ", deviceId: " + deviceId);
165 AccountServerManager oauthServerManager = new AccountServerManager();
166 Boolean status = oauthServerManager.registerUserAccount(userId,
169 Logger.d("status : " + status);
171 CoapMessageBuilder responseMessage = new CoapMessageBuilder();
172 CoapResponse coapResponse = null;
175 coapResponse = responseMessage.buildCoapResponse(request.getToken(),
178 coapResponse = responseMessage.buildCoapResponse(request.getToken(),
179 CoapStatus.INTERNAL_SERVER_ERROR);
185 private CoapResponse handleFindRequest(CoapRequest request) {
187 String payload = request.getPayloadString();
188 // String payload = getPayloadString(request.getPayload());
190 JSONUtil util = new JSONUtil();
191 String userId = util.parseJSON(payload, Const.REQUEST_USER_ID);
193 Logger.d("userId: " + userId);
195 AccountServerManager oauthServerManager = new AccountServerManager();
196 ArrayList<String> deviceList = oauthServerManager
197 .requestAccountDevices(userId);
199 ResponseObject response = new ResponseObject();
200 response.setDeviceList(deviceList);
202 String responseJson = convertFindResponseToJson(response);
203 Logger.d("responseJson: " + responseJson);
205 CoapMessageBuilder responseMessage = new CoapMessageBuilder();
206 CoapResponse coapResponse = responseMessage.buildCoapResponse(
207 request.getToken(), responseJson, CoapStatus.CONTENT);
212 private String convertFindResponseToJson(ResponseObject response) {
214 HashMap<Object, Object> responseMap = new HashMap<Object, Object>();
216 ArrayList<String> deviceList = response.getDeviceList();
217 responseMap.put(Const.RESPONSE_DEVICES, deviceList);
219 JSONUtil jsonUtil = new JSONUtil();
220 String responseJson = jsonUtil.writeJSON(responseMap);
225 private String extractQuery(CoapRequest request, String key) {
229 List<String> Segments = request.getUriQuerySegments();
231 for (String s : Segments) {
233 String pair[] = s.split("=");
235 if (pair[0].equals(key)) {
245 * private static String getPayloadString(byte[] payload) {
247 * if (payload == null) return "";
249 * return new String(payload, Charset.forName("UTF-8")); }