1 package org.iotivity.cloud.accountserver.resources.acl.id;
3 import java.util.Arrays;
4 import java.util.HashMap;
7 import org.iotivity.cloud.accountserver.Constants;
8 import org.iotivity.cloud.base.device.Device;
9 import org.iotivity.cloud.base.exception.ServerException;
10 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
11 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
12 import org.iotivity.cloud.base.protocols.IRequest;
13 import org.iotivity.cloud.base.protocols.IResponse;
14 import org.iotivity.cloud.base.protocols.MessageBuilder;
15 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
16 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
17 import org.iotivity.cloud.base.resource.Resource;
18 import org.iotivity.cloud.util.Cbor;
20 public class AclResource extends Resource {
22 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
23 private static AclManager mAclManager = new AclManager();
25 public AclResource() {
26 super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
30 public static AclManager getInstance() {
35 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
36 throws ServerException {
38 IResponse response = null;
40 if (request.getUriPathSegments().size() > getUriPathSegments().size()
42 throw new BadRequestException("uriPath is invalid");
45 switch (request.getMethod()) {
47 response = handlePutRequest(request);
50 response = handlePostRequest(request);
53 response = handleGetRequest(srcDevice, request);
56 response = handleDeleteRequest(request);
59 throw new BadRequestException(
60 request.getMethod() + " request type is not support");
63 srcDevice.sendResponse(response);
66 private IResponse handlePutRequest(IRequest request)
67 throws ServerException {
69 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
70 String oid = request.getUriQueryMap().get(Constants.REQ_OWNER_ID)
72 String di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
74 if (mAclManager.getAclid(di) == null) {
75 return MessageBuilder.createResponse(request,
76 ResponseStatus.CREATED, ContentFormat.APPLICATION_CBOR,
77 mCbor.encodingPayloadToCbor(
78 mAclManager.createAcl(oid, di)));
80 throw new BadRequestException(
81 "aclid already exists for the given di");
85 throw new BadRequestException("uriPath is invalid");
88 private IResponse handlePostRequest(IRequest request)
89 throws ServerException {
91 HashMap<String, Object> payloadData = mCbor
92 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
94 if (!getUriPathSegments().containsAll(request.getUriPathSegments())) {
95 String aclid = request.getUriPathSegments()
96 .get(getUriPathSegments().size());
98 List<HashMap<String, Object>> aclist = null;
100 checkPayloadException(Constants.REQ_ACL_LIST, payloadData);
102 aclist = (List<HashMap<String, Object>>) payloadData
103 .get(Constants.REQ_ACL_LIST);
104 mAclManager.addAclACE(aclid, aclist);
105 return MessageBuilder.createResponse(request,
106 ResponseStatus.CHANGED);
107 } else if (getUriPathSegments()
108 .containsAll(request.getUriPathSegments())) {
109 String aclid = request.getUriPathSegments()
110 .get(getUriPathSegments().size());
111 String aceid = request.getUriQueryMap().get(Constants.REQ_ACE_ID)
113 HashMap<String, Object> ace = (HashMap<String, Object>) payloadData
114 .get(Constants.REQ_ACL_LIST);
115 mAclManager.updateACE(aclid, aceid, ace);
116 return MessageBuilder.createResponse(request,
117 ResponseStatus.CHANGED);
120 throw new BadRequestException("uriPath is invalid");
124 @SuppressWarnings("unchecked")
126 private IResponse handleGetRequest(Device srcDevice, IRequest request)
127 throws ServerException {
129 HashMap<String, Object> responsePayload = null;
132 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
133 di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID).get(0);
135 throw new PreconditionFailedException("di is invalid");
137 responsePayload = mAclManager.getAclid(di);
139 String aclid = request.getUriPathSegments()
140 .get(getUriPathSegments().size());
141 switch (request.getObserve()) {
143 responsePayload = mAclManager.getAclInfo(aclid);
146 di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
148 responsePayload = mAclManager.addAclSubscriber(aclid, di,
152 di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
154 responsePayload = mAclManager.removeAclSubscriber(aclid,
158 throw new BadRequestException(request.getObserve()
159 + " observe type is not supported");
164 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
165 ContentFormat.APPLICATION_CBOR,
166 mCbor.encodingPayloadToCbor(responsePayload));
170 private IResponse handleDeleteRequest(IRequest request)
171 throws ServerException {
175 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
176 aclid = request.getUriQueryMap().get(Constants.REQ_ACL_ID).get(0);
178 throw new PreconditionFailedException("aclid is invalid");
180 mAclManager.deleteAcl(aclid);
182 aclid = request.getUriPathSegments()
183 .get(getUriPathSegments().size());
184 String aceid = request.getUriQueryMap().get(Constants.REQ_ACE_ID)
187 mAclManager.deleteAclAclist(aclid);
189 mAclManager.deleteAclACE(aclid, aceid);
193 return MessageBuilder.createResponse(request, ResponseStatus.DELETED);