1e57ec7dc3630bda9b0c05574b1870a47fec5ef6
[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 public class SessionResource extends Resource {
41
42     private Cbor<HashMap<String, Object>> mCbor      = new Cbor<>();
43
44     private AccountManager                mAsManager = new AccountManager();
45
46     public SessionResource() {
47         super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
48                 Constants.ACCOUNT_URI, Constants.SESSION_URI));
49     }
50
51     @Override
52     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
53             throws ServerException {
54
55         IResponse response = null;
56
57         switch (request.getMethod()) {
58             case POST:
59                 // Used for user sign-in & out
60                 response = handlePostSigninout(request);
61                 break;
62
63             default:
64                 throw new BadRequestException(
65                         request.getMethod() + " request type is not support");
66         }
67
68         srcDevice.sendResponse(response);
69     }
70
71     private IResponse handlePostSigninout(IRequest request)
72             throws ServerException {
73
74         // exception handle before getting payload
75         if (request.getPayload() == null) {
76             throw new BadRequestException("payload is empty");
77         }
78
79         HashMap<String, Object> payloadData = mCbor
80                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
81
82         if (payloadData == null) {
83             throw new BadRequestException("CBOR parsing failed");
84         }
85
86         HashMap<String, Object> responsePayload = null;
87
88         if (checkPayloadException(
89                 Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_DEVICE_ID,
90                         Constants.REQ_ACCESS_TOKEN, Constants.REQ_LOGIN),
91                 payloadData)) {
92
93             String uuid = payloadData.get(Constants.REQ_UUID_ID).toString();
94             String deviceId = payloadData.get(Constants.REQ_DEVICE_ID)
95                     .toString();
96             String accessToken = payloadData.get(Constants.REQ_ACCESS_TOKEN)
97                     .toString();
98             boolean signinRequest = (boolean) payloadData
99                     .get(Constants.REQ_LOGIN);
100
101             if (signinRequest) {
102                 responsePayload = mAsManager.signInOut(uuid, deviceId,
103                         accessToken);
104             } else {
105                 mAsManager.signInOut(uuid, deviceId, accessToken);
106                 return MessageBuilder.createResponse(request,
107                         ResponseStatus.CHANGED);
108             }
109         }
110
111         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
112                 ContentFormat.APPLICATION_CBOR,
113                 mCbor.encodingPayloadToCbor(responsePayload));
114     }
115 }