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.rdserver.resources.directory.res;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
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.rdserver.Constants;
40 import org.iotivity.cloud.rdserver.db.DBManager;
41 import org.iotivity.cloud.rdserver.util.TypeCastingManager;
42 import org.iotivity.cloud.util.Cbor;
43 import org.iotivity.cloud.util.Log;
45 public class DiscoveryResource extends Resource {
46 private Cbor<HashMap<Object, Object>> mCbor = new Cbor<>();
47 private TypeCastingManager<DiscoveryTags> mDiscoveryTagsTypeManager = new TypeCastingManager<>();
48 private TypeCastingManager<DiscoveryLinks> mDiscoveryLinkTypeManager = new TypeCastingManager<>();
50 public DiscoveryResource() {
51 super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
55 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
56 throws ServerException {
58 IResponse response = null;
60 switch (request.getMethod()) {
62 response = handleGetRequest(request);
66 throw new BadRequestException(
67 request.getMethod() + " request type is not supported");
70 srcDevice.sendResponse(response);
73 private IResponse handleGetRequest(IRequest request)
74 throws ServerException {
76 HashMap<String, List<String>> queryMap = request.getUriQueryMap();
78 ArrayList<DiscoveryPayload> resourceList = new ArrayList<DiscoveryPayload>();
80 if (queryMap == null) {
81 throw new PreconditionFailedException("query is null");
84 List<String> deviceList = queryMap.get(Constants.DEVICE_ID);
86 if (deviceList == null) {
87 throw new PreconditionFailedException(
88 "di property is not included");
91 List<String> listRT = queryMap.get(Constants.RESOURCE_TYPE);
92 List<String> listITF = queryMap.get(Constants.INTERFACE);
93 String key, value = null;
94 ArrayList<HashMap<Object, Object>> foundResList = null;
96 // TODO: Multiple RT or ITF support required
98 key = Constants.RESOURCE_TYPE;
99 value = listRT.get(0);
100 } else if (listITF != null) {
101 key = Constants.INTERFACE;
102 value = listITF.get(0);
104 throw new PreconditionFailedException(
105 "rt or if property is not included");
108 for (String deviceId : deviceList) {
109 foundResList = DBManager.getInstance().findResourceAboutDi(deviceId,
111 if (foundResList != null) {
112 resourceList.add(makeDiscoveryPayloadSegment(foundResList));
116 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
117 ContentFormat.APPLICATION_CBOR,
118 createDiscoveryResponse(resourceList));
121 private DiscoveryPayload makeDiscoveryPayloadSegment(
122 ArrayList<HashMap<Object, Object>> foundResList) {
124 ArrayList<DiscoveryLinks> discoveryLinksList = new ArrayList<DiscoveryLinks>();
126 for (HashMap<Object, Object> res : foundResList) {
127 DiscoveryLinks discoveryLinksPayload = new DiscoveryLinks();
128 discoveryLinksPayload = mDiscoveryLinkTypeManager
129 .convertMaptoObject(res, discoveryLinksPayload);
130 discoveryLinksList.add(discoveryLinksPayload);
133 DiscoveryPayload discoveryPayload = new DiscoveryPayload();
135 DiscoveryTags tagsPayload = new DiscoveryTags();
137 tagsPayload = mDiscoveryTagsTypeManager
138 .convertMaptoObject(foundResList.get(0), tagsPayload);
140 discoveryPayload.setTags(tagsPayload);
141 discoveryPayload.setLinks(discoveryLinksList);
143 return discoveryPayload;
146 private byte[] createDiscoveryResponse(
147 ArrayList<DiscoveryPayload> discoveryPayloadList) {
148 ArrayList<HashMap<Object, Object>> responseMapList = new ArrayList<HashMap<Object, Object>>();
150 for (DiscoveryPayload discoveryPayload : discoveryPayloadList) {
151 HashMap<Object, Object> responseSegment = null;
152 DiscoveryTags tags = discoveryPayload.getTags();
153 responseSegment = mDiscoveryTagsTypeManager
154 .convertObjectToMap(tags);
156 ArrayList<DiscoveryLinks> discoveryLinksList = discoveryPayload
159 ArrayList<HashMap<Object, Object>> links = new ArrayList<HashMap<Object, Object>>();
161 for (DiscoveryLinks discoveryLinks : discoveryLinksList) {
162 links.add(mDiscoveryLinkTypeManager
163 .convertObjectToMap(discoveryLinks));
165 responseSegment.put(Constants.LINKS, links);
167 responseMapList.add(responseSegment);
170 Log.i("discover payload :" + responseMapList.toString());
172 byte[] encodedPaylod = mCbor.encodingPayloadToCbor(responseMapList);
174 return encodedPaylod;