Implement reconnection and ping-pong between cloud services. Make
[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.resource.Resource;
40 import org.iotivity.cloud.ciserver.Constants;
41 import org.iotivity.cloud.util.Cbor;
42
43 /**
44  *
45  * This class provides a set of APIs to send requests about find resource to
46  * resource directory
47  *
48  */
49
50 public class ResourceFind extends Resource {
51     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
52
53     public ResourceFind() {
54         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
55     }
56
57     class AccountReceiveHandler implements IResponseEventHandler {
58
59         private Device   mSrcDevice;
60         private IRequest mRequest;
61
62         public AccountReceiveHandler(IRequest request, Device srcDevice) {
63
64             mSrcDevice = srcDevice;
65             mRequest = request;
66         }
67
68         @Override
69         public void onResponseReceived(IResponse response)
70                 throws ClientException {
71             switch (response.getStatus()) {
72                 case CONTENT:
73                     HashMap<String, Object> payloadData = mCbor
74                             .parsePayloadFromCbor(response.getPayload(),
75                                     HashMap.class);
76
77                     ArrayList<String> devices = (ArrayList<String>) getResponseDeviceList(
78                             payloadData);
79
80                     StringBuilder additionalQuery = makeAdditionalQuery(
81                             devices);
82                     String uriQuery = (additionalQuery != null
83                             ? additionalQuery.toString() : "")
84                             + (mRequest.getUriQuery() != null
85                                     ? (";" + mRequest.getUriQuery()) : "");
86                     mRequest = MessageBuilder.modifyRequest(mRequest, null,
87                             uriQuery, null, null);
88
89                     ConnectorPool.getConnection("rd").sendRequest(mRequest, mSrcDevice);
90                     break;
91
92                 default:
93                     mSrcDevice.sendResponse(response);
94             }
95         }
96
97         private StringBuilder makeAdditionalQuery(
98                 ArrayList<String> deviceList) {
99
100             StringBuilder additionalQuery = new StringBuilder();
101
102             if (deviceList == null) {
103                 return null;
104             }
105
106             if (deviceList.isEmpty()) {
107                 return null;
108             }
109
110             int index = deviceList.size();
111             for (String device : deviceList) {
112                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
113                 if (--index > 0) {
114                     additionalQuery.append(";");
115                 }
116             }
117             return additionalQuery;
118         }
119
120         @SuppressWarnings("unchecked")
121         private List<String> getResponseDeviceList(
122                 HashMap<String, Object> payloadData) {
123             ArrayList<String> deviceList = new ArrayList<>();
124             if (payloadData.containsKey(Constants.REQ_DEVICE_LIST)) {
125                 deviceList = (ArrayList<String>) payloadData
126                         .get(Constants.REQ_DEVICE_LIST);
127             }
128             return deviceList;
129         }
130     }
131
132     @Override
133     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
134             throws ServerException {
135         if (request.getUriQuery() != null && request.getUriQueryMap()
136                 .containsKey(Constants.REQ_DEVICE_ID)) {
137
138             ConnectorPool.getConnection("rd").sendRequest(request, srcDevice);
139
140         } else {
141             StringBuffer additionalQuery = new StringBuffer();
142             additionalQuery
143                     .append(Constants.USER_ID + "=" + srcDevice.getUserId());
144             additionalQuery.append(";");
145             additionalQuery.append(
146                     Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId());
147
148             String uriQuery = additionalQuery.toString()
149                     + (request.getUriQuery() != null
150                             ? (";" + request.getUriQuery()) : "");
151             StringBuffer uriPath = new StringBuffer();
152             uriPath.append(Constants.PREFIX_OIC + "/");
153             uriPath.append(Constants.ACL_URI + "/");
154             uriPath.append(Constants.GROUP_URI + "/");
155             uriPath.append(srcDevice.getUserId());
156
157             IRequest requestToAS = MessageBuilder.createRequest(
158                     RequestMethod.GET, uriPath.toString(), uriQuery);
159
160             ConnectorPool.getConnection("account").sendRequest(requestToAS,
161                     new AccountReceiveHandler(request, srcDevice));
162         }
163     }
164 }