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.base.device.Device;
30 import org.iotivity.cloud.base.exception.ServerException;
31 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
32 import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
33 import org.iotivity.cloud.base.protocols.IRequest;
34 import org.iotivity.cloud.base.protocols.IResponse;
35 import org.iotivity.cloud.base.protocols.MessageBuilder;
36 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
37 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
38 import org.iotivity.cloud.base.resource.Resource;
39 import org.iotivity.cloud.util.Cbor;
40 import org.iotivity.cloud.util.Log;
44 * This class provides a set of APIs to manage resources corresponding with user
48 public class AccountResource extends Resource {
50 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
52 private AccountManager mAsManager = new AccountManager();
54 public AccountResource() {
55 super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
60 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
61 throws ServerException {
63 IResponse response = null;
65 switch (request.getMethod()) {
68 // make sign-up response message
69 response = handlePostSignUp(request);
73 response = handleGetSearch(request);
77 response = handleDeleteDevice(request);
81 throw new BadRequestException(
82 request.getMethod() + " request type is not support");
84 // send sign-up response to the source device
85 srcDevice.sendResponse(response);
88 private IResponse handlePostSignUp(IRequest request)
89 throws ServerException {
91 HashMap<String, Object> payloadData = mCbor
92 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
94 if (payloadData == null) {
95 throw new BadRequestException("payload is null");
98 HashMap<String, Object> responsePayload = null;
100 // payload verification if the mandatory properties are
101 // included in the payload
102 if (checkPayloadException(Arrays.asList(Constants.REQ_DEVICE_ID,
103 Constants.REQ_AUTH_CODE, Constants.REQ_AUTH_PROVIDER),
106 String did = payloadData.get(Constants.REQ_DEVICE_ID).toString();
107 String authCode = payloadData.get(Constants.REQ_AUTH_CODE)
109 String authProvider = payloadData.get(Constants.REQ_AUTH_PROVIDER)
112 Log.d("authCode: " + authCode);
114 Object options = payloadData.get(Constants.REQ_AUTH_OPTIONS);
116 responsePayload = mAsManager.signUp(did, authCode, authProvider,
120 return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
121 ContentFormat.APPLICATION_CBOR,
122 mCbor.encodingPayloadToCbor(responsePayload));
125 private IResponse handleGetSearch(IRequest request) {
126 HashMap<String, Object> responsePayload = null;
128 HashMap<String, List<String>> queryData = request.getUriQueryMap();
130 if (queryData == null) {
131 throw new BadRequestException("query is null");
133 List<String> suid = queryData.get(Constants.REQ_UUID_ID);
134 List<String> criteria = queryData.get(Constants.REQ_SEARCH_CRITERIA);
137 responsePayload = mAsManager.searchUserAboutUuid(suid.get(0));
138 } else if (criteria != null) {
139 responsePayload = mAsManager
140 .searchUserAboutCriteria(criteria.get(0));
143 throw new BadRequestException(
144 "uid and search query param are null");
147 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
148 ContentFormat.APPLICATION_CBOR,
149 mCbor.encodingPayloadToCbor(responsePayload));
152 private IResponse handleDeleteDevice(IRequest request) {
154 HashMap<String, List<String>> queryMap = request.getUriQueryMap();
156 if (checkQueryException(Arrays.asList(Constants.REQ_UUID_ID,
157 Constants.REQ_DEVICE_ID, Constants.REQ_ACCESS_TOKEN),
160 String uid = queryMap.get(Constants.REQ_UUID_ID).get(0);
161 String did = queryMap.get(Constants.REQ_DEVICE_ID).get(0);
162 String accesstoken = queryMap.get(Constants.REQ_ACCESS_TOKEN)
164 if (!mAsManager.deleteDevice(uid, did, accesstoken))
165 throw new UnAuthorizedException("accesstoken is not valid");
168 return MessageBuilder.createResponse(request, ResponseStatus.DELETED);