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