Modify subscriber list in Invite resource
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / acl / invite / InviteResource.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.acl.invite;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import org.iotivity.cloud.accountserver.Constants;
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.PreconditionFailedException;
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
42 public class InviteResource extends Resource {
43
44     private InviteManager                 mInviteManager = new InviteManager();
45
46     private Cbor<HashMap<String, Object>> mCbor          = new Cbor<>();
47
48     public InviteResource() {
49         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
50                 Constants.INVITE_URI));
51     }
52
53     @Override
54     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
55             throws ServerException {
56
57         IResponse response = null;
58
59         switch (request.getMethod()) {
60             case GET:
61                 response = handleGetRequest(srcDevice, request);
62                 break;
63             case POST:
64                 // handle send-invitation message
65                 response = handlePostRequest(request);
66                 break;
67             case DELETE:
68                 response = handleDeleteRequest(request);
69                 break;
70             default:
71                 throw new BadRequestException(
72                         request.getMethod() + " request type is not supported");
73         }
74
75         srcDevice.sendResponse(response);
76     }
77
78     private IResponse handleGetRequest(Device srcDevice, IRequest request)
79             throws ServerException {
80
81         checkQueryException(Constants.REQ_UUID_ID, request.getUriQueryMap());
82
83         String uid = request.getUriQueryMap().get(Constants.REQ_UUID_ID).get(0);
84         HashMap<String, Object> responsePayload = null;
85
86         switch (request.getObserve()) {
87             case NOTHING:
88                 responsePayload = mInviteManager.getInvitationInfo(uid);
89                 break;
90             case SUBSCRIBE:
91                 responsePayload = mInviteManager.addSubscriber(uid, srcDevice,
92                         request);
93                 break;
94             case UNSUBSCRIBE:
95                 responsePayload = mInviteManager.removeSubscriber(uid, request);
96                 break;
97             default:
98                 break;
99         }
100
101         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
102                 ContentFormat.APPLICATION_CBOR,
103                 mCbor.encodingPayloadToCbor(responsePayload));
104     }
105
106     @SuppressWarnings("unchecked")
107     private IResponse handlePostRequest(IRequest request)
108             throws ServerException {
109
110         HashMap<String, Object> payload = mCbor
111                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
112
113         // check properties in payload
114         checkPayloadException(
115                 Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_INVITE),
116                 payload);
117
118         // get user uid
119         String uid = (String) payload.get(Constants.REQ_UUID_ID);
120         ArrayList<HashMap<String, String>> inviteList = (ArrayList<HashMap<String, String>>) payload
121                 .get(Constants.REQ_INVITE);
122
123         for (HashMap<String, String> invite : inviteList) {
124
125             String gid = invite.get(Constants.REQ_GROUP_ID);
126             String mid = invite.get(Constants.REQ_MEMBER);
127
128             if (gid == null || mid == null) {
129
130                 throw new PreconditionFailedException(
131                         "value of invite property is invalid");
132             }
133
134             mInviteManager.addInvitation(uid, gid, mid);
135         }
136
137         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED);
138     }
139
140     private IResponse handleDeleteRequest(IRequest request)
141             throws ServerException {
142
143         HashMap<String, List<String>> queryParams = request.getUriQueryMap();
144
145         checkQueryException(
146                 Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_GROUP_ID),
147                 queryParams);
148
149         String gid = queryParams.get(Constants.REQ_GROUP_ID).get(0);
150         String uid = queryParams.get(Constants.REQ_UUID_ID).get(0);
151
152         if (queryParams.get(Constants.REQ_MEMBER) == null) {
153             mInviteManager.deleteInvitation(uid, gid);
154         } else {
155             String mid = queryParams.get(Constants.REQ_MEMBER).get(0);
156             mInviteManager.cancelInvitation(uid, gid, mid);
157         }
158
159         return MessageBuilder.createResponse(request, ResponseStatus.DELETED);
160     }
161
162 }