fixed CI unit test.
[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 (payloadData == null) {
76                         mSrcDevice.sendResponse(MessageBuilder.createResponse(
77                                 mRequest, ResponseStatus.BAD_REQUEST));
78                         return;
79                     }
80
81                     if (mRequest.getUriQuery() != null
82                             && mRequest.getUriQueryMap()
83                                     .containsKey(Constants.REQ_DEVICE_ID)) {
84                         if (!getResponseDeviceList(payloadData)
85                                 .containsAll(mRequest.getUriQueryMap()
86                                         .get(Constants.REQ_DEVICE_ID))) {
87                             mSrcDevice.sendResponse(
88                                     MessageBuilder.createResponse(mRequest,
89                                             ResponseStatus.BAD_REQUEST));
90                         }
91                     } else {
92                         String additionalQuery = makeAdditionalQuery(
93                                 payloadData, mSrcDevice.getDeviceId());
94                         if (additionalQuery == null) {
95                             mSrcDevice.sendResponse(
96                                     MessageBuilder.createResponse(mRequest,
97                                             ResponseStatus.BAD_REQUEST));
98                             return;
99                         }
100                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
101                                 additionalQuery
102                                         + (mRequest.getUriQuery() != null
103                                                 ? ";" + mRequest.getUriQuery()
104                                                 : ""),
105                                 null, null);
106                     }
107
108                     mRDServer.sendRequest(mRequest, mSrcDevice);
109                     break;
110                 default:
111                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
112                             mRequest, ResponseStatus.BAD_REQUEST));
113             }
114         }
115
116         private String makeAdditionalQuery(HashMap<String, Object> payloadData,
117                 String did) {
118
119             StringBuilder additionalQuery = new StringBuilder();
120
121             List<String> deviceList = getResponseDeviceList(payloadData);
122
123             if (deviceList.isEmpty()) {
124                 return null;
125             }
126
127             int index = deviceList.size();
128             for (String device : deviceList) {
129                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
130                 if (--index > 0) {
131                     additionalQuery.append(";");
132                 }
133             }
134             return additionalQuery.toString();
135         }
136
137         @SuppressWarnings("unchecked")
138         private List<String> getResponseDeviceList(
139                 HashMap<String, Object> payloadData) {
140             List<String> deviceList = (List<String>) payloadData
141                     .get(Constants.REQ_DEVICE_LIST);
142
143             return deviceList;
144         }
145     }
146
147     @Override
148     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
149             throws ServerException {
150         StringBuffer uriQuery = new StringBuffer();
151         uriQuery.append(Constants.REQ_MEMBER_ID + "=" + srcDevice.getUserId());
152
153         StringBuffer uriPath = new StringBuffer();
154         uriPath.append(Constants.PREFIX_OIC + "/");
155         uriPath.append(Constants.ACL_URI + "/");
156         uriPath.append(Constants.GROUP_URI + "/");
157         uriPath.append(srcDevice.getUserId());
158
159         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
160                 uriPath.toString(), uriQuery.toString());
161
162         mASServer.sendRequest(requestToAS,
163                 new AccountReceiveHandler(request, srcDevice));
164     }
165 }