[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / account / AccountResource.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;
23
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27
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;
41
42 /**
43  *
44  * This class provides a set of APIs to manage resources corresponding with user
45  * account
46  *
47  */
48 public class AccountResource extends Resource {
49
50     private Cbor<HashMap<String, Object>> mCbor      = new Cbor<>();
51
52     private AccountManager                mAsManager = new AccountManager();
53
54     public AccountResource() {
55         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
56
57     }
58
59     @Override
60     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
61             throws ServerException {
62
63         IResponse response = null;
64
65         switch (request.getMethod()) {
66
67             case POST:
68                 // make sign-up response message
69                 response = handlePostSignUp(request);
70                 break;
71
72             case GET:
73                 response = handleGetSearch(request);
74                 break;
75
76             case DELETE:
77                 response = handleDeleteDevice(request);
78                 break;
79
80             default:
81                 throw new BadRequestException(
82                         request.getMethod() + " request type is not support");
83         }
84         // send sign-up response to the source device
85         srcDevice.sendResponse(response);
86     }
87
88     private IResponse handlePostSignUp(IRequest request)
89             throws ServerException {
90
91         HashMap<String, Object> payloadData = mCbor
92                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
93
94         if (payloadData == null) {
95             throw new BadRequestException("payload is null");
96         }
97
98         HashMap<String, Object> responsePayload = null;
99
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),
104                 payloadData)) {
105
106             String did = payloadData.get(Constants.REQ_DEVICE_ID).toString();
107             String authCode = payloadData.get(Constants.REQ_AUTH_CODE)
108                     .toString();
109             String authProvider = payloadData.get(Constants.REQ_AUTH_PROVIDER)
110                     .toString();
111
112             Log.d("authCode: " + authCode);
113
114             Object options = payloadData.get(Constants.REQ_AUTH_OPTIONS);
115
116             responsePayload = mAsManager.signUp(did, authCode, authProvider,
117                     options);
118         }
119
120         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
121                 ContentFormat.APPLICATION_CBOR,
122                 mCbor.encodingPayloadToCbor(responsePayload));
123     }
124
125     private IResponse handleGetSearch(IRequest request) {
126         HashMap<String, Object> responsePayload = null;
127
128         HashMap<String, List<String>> queryData = request.getUriQueryMap();
129
130         if (queryData == null) {
131             throw new BadRequestException("query is null");
132         }
133         List<String> suid = queryData.get(Constants.REQ_UUID_ID);
134         List<String> criteria = queryData.get(Constants.REQ_SEARCH_CRITERIA);
135
136         if (suid != null) {
137             responsePayload = mAsManager.searchUserAboutUuid(suid.get(0));
138         } else if (criteria != null) {
139             responsePayload = mAsManager
140                     .searchUserAboutCriteria(criteria.get(0));
141
142         } else {
143             throw new BadRequestException(
144                     "uid and search query param are null");
145         }
146
147         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
148                 ContentFormat.APPLICATION_CBOR,
149                 mCbor.encodingPayloadToCbor(responsePayload));
150     }
151
152     private IResponse handleDeleteDevice(IRequest request) {
153
154         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
155
156         if (checkQueryException(Arrays.asList(Constants.REQ_UUID_ID,
157                 Constants.REQ_DEVICE_ID, Constants.REQ_ACCESS_TOKEN),
158                 queryMap)) {
159
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)
163                     .get(0);
164             if (!mAsManager.deleteDevice(uid, did, accesstoken))
165                 throw new UnAuthorizedException("accesstoken is not valid");
166         }
167
168         return MessageBuilder.createResponse(request, ResponseStatus.DELETED);
169     }
170 }