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