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