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.account;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.iotivity.cloud.accountserver.Constants;
29 import org.iotivity.cloud.accountserver.resources.account.AccountManager.SearchOperation;
30 import org.iotivity.cloud.base.device.Device;
31 import org.iotivity.cloud.base.exception.ServerException;
32 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
33 import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
34 import org.iotivity.cloud.base.protocols.IRequest;
35 import org.iotivity.cloud.base.protocols.IResponse;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
38 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
39 import org.iotivity.cloud.base.resource.Resource;
40 import org.iotivity.cloud.util.Cbor;
41 import org.iotivity.cloud.util.Log;
45 * This class provides a set of APIs to manage resources corresponding with user
49 public class AccountResource extends Resource {
51 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
53 private AccountManager mAsManager = new AccountManager();
55 public AccountResource() {
56 super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
61 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
62 throws ServerException {
64 IResponse response = null;
66 switch (request.getMethod()) {
69 // make sign-up response message
70 response = handlePostSignUp(request);
74 response = handleGetSearch(request);
78 response = handleDeleteDevice(request);
82 throw new BadRequestException(
83 request.getMethod() + " request type is not support");
85 // send sign-up response to the source device
86 srcDevice.sendResponse(response);
89 private IResponse handlePostSignUp(IRequest request)
90 throws ServerException {
92 HashMap<String, Object> payloadData = mCbor
93 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
95 if (payloadData == null) {
96 throw new BadRequestException("payload is null");
99 HashMap<String, Object> responsePayload = null;
101 // payload verification if the mandatory properties are
102 // included in the payload
103 if (checkPayloadException(Arrays.asList(Constants.REQ_DEVICE_ID,
104 Constants.REQ_AUTH_CODE, Constants.REQ_AUTH_PROVIDER),
107 String did = payloadData.get(Constants.REQ_DEVICE_ID).toString();
108 String authCode = payloadData.get(Constants.REQ_AUTH_CODE)
110 String authProvider = payloadData.get(Constants.REQ_AUTH_PROVIDER)
113 Log.d("authCode: " + authCode);
115 Object options = payloadData.get(Constants.REQ_AUTH_OPTIONS);
117 responsePayload = mAsManager.signUp(did, authCode, authProvider,
121 return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
122 ContentFormat.APPLICATION_CBOR,
123 mCbor.encodingPayloadToCbor(responsePayload));
126 private IResponse handleGetSearch(IRequest request) {
128 HashMap<String, List<String>> queryData = request.getUriQueryMap();
130 if (queryData == null) {
131 throw new BadRequestException("query is null");
134 HashMap<String, Object> responsePayload = null;
136 String uriQuery = request.getUriQuery();
138 // AND or OR operation to find users
139 if (uriQuery != null && uriQuery.contains(",")) {
140 queryData = mAsManager.getQueryMap(uriQuery, ",");
141 responsePayload = (mAsManager.searchUserUsingCriteria(queryData,
142 SearchOperation.AND));
144 responsePayload = (mAsManager.searchUserUsingCriteria(queryData,
145 SearchOperation.OR));
148 Log.d("Search criteria query : " + queryData);
150 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
151 ContentFormat.APPLICATION_CBOR,
152 mCbor.encodingPayloadToCbor(responsePayload));
155 private IResponse handleDeleteDevice(IRequest request) {
157 HashMap<String, List<String>> queryMap = request.getUriQueryMap();
159 if (checkQueryException(Arrays.asList(Constants.REQ_UUID_ID,
160 Constants.REQ_DEVICE_ID, Constants.REQ_ACCESS_TOKEN),
163 String uid = queryMap.get(Constants.REQ_UUID_ID).get(0);
164 String did = queryMap.get(Constants.REQ_DEVICE_ID).get(0);
165 String accesstoken = queryMap.get(Constants.REQ_ACCESS_TOKEN)
167 if (!mAsManager.deleteDevice(uid, did, accesstoken))
168 throw new UnAuthorizedException("accesstoken is not valid");
171 return MessageBuilder.createResponse(request, ResponseStatus.DELETED);