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