[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / account / session / SessionResource.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.session;
23
24 import java.util.Arrays;
25 import java.util.HashMap;
26
27 import org.iotivity.cloud.accountserver.Constants;
28 import org.iotivity.cloud.accountserver.resources.account.AccountManager;
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.protocols.IRequest;
33 import org.iotivity.cloud.base.protocols.IResponse;
34 import org.iotivity.cloud.base.protocols.MessageBuilder;
35 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
36 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
37 import org.iotivity.cloud.base.resource.Resource;
38 import org.iotivity.cloud.util.Cbor;
39
40 /**
41  *
42  * This class provides a set of APIs to handle sign-in/sign-out requests
43  *
44  */
45
46 public class SessionResource extends Resource {
47
48     private Cbor<HashMap<String, Object>> mCbor      = new Cbor<>();
49
50     private AccountManager                mAsManager = new AccountManager();
51
52     public SessionResource() {
53         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI,
54                 Constants.SESSION_URI));
55     }
56
57     @Override
58     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
59             throws ServerException {
60
61         IResponse response = null;
62
63         switch (request.getMethod()) {
64             case POST:
65                 // Used for user sign-in & out
66                 response = handlePostSigninout(request);
67                 break;
68
69             default:
70                 throw new BadRequestException(
71                         request.getMethod() + " request type is not support");
72         }
73         // send sign-up response to the source device
74         srcDevice.sendResponse(response);
75     }
76
77     private IResponse handlePostSigninout(IRequest request)
78             throws ServerException {
79
80         HashMap<String, Object> payloadData = mCbor
81                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
82
83         if (payloadData == null) {
84             throw new BadRequestException("payload is null");
85         }
86
87         HashMap<String, Object> responsePayload = null;
88
89         checkPayloadException(Arrays.asList(Constants.REQ_DEVICE_ID,
90                 Constants.REQ_ACCESS_TOKEN), payloadData);
91
92         String deviceId = payloadData.get(Constants.REQ_DEVICE_ID).toString();
93         String accessToken = payloadData.get(Constants.REQ_ACCESS_TOKEN)
94                 .toString();
95
96         checkPayloadException(Constants.REQ_LOGIN, payloadData);
97
98         boolean signinRequest = (boolean) payloadData.get(Constants.REQ_LOGIN);
99
100         if (signinRequest) {
101             // sign-in response payload
102             checkPayloadException(Constants.REQ_UUID_ID, payloadData);
103             String uuid = payloadData.get(Constants.REQ_UUID_ID).toString();
104             responsePayload = mAsManager.signInOut(uuid, deviceId, accessToken);
105         } else {
106             // sign-out response
107             checkQueryException(Constants.REQ_UUID_ID,
108                     request.getUriQueryMap());
109             String uuid = request.getUriQueryMap().get(Constants.REQ_UUID_ID)
110                     .get(0);
111             mAsManager.signInOut(uuid, deviceId, accessToken);
112             return MessageBuilder.createResponse(request,
113                     ResponseStatus.CHANGED);
114         }
115
116         // sign-in response
117         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
118                 ContentFormat.APPLICATION_CBOR,
119                 mCbor.encodingPayloadToCbor(responsePayload));
120     }
121 }