[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourceFind.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.ciserver.resources.proxy.rd;
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.connector.ConnectorPool;
30 import org.iotivity.cloud.base.device.Device;
31 import org.iotivity.cloud.base.device.IRequestChannel;
32 import org.iotivity.cloud.base.device.IResponseEventHandler;
33 import org.iotivity.cloud.base.exception.ClientException;
34 import org.iotivity.cloud.base.exception.ServerException;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.IResponse;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
39 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.base.resource.Resource;
41 import org.iotivity.cloud.ciserver.Constants;
42 import org.iotivity.cloud.util.Cbor;
43
44 /**
45  *
46  * This class provides a set of APIs to send requests about find resource to
47  * resource directory
48  *
49  */
50
51 public class ResourceFind extends Resource {
52     IRequestChannel                       mASServer = null;
53     IRequestChannel                       mRDServer = null;
54     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
55
56     public ResourceFind() {
57         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
58
59         mASServer = ConnectorPool.getConnection("account");
60         mRDServer = ConnectorPool.getConnection("rd");
61     }
62
63     class AccountReceiveHandler implements IResponseEventHandler {
64
65         private Device   mSrcDevice;
66         private IRequest mRequest;
67
68         public AccountReceiveHandler(IRequest request, Device srcDevice) {
69
70             mSrcDevice = srcDevice;
71             mRequest = request;
72         }
73
74         @Override
75         public void onResponseReceived(IResponse response)
76                 throws ClientException {
77             switch (response.getStatus()) {
78                 case CONTENT:
79                     HashMap<String, Object> payloadData = mCbor
80                             .parsePayloadFromCbor(response.getPayload(),
81                                     HashMap.class);
82
83                     if (payloadData == null) {
84                         mSrcDevice.sendResponse(MessageBuilder.createResponse(
85                                 mRequest, ResponseStatus.BAD_REQUEST));
86                         return;
87                     }
88
89                     ArrayList<String> devices = (ArrayList<String>) getResponseDeviceList(
90                             payloadData);
91
92                     if (mRequest.getUriQuery() != null
93                             && mRequest.getUriQueryMap()
94                                     .containsKey(Constants.REQ_DEVICE_ID)) {
95                         if (!devices.containsAll(mRequest.getUriQueryMap()
96                                 .get(Constants.REQ_DEVICE_ID))) {
97                             mSrcDevice.sendResponse(
98                                     MessageBuilder.createResponse(mRequest,
99                                             ResponseStatus.BAD_REQUEST));
100                         }
101                     } else {
102                         StringBuilder additionalQuery = makeAdditionalQuery(
103                                 devices);
104                         String uriQuery = (additionalQuery != null
105                                 ? additionalQuery.toString() : "")
106                                 + (mRequest.getUriQuery() != null
107                                         ? (";" + mRequest.getUriQuery()) : "");
108                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
109                                 uriQuery, null, null);
110                     }
111                     mRDServer.sendRequest(mRequest, mSrcDevice);
112                     break;
113                 default:
114                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
115                             mRequest, ResponseStatus.BAD_REQUEST));
116             }
117         }
118
119         private StringBuilder makeAdditionalQuery(
120                 ArrayList<String> deviceList) {
121
122             StringBuilder additionalQuery = new StringBuilder();
123
124             if (deviceList == null) {
125                 return null;
126             }
127
128             if (deviceList.isEmpty()) {
129                 return null;
130             }
131
132             int index = deviceList.size();
133             for (String device : deviceList) {
134                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
135                 if (--index > 0) {
136                     additionalQuery.append(";");
137                 }
138             }
139             return additionalQuery;
140         }
141
142         @SuppressWarnings("unchecked")
143         private List<String> getResponseDeviceList(
144                 HashMap<String, Object> payloadData) {
145             ArrayList<String> deviceList = new ArrayList<>();
146             if (payloadData.containsKey(Constants.REQ_DEVICE_LIST)) {
147                 deviceList = (ArrayList<String>) payloadData
148                         .get(Constants.REQ_DEVICE_LIST);
149             }
150             return deviceList;
151         }
152     }
153
154     @Override
155     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
156             throws ServerException {
157         if (request.getUriQuery() != null && request.getUriQueryMap()
158                 .containsKey(Constants.REQ_DEVICE_ID)) {
159
160             mRDServer.sendRequest(request, srcDevice);
161         } else {
162             StringBuffer additionalQuery = new StringBuffer();
163             additionalQuery
164                     .append(Constants.USER_ID + "=" + srcDevice.getUserId());
165             additionalQuery.append(";");
166             additionalQuery.append(
167                     Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId());
168
169             String uriQuery = additionalQuery.toString()
170                     + (request.getUriQuery() != null
171                             ? (";" + request.getUriQuery()) : "");
172             StringBuffer uriPath = new StringBuffer();
173             uriPath.append(Constants.PREFIX_OIC + "/");
174             uriPath.append(Constants.ACL_URI + "/");
175             uriPath.append(Constants.GROUP_URI + "/");
176             uriPath.append(srcDevice.getUserId());
177
178             IRequest requestToAS = MessageBuilder.createRequest(
179                     RequestMethod.GET, uriPath.toString(), uriQuery);
180
181             mASServer.sendRequest(requestToAS,
182                     new AccountReceiveHandler(request, srcDevice));
183         }
184     }
185 }