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