2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver.resources.acl.group;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
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;
42 public class GroupResource extends Resource {
43 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
45 public GroupResource() {
46 super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
47 Constants.GROUP_URI));
51 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
52 throws ServerException {
54 IResponse response = null;
56 if (request.getUriPathSegments().size() > getUriPathSegments().size()
58 throw new BadRequestException("uriPath is invalid");
61 switch (request.getMethod()) {
63 response = handlePostRequest(request);
66 response = handleGetRequest(srcDevice, request);
69 response = handleDeleteRequest(request);
72 throw new BadRequestException(
73 request.getMethod() + " request type is not support");
75 srcDevice.sendResponse(response);
78 private IResponse handlePostRequest(IRequest request)
79 throws ServerException {
80 HashMap<String, Object> payloadData = mCbor
81 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
83 if (payloadData == null) {
84 throw new BadRequestException("payload is null");
87 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
88 String uuid = payloadData.get(Constants.REQ_GROUP_MASTER_ID)
90 String gtype = (String) payloadData.get(Constants.REQ_GROUP_TYPE)
92 if (uuid == null || gtype == null) {
93 throw new PreconditionFailedException(
94 "value of group property is invalid");
96 return MessageBuilder.createResponse(request,
97 ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR,
98 mCbor.encodingPayloadToCbor(GroupManager.getInstance()
99 .createGroup(uuid, gtype)));
101 String gid = request.getUriPathSegments()
102 .get(getUriPathSegments().size());
104 if (payloadData.containsKey(Constants.REQ_MEMBER_LIST)) {
105 List<String> midList = (List<String>) payloadData
106 .get(Constants.REQ_MEMBER_LIST);
107 if (midList == null) {
108 throw new PreconditionFailedException(
109 "midList property is invalid");
111 GroupManager.getInstance().addGroupMember(gid,
112 new HashSet<String>(midList));
115 if (payloadData.containsKey(Constants.REQ_DEVICE_ID_LIST)) {
116 List<String> diList = (List<String>) payloadData
117 .get(Constants.REQ_DEVICE_ID_LIST);
118 if (diList == null) {
119 throw new PreconditionFailedException(
120 "diList property is invalid");
122 GroupManager.getInstance().addGroupDevice(gid,
123 new HashSet<String>(diList));
126 return MessageBuilder.createResponse(request, ResponseStatus.CHANGED);
130 private IResponse handleGetRequest(Device srcDevice, IRequest request)
131 throws ServerException {
132 HashMap<String, Object> responsePayload = null;
135 if (!request.getUriQueryMap().containsKey(Constants.REQ_MEMBER)) {
136 throw new PreconditionFailedException("mid property is invalid");
139 mid = request.getUriQueryMap().get(Constants.REQ_MEMBER).get(0);
141 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
142 responsePayload = GroupManager.getInstance().getGroupList(mid);
144 String gid = request.getUriPathSegments()
145 .get(getUriPathSegments().size());
146 switch (request.getObserve()) {
148 responsePayload = GroupManager.getInstance()
149 .getGroupInfo(gid, mid);
152 responsePayload = GroupManager.getInstance()
153 .addGroupSubscriber(gid, mid, srcDevice, request);
156 responsePayload = GroupManager.getInstance()
157 .removeGroupSubscriber(gid, mid);
160 throw new BadRequestException(request.getObserve()
161 + " observe type is not support");
164 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
165 ContentFormat.APPLICATION_CBOR,
166 mCbor.encodingPayloadToCbor(responsePayload));
169 private IResponse handleDeleteRequest(IRequest request)
170 throws ServerException {
171 if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
173 checkQueryException(Arrays.asList(Constants.REQ_GROUP_MASTER_ID,
174 Constants.REQ_GROUP_ID), request.getUriQueryMap());
176 String gmid = request.getUriQueryMap()
177 .get(Constants.REQ_GROUP_MASTER_ID).get(0);
178 String gid = request.getUriQueryMap().get(Constants.REQ_GROUP_ID)
181 GroupManager.getInstance().deleteGroup(gmid, gid);
183 String gid = request.getUriPathSegments()
184 .get(getUriPathSegments().size());
185 if (request.getUriQueryMap()
186 .containsKey(Constants.REQ_MEMBER_LIST)) {
187 List<String> midList = request.getUriQueryMap()
188 .get(Constants.REQ_MEMBER_LIST);
189 if (midList == null) {
190 throw new PreconditionFailedException(
191 "midList property is invalid");
193 GroupManager.getInstance().removeGroupMember(gid,
194 new HashSet<String>(midList));
196 if (request.getUriQueryMap()
197 .containsKey(Constants.REQ_DEVICE_ID_LIST)) {
198 List<String> diList = request.getUriQueryMap()
199 .get(Constants.REQ_DEVICE_ID_LIST);
200 if (diList == null) {
201 throw new PreconditionFailedException(
202 "diList property is invalid");
204 GroupManager.getInstance().removeGroupDevice(gid,
205 new HashSet<String>(diList));
208 return MessageBuilder.createResponse(request, ResponseStatus.DELETED);