864c755a08204798b7b1e7b0b0aec8cd44cab54c
[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.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27
28 import org.iotivity.cloud.base.connector.ConnectorPool;
29 import org.iotivity.cloud.base.device.Device;
30 import org.iotivity.cloud.base.device.IRequestChannel;
31 import org.iotivity.cloud.base.device.IResponseEventHandler;
32 import org.iotivity.cloud.base.exception.ClientException;
33 import org.iotivity.cloud.base.exception.ServerException;
34 import org.iotivity.cloud.base.protocols.IRequest;
35 import org.iotivity.cloud.base.protocols.IResponse;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
38 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
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 device presence to
46  * resource directory
47  *
48  */
49
50 public class DevicePresence extends Resource {
51     IRequestChannel                       mASServer = null;
52     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
53
54     public DevicePresence() {
55         super(Arrays.asList(Constants.PREFIX_OIC,
56                 Constants.DEVICE_PRESENCE_URI));
57
58         mASServer = ConnectorPool.getConnection("account");
59     }
60
61     class AccountReceiveHandler implements IResponseEventHandler {
62
63         IRequestChannel  mRDServer = null;
64         private Device   mSrcDevice;
65         private IRequest mRequest;
66
67         public AccountReceiveHandler(IRequest request, Device srcDevice) {
68             mRDServer = ConnectorPool.getConnection("rd");
69             mSrcDevice = srcDevice;
70             mRequest = request;
71         }
72
73         @Override
74         public void onResponseReceived(IResponse response)
75                 throws ClientException {
76             switch (response.getStatus()) {
77                 case CONTENT:
78                     HashMap<String, Object> payloadData = mCbor
79                             .parsePayloadFromCbor(response.getPayload(),
80                                     HashMap.class);
81
82                     if (payloadData == null) {
83                         mSrcDevice.sendResponse(MessageBuilder.createResponse(
84                                 mRequest, ResponseStatus.BAD_REQUEST));
85                         return;
86                     }
87
88                     if (mRequest.getUriQuery() != null
89                             && mRequest.getUriQueryMap()
90                                     .containsKey(Constants.REQ_DEVICE_ID)) {
91                         if (!getResponseDeviceList(payloadData)
92                                 .containsAll(mRequest.getUriQueryMap()
93                                         .get(Constants.REQ_DEVICE_ID))) {
94                             mSrcDevice.sendResponse(
95                                     MessageBuilder.createResponse(mRequest,
96                                             ResponseStatus.BAD_REQUEST));
97                         }
98                     } else {
99                         String additionalQuery = makeAdditionalQuery(
100                                 payloadData, mSrcDevice.getDeviceId());
101                         if (additionalQuery == null) {
102                             mSrcDevice.sendResponse(
103                                     MessageBuilder.createResponse(mRequest,
104                                             ResponseStatus.BAD_REQUEST));
105                             return;
106                         }
107                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
108                                 additionalQuery
109                                         + (mRequest.getUriQuery() != null
110                                                 ? ";" + mRequest.getUriQuery()
111                                                 : ""),
112                                 null, null);
113                     }
114
115                     mRDServer.sendRequest(mRequest, mSrcDevice);
116                     break;
117                 default:
118                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
119                             mRequest, ResponseStatus.BAD_REQUEST));
120             }
121         }
122
123         private String makeAdditionalQuery(HashMap<String, Object> payloadData,
124                 String did) {
125
126             StringBuilder additionalQuery = new StringBuilder();
127
128             List<String> deviceList = getResponseDeviceList(payloadData);
129
130             if (deviceList.isEmpty()) {
131                 return null;
132             }
133
134             int index = deviceList.size();
135             for (String device : deviceList) {
136                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
137                 if (--index > 0) {
138                     additionalQuery.append(";");
139                 }
140             }
141             return additionalQuery.toString();
142         }
143
144         @SuppressWarnings("unchecked")
145         private List<String> getResponseDeviceList(
146                 HashMap<String, Object> payloadData) {
147             List<String> deviceList = (List<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         StringBuffer uriQuery = new StringBuffer();
158         uriQuery.append(Constants.REQ_MEMBER_ID + "=" + srcDevice.getUserId());
159
160         StringBuffer uriPath = new StringBuffer();
161         uriPath.append(Constants.PREFIX_OIC + "/");
162         uriPath.append(Constants.ACL_URI + "/");
163         uriPath.append(Constants.GROUP_URI + "/");
164         uriPath.append(srcDevice.getUserId());
165
166         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
167                 uriPath.toString(), uriQuery.toString());
168
169         mASServer.sendRequest(requestToAS,
170                 new AccountReceiveHandler(request, srcDevice));
171     }
172 }