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.Constants;
30 import org.iotivity.cloud.accountserver.util.CoapMessageBuilder;
31 import org.iotivity.cloud.base.Resource;
32 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
33 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
34 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
35 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
36 import org.iotivity.cloud.util.JSONUtil;
37 import org.iotivity.cloud.util.Logger;
39 import io.netty.channel.ChannelHandlerContext;
43 * This class provides a set of APIs to manage resources corresponding with user
47 public class AccountResource extends Resource {
49 public AccountResource() {
50 setUri(Constants.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");
90 private void handleGetRequest(ChannelHandlerContext ctx,
91 CoapRequest request) throws Exception {
93 String reqType = extractQuery(request, Constants.REQ_TYPE);
96 throw new IllegalArgumentException(
97 "request type is null in query!");
99 CoapResponse response = null;
102 case Constants.TYPE_FIND:
103 response = handleFindRequest(request);
106 Logger.w("reqType[" + reqType + "] is not supported");
108 if (response != null) {
109 ctx.writeAndFlush(response);
114 private void handlePostRequest(ChannelHandlerContext ctx,
115 CoapRequest request) throws Exception {
117 String reqType = extractQuery(request, Constants.REQ_TYPE);
120 throw new IllegalArgumentException(
121 "request type is null in query!");
123 CoapResponse response;
126 case Constants.TYPE_PUBLISH:
127 response = handlePublishRequest(request);
130 throw new IllegalArgumentException(
131 "request type is not supported");
134 ctx.writeAndFlush(response);
138 * API for handling request for publishing resource corresponding with user
142 * CoAP request message
143 * @return CoapResponse - CoAP response message with response result
146 private CoapResponse handlePublishRequest(CoapRequest request) {
148 String payload = request.getPayloadString();
150 String userId = JSONUtil.parseJSON(payload, Constants.REQUEST_USER_ID);
151 String deviceId = JSONUtil.parseJSON(payload,
152 Constants.REQUEST_DEVICE_ID);
154 Logger.d("userId: " + userId + ", deviceId: " + deviceId);
156 AccountServerManager oauthServerManager = new AccountServerManager();
157 Boolean status = oauthServerManager.registerUserAccount(userId,
160 Logger.d("status : " + status);
162 CoapMessageBuilder responseMessage = new CoapMessageBuilder();
163 CoapResponse coapResponse;
166 coapResponse = responseMessage.buildCoapResponse(request.getToken(),
169 coapResponse = responseMessage.buildCoapResponse(request.getToken(),
170 CoapStatus.INTERNAL_SERVER_ERROR);
177 * API for handling request for finding resource corresponding with user
181 * CoAP request message
182 * @return CoapResponse - CoAP response message with response result
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, Constants.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(Constants.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 if (Segments != null) {
232 for (String s : Segments) {
234 String pair[] = s.split("=");
236 if (pair[0].equals(key)) {
247 * private static String getPayloadString(byte[] payload) {
249 * if (payload == null) return "";
251 * return new String(payload, Charset.forName("UTF-8")); }