Remove classpath files and update ignore
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / account / device / DeviceResource.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.resources.account.device;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27
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;
42
43 public class DeviceResource extends Resource {
44
45     private Cbor<HashMap<String, Object>> mCbor         = new Cbor<>();
46
47     TokenManager                          mTokenManager = new TokenManager();
48
49     private AccountServerManager          mAsManager    = new AccountServerManager();
50
51     public DeviceResource() {
52         super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
53                 Constants.ACCOUNT_URI, Constants.DEVICE_URI));
54     }
55
56     @Override
57     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
58             throws ServerException {
59
60         IResponse response = null;
61
62         switch (request.getMethod()) {
63             case GET:
64                 // Used for getting devices.
65                 response = handleGetDevice(request);
66                 break;
67
68             default:
69                 throw new BadRequestException(
70                         request.getMethod() + " request type is not support");
71         }
72         srcDevice.sendResponse(response);
73     }
74
75     private IResponse handleGetDevice(IRequest request) throws ServerException {
76
77         String accessToken = request.getUriQueryMap().get("accesstoken").get(0);
78
79         if (accessToken == null) {
80             throw new BadRequestException("AccessToken is empty");
81         }
82
83         Boolean res = false;
84
85         res = mTokenManager.verifyAccessToken(accessToken);
86
87         if (!res) {
88             throw new UnAuthorizedException("AccessToken is unauthorized");
89         }
90
91         String userId = mAsManager.requestUserId(accessToken);
92
93         if (userId == null) {
94             throw new BadRequestException("userid is invalid");
95         }
96
97         ArrayList<String> deviceList = mAsManager.requestAccountDevices(userId);
98
99         if (request.getUriQueryMap().get("di") != null) {
100             String di = request.getUriQueryMap().get("di").get(0);
101             if (deviceList.contains(di)) {
102                 return MessageBuilder.createResponse(request,
103                         ResponseStatus.VALID);
104             } else {
105                 throw new UnAuthorizedException("di is invalid");
106             }
107         } else {
108             HashMap<String, Object> responsePayload = new HashMap<String, Object>();
109             responsePayload.put(Constants.RESP_DEVICES, deviceList);
110
111             return MessageBuilder.createResponse(request,
112                     ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
113                     mCbor.encodingPayloadToCbor(responsePayload));
114         }
115     }
116 }