Remove classpath files and update ignore
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / resources / directory / res / DiscoveryResource.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.rdserver.resources.directory.res;
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.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;
44
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<>();
49
50     public DiscoveryResource() {
51         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
52     }
53
54     @Override
55     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
56             throws ServerException {
57
58         IResponse response = null;
59
60         switch (request.getMethod()) {
61             case GET:
62                 response = handleGetRequest(request);
63                 break;
64
65             default:
66                 throw new BadRequestException(
67                         request.getMethod() + " request type is not supported");
68         }
69
70         srcDevice.sendResponse(response);
71     }
72
73     private IResponse handleGetRequest(IRequest request)
74             throws ServerException {
75
76         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
77
78         ArrayList<DiscoveryPayload> resourceList = new ArrayList<DiscoveryPayload>();
79
80         if (queryMap == null) {
81             throw new PreconditionFailedException("query is null");
82         }
83
84         List<String> deviceList = queryMap.get(Constants.DEVICE_ID);
85
86         if (deviceList == null) {
87             throw new PreconditionFailedException(
88                     "di property is not included");
89         }
90
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;
95
96         // TODO: Multiple RT or ITF support required
97         if (listRT != null) {
98             key = Constants.RESOURCE_TYPE;
99             value = listRT.get(0);
100         } else if (listITF != null) {
101             key = Constants.INTERFACE;
102             value = listITF.get(0);
103         } else {
104             throw new PreconditionFailedException(
105                     "rt or if property is not included");
106         }
107
108         for (String deviceId : deviceList) {
109             foundResList = DBManager.getInstance().findResourceAboutDi(deviceId,
110                     key, value);
111             if (foundResList != null) {
112                 resourceList.add(makeDiscoveryPayloadSegment(foundResList));
113             }
114         }
115
116         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
117                 ContentFormat.APPLICATION_CBOR,
118                 createDiscoveryResponse(resourceList));
119     }
120
121     private DiscoveryPayload makeDiscoveryPayloadSegment(
122             ArrayList<HashMap<Object, Object>> foundResList) {
123
124         ArrayList<DiscoveryLinks> discoveryLinksList = new ArrayList<DiscoveryLinks>();
125
126         for (HashMap<Object, Object> res : foundResList) {
127             DiscoveryLinks discoveryLinksPayload = new DiscoveryLinks();
128             discoveryLinksPayload = mDiscoveryLinkTypeManager
129                     .convertMaptoObject(res, discoveryLinksPayload);
130             discoveryLinksList.add(discoveryLinksPayload);
131         }
132
133         DiscoveryPayload discoveryPayload = new DiscoveryPayload();
134
135         DiscoveryTags tagsPayload = new DiscoveryTags();
136
137         tagsPayload = mDiscoveryTagsTypeManager
138                 .convertMaptoObject(foundResList.get(0), tagsPayload);
139
140         discoveryPayload.setTags(tagsPayload);
141         discoveryPayload.setLinks(discoveryLinksList);
142
143         return discoveryPayload;
144     }
145
146     private byte[] createDiscoveryResponse(
147             ArrayList<DiscoveryPayload> discoveryPayloadList) {
148         ArrayList<HashMap<Object, Object>> responseMapList = new ArrayList<HashMap<Object, Object>>();
149
150         for (DiscoveryPayload discoveryPayload : discoveryPayloadList) {
151             HashMap<Object, Object> responseSegment = null;
152             DiscoveryTags tags = discoveryPayload.getTags();
153             responseSegment = mDiscoveryTagsTypeManager
154                     .convertObjectToMap(tags);
155
156             ArrayList<DiscoveryLinks> discoveryLinksList = discoveryPayload
157                     .getLinks();
158
159             ArrayList<HashMap<Object, Object>> links = new ArrayList<HashMap<Object, Object>>();
160
161             for (DiscoveryLinks discoveryLinks : discoveryLinksList) {
162                 links.add(mDiscoveryLinkTypeManager
163                         .convertObjectToMap(discoveryLinks));
164             }
165             responseSegment.put(Constants.LINKS, links);
166
167             responseMapList.add(responseSegment);
168         }
169
170         Log.i("discover payload :" + responseMapList.toString());
171
172         byte[] encodedPaylod = mCbor.encodingPayloadToCbor(responseMapList);
173
174         return encodedPaylod;
175     }
176 }