[IOT-1604] Fix cloud routing code
[iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / rd / DevicePresence.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 device presence to
47  * resource directory
48  *
49  */
50
51 public class DevicePresence extends Resource {
52     IRequestChannel                       mASServer = null;
53     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
54
55     public DevicePresence() {
56         super(Arrays.asList(Constants.PREFIX_OIC,
57                 Constants.DEVICE_PRESENCE_URI));
58
59         mASServer = ConnectorPool.getConnection("account");
60     }
61
62     class AccountReceiveHandler implements IResponseEventHandler {
63
64         IRequestChannel  mRDServer = null;
65         private Device   mSrcDevice;
66         private IRequest mRequest;
67
68         public AccountReceiveHandler(IRequest request, Device srcDevice) {
69             mRDServer = ConnectorPool.getConnection("rd");
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                     ArrayList<String> devices = (ArrayList<String>) getResponseDeviceList(
84                             payloadData);
85
86                     if (mRequest.getUriQuery() != null
87                             && mRequest.getUriQueryMap()
88                                     .containsKey(Constants.REQ_DEVICE_ID)) {
89                         if (!devices.containsAll(mRequest.getUriQueryMap()
90                                 .get(Constants.REQ_DEVICE_ID))) {
91                             mSrcDevice.sendResponse(
92                                     MessageBuilder.createResponse(mRequest,
93                                             ResponseStatus.BAD_REQUEST));
94                         }
95                     } else {
96                         String additionalQuery = makeAdditionalQuery(
97                                 payloadData, mSrcDevice.getDeviceId());
98
99                         String uriQuery = additionalQuery.toString()
100                                 + (mRequest.getUriQuery() != null
101                                         ? (";" + mRequest.getUriQuery()) : "");
102                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
103                                 uriQuery, null, null);
104                     }
105
106                     mRDServer.sendRequest(mRequest, mSrcDevice);
107                     break;
108
109                 default:
110                     mSrcDevice.sendResponse(response);
111             }
112         }
113
114         private String makeAdditionalQuery(HashMap<String, Object> payloadData,
115                 String did) {
116
117             StringBuilder additionalQuery = new StringBuilder();
118
119             List<String> deviceList = getResponseDeviceList(payloadData);
120
121             if (deviceList.isEmpty()) {
122                 return null;
123             }
124
125             int index = deviceList.size();
126             for (String device : deviceList) {
127                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
128                 if (--index > 0) {
129                     additionalQuery.append(";");
130                 }
131             }
132             return additionalQuery.toString();
133         }
134
135         @SuppressWarnings("unchecked")
136         private List<String> getResponseDeviceList(
137                 HashMap<String, Object> payloadData) {
138             List<String> deviceList = (List<String>) payloadData
139                     .get(Constants.REQ_DEVICE_LIST);
140
141             return deviceList;
142         }
143     }
144
145     @Override
146     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
147             throws ServerException {
148         StringBuffer additionalQuery = new StringBuffer();
149         additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId());
150         additionalQuery.append(";");
151         additionalQuery.append(
152                 Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId());
153         String uriQuery = additionalQuery.toString()
154                 + (request.getUriQuery() != null ? (";" + request.getUriQuery())
155                         : "");
156
157         StringBuffer uriPath = new StringBuffer();
158         uriPath.append(Constants.PREFIX_OIC + "/");
159         uriPath.append(Constants.ACL_URI + "/");
160         uriPath.append(Constants.GROUP_URI + "/");
161         uriPath.append(srcDevice.getUserId());
162
163         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
164                 uriPath.toString(), uriQuery);
165
166         mASServer.sendRequest(requestToAS,
167                 new AccountReceiveHandler(request, srcDevice));
168     }
169 }