fe86968f28f475e9515cd8bf3ea210b716a28409
[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                     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                         String additionalQuery = makeAdditionalQuery(
103                                 payloadData, mSrcDevice.getDeviceId());
104                         if (additionalQuery == null) {
105                             mSrcDevice.sendResponse(
106                                     MessageBuilder.createResponse(mRequest,
107                                             ResponseStatus.BAD_REQUEST));
108                             return;
109                         }
110                         String uriQuery = additionalQuery.toString()
111                                 + (mRequest.getUriQuery() != null
112                                         ? (";" + mRequest.getUriQuery()) : "");
113                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
114                                 uriQuery, null, null);
115                     }
116
117                     mRDServer.sendRequest(mRequest, mSrcDevice);
118                     break;
119                 default:
120                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
121                             mRequest, ResponseStatus.BAD_REQUEST));
122             }
123         }
124
125         private String makeAdditionalQuery(HashMap<String, Object> payloadData,
126                 String did) {
127
128             StringBuilder additionalQuery = new StringBuilder();
129
130             List<String> deviceList = getResponseDeviceList(payloadData);
131
132             if (deviceList.isEmpty()) {
133                 return null;
134             }
135
136             int index = deviceList.size();
137             for (String device : deviceList) {
138                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
139                 if (--index > 0) {
140                     additionalQuery.append(";");
141                 }
142             }
143             return additionalQuery.toString();
144         }
145
146         @SuppressWarnings("unchecked")
147         private List<String> getResponseDeviceList(
148                 HashMap<String, Object> payloadData) {
149             List<String> deviceList = (List<String>) payloadData
150                     .get(Constants.REQ_DEVICE_LIST);
151
152             return deviceList;
153         }
154     }
155
156     @Override
157     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
158             throws ServerException {
159         StringBuffer additionalQuery = new StringBuffer();
160         additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId());
161         additionalQuery.append(";");
162         additionalQuery.append(
163                 Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId());
164         String uriQuery = additionalQuery.toString()
165                 + (request.getUriQuery() != null ? (";" + request.getUriQuery())
166                         : "");
167
168         StringBuffer uriPath = new StringBuffer();
169         uriPath.append(Constants.PREFIX_OIC + "/");
170         uriPath.append(Constants.ACL_URI + "/");
171         uriPath.append(Constants.GROUP_URI + "/");
172         uriPath.append(srcDevice.getUserId());
173
174         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
175                 uriPath.toString(), uriQuery);
176
177         mASServer.sendRequest(requestToAS,
178                 new AccountReceiveHandler(request, srcDevice));
179     }
180 }