Modify /oic/res response
[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.protocols.IRequest;
33 import org.iotivity.cloud.base.protocols.IResponse;
34 import org.iotivity.cloud.base.protocols.MessageBuilder;
35 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
36 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
37 import org.iotivity.cloud.base.resource.Resource;
38 import org.iotivity.cloud.rdserver.Constants;
39 import org.iotivity.cloud.rdserver.resources.directory.RDManager;
40 import org.iotivity.cloud.util.Cbor;
41
42 /**
43  * 
44  * This class provides a set of APIs handle requests about resource discovery
45  *
46  */
47 public class DiscoveryResource extends Resource {
48     private Cbor<ArrayList<Object>> mCbor      = new Cbor<>();
49     private RDManager               mRdManager = new RDManager();
50
51     public DiscoveryResource() {
52         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
53     }
54
55     @Override
56     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
57             throws ServerException {
58
59         IResponse response = null;
60
61         switch (request.getMethod()) {
62             case GET:
63                 response = handleGetRequest(request);
64                 break;
65
66             default:
67                 throw new BadRequestException(
68                         request.getMethod() + " request type is not supported");
69         }
70
71         srcDevice.sendResponse(response);
72     }
73
74     private IResponse handleGetRequest(IRequest request)
75             throws ServerException {
76
77         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
78
79         List<String> diList = queryMap.get(Constants.DEVICE_ID);
80         List<String> rtList = queryMap.get(Constants.RESOURCE_TYPE);
81         List<String> ifList = queryMap.get(Constants.INTERFACE);
82
83         ArrayList<Object> response = mRdManager.discoverResource(diList, rtList,
84                 ifList);
85
86         if (response.size() == 0) {
87             return MessageBuilder.createResponse(request,
88                     ResponseStatus.NOT_FOUND);
89         }
90
91         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
92                 ContentFormat.APPLICATION_CBOR,
93                 mCbor.encodingPayloadToCbor(response));
94     }
95
96 }