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