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.device;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
28 import org.iotivity.cloud.accountserver.AccountServerManager;
29 import org.iotivity.cloud.accountserver.Constants;
30 import org.iotivity.cloud.accountserver.token.TokenManager;
31 import org.iotivity.cloud.base.device.Device;
32 import org.iotivity.cloud.base.exception.ServerException;
33 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
34 import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.IResponse;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.base.resource.Resource;
41 import org.iotivity.cloud.util.Cbor;
43 public class DeviceResource extends Resource {
45 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
47 TokenManager mTokenManager = new TokenManager();
49 private AccountServerManager mAsManager = new AccountServerManager();
51 public DeviceResource() {
52 super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
53 Constants.ACCOUNT_URI, Constants.DEVICE_URI));
57 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
58 throws ServerException {
60 IResponse response = null;
62 switch (request.getMethod()) {
64 // Used for getting devices.
65 response = handleGetDevice(request);
69 throw new BadRequestException(
70 request.getMethod() + " request type is not support");
72 srcDevice.sendResponse(response);
75 private IResponse handleGetDevice(IRequest request) throws ServerException {
77 String accessToken = request.getUriQueryMap().get("accesstoken").get(0);
79 if (accessToken == null) {
80 throw new BadRequestException("AccessToken is empty");
85 res = mTokenManager.verifyAccessToken(accessToken);
88 throw new UnAuthorizedException("AccessToken is unauthorized");
91 String userId = mAsManager.requestUserId(accessToken);
94 throw new BadRequestException("userid is invalid");
97 ArrayList<String> deviceList = mAsManager.requestAccountDevices(userId);
99 if (request.getUriQueryMap().get("di") != null) {
100 if (deviceList.containsAll(request.getUriQueryMap().get("di"))) {
101 return MessageBuilder.createResponse(request,
102 ResponseStatus.VALID);
104 throw new UnAuthorizedException("di is invalid");
107 HashMap<String, Object> responsePayload = new HashMap<String, Object>();
108 responsePayload.put(Constants.RESP_DEVICES, deviceList);
110 return MessageBuilder.createResponse(request,
111 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
112 mCbor.encodingPayloadToCbor(responsePayload));