8a0e91aee93e688a5c901383c1b8680681af9d9d
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / acl / id / AclResource.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.id;
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.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.exception.ServerException.PreconditionFailedException;
33 import org.iotivity.cloud.base.protocols.IRequest;
34 import org.iotivity.cloud.base.protocols.IResponse;
35 import org.iotivity.cloud.base.protocols.MessageBuilder;
36 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
37 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
38 import org.iotivity.cloud.base.resource.Resource;
39 import org.iotivity.cloud.util.Cbor;
40
41 public class AclResource extends Resource {
42
43     private Cbor<HashMap<String, Object>> mCbor       = new Cbor<>();
44     private static AclManager             mAclManager = new AclManager();
45
46     public AclResource() {
47         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
48                 Constants.ID_URI));
49     }
50
51     public static AclManager getInstance() {
52         return mAclManager;
53     }
54
55     @Override
56     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
57             throws ServerException {
58
59         IResponse response = null;
60
61         if (request.getUriPathSegments().size() > getUriPathSegments().size()
62                 + 1) {
63             throw new BadRequestException("uriPath is invalid");
64         }
65
66         switch (request.getMethod()) {
67             case PUT:
68                 response = handlePutRequest(request);
69                 break;
70             case POST:
71                 response = handlePostRequest(request);
72                 break;
73             case GET:
74                 response = handleGetRequest(srcDevice, request);
75                 break;
76             case DELETE:
77                 response = handleDeleteRequest(request);
78                 break;
79             default:
80                 throw new BadRequestException(
81                         request.getMethod() + " request type is not support");
82         }
83
84         srcDevice.sendResponse(response);
85     }
86
87     private IResponse handlePutRequest(IRequest request)
88             throws ServerException {
89
90         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
91             String oid = request.getUriQueryMap().get(Constants.REQ_OWNER_ID)
92                     .get(0);
93             String di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
94                     .get(0);
95             if (mAclManager.getAclid(di) == null) {
96                 return MessageBuilder.createResponse(request,
97                         ResponseStatus.CREATED, ContentFormat.APPLICATION_CBOR,
98                         mCbor.encodingPayloadToCbor(
99                                 mAclManager.createAcl(oid, di)));
100             } else {
101                 throw new BadRequestException(
102                         "aclid already exists for the given di");
103             }
104         }
105
106         throw new BadRequestException("uriPath is invalid");
107     }
108
109     private IResponse handlePostRequest(IRequest request)
110             throws ServerException {
111
112         if (request.getPayload() == null) {
113             throw new BadRequestException("payload is null");
114         }
115
116         HashMap<String, Object> payloadData = mCbor
117                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
118
119         if (null != request.getUriQueryMap()) {
120             String aclid = request.getUriPathSegments()
121                     .get(getUriPathSegments().size());
122             String aceid = request.getUriQueryMap().get(Constants.REQ_ACE_ID)
123                     .get(0);
124             List<HashMap<String, Object>> aclist = (List<HashMap<String, Object>>) payloadData
125                     .get(Constants.REQ_ACL_LIST);
126             mAclManager.updateACE(aclid, aceid, aclist.get(0));
127             return MessageBuilder.createResponse(request,
128                     ResponseStatus.CHANGED);
129         } else if (!getUriPathSegments()
130                 .containsAll(request.getUriPathSegments())) {
131             String aclid = request.getUriPathSegments()
132                     .get(getUriPathSegments().size());
133
134             List<HashMap<String, Object>> aclist = null;
135             if (!payloadData.containsKey(Constants.REQ_ACL_LIST)) {
136                 throw new BadRequestException("aclist not included in payload");
137             }
138             aclist = (List<HashMap<String, Object>>) payloadData
139                     .get(Constants.REQ_ACL_LIST);
140             aclist = mAclManager.addAclACE(aclid, aclist);
141             payloadData.put(Constants.REQ_ACL_LIST, aclist);
142             return MessageBuilder.createResponse(request,
143                     ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR,
144                     mCbor.encodingPayloadToCbor(payloadData));
145         }
146         throw new BadRequestException("uriPath is invalid");
147     }
148
149     @SuppressWarnings("unchecked")
150
151     private IResponse handleGetRequest(Device srcDevice, IRequest request)
152             throws ServerException {
153
154         HashMap<String, Object> responsePayload = null;
155         String di = null;
156
157         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
158             di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID).get(0);
159             if (di == null) {
160                 throw new PreconditionFailedException("di is invalid");
161             }
162             responsePayload = mAclManager.getAclid(di);
163         } else {
164             String aclid = request.getUriPathSegments()
165                     .get(getUriPathSegments().size());
166             switch (request.getObserve()) {
167                 case NOTHING:
168                     responsePayload = mAclManager.getAclInfo(aclid);
169                     break;
170                 case SUBSCRIBE:
171                     di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
172                             .get(0);
173                     responsePayload = mAclManager.addAclSubscriber(aclid, di,
174                             srcDevice, request);
175                     break;
176                 case UNSUBSCRIBE:
177                     di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
178                             .get(0);
179                     responsePayload = mAclManager.removeAclSubscriber(aclid,
180                             di);
181                     break;
182                 default:
183                     throw new BadRequestException(request.getObserve()
184                             + " observe type is not supported");
185             }
186
187         }
188
189         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
190                 ContentFormat.APPLICATION_CBOR,
191                 mCbor.encodingPayloadToCbor(responsePayload));
192
193     }
194
195     private IResponse handleDeleteRequest(IRequest request)
196             throws ServerException {
197
198         String aclid = null;
199
200         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
201             aclid = request.getUriQueryMap().get(Constants.REQ_ACL_ID).get(0);
202             if (aclid == null) {
203                 throw new PreconditionFailedException("aclid is invalid");
204             }
205             mAclManager.deleteAcl(aclid);
206         } else {
207             aclid = request.getUriPathSegments()
208                     .get(getUriPathSegments().size());
209
210             if (request.getUriQueryMap() == null) {
211                 mAclManager.deleteAclAclist(aclid);
212             } else if (request.getUriQueryMap()
213                     .containsKey(Constants.REQ_ACE_ID)) {
214                 String aceid = request.getUriQueryMap()
215                         .get(Constants.REQ_ACE_ID).get(0);
216                 mAclManager.deleteAclACE(aclid, aceid);
217             } else {
218                 throw new BadRequestException("uriPath is invalid");
219             }
220         }
221
222         return MessageBuilder.createResponse(request, ResponseStatus.DELETED);
223     }
224
225 }