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