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