2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.ciserver.resources.proxy.rd;
24 import java.util.Arrays;
25 import java.util.HashMap;
27 import org.iotivity.cloud.base.connector.ConnectorPool;
28 import org.iotivity.cloud.base.device.Device;
29 import org.iotivity.cloud.base.device.IRequestChannel;
30 import org.iotivity.cloud.base.device.IResponseEventHandler;
31 import org.iotivity.cloud.base.exception.ClientException;
32 import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
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.ContentFormat;
38 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
39 import org.iotivity.cloud.base.resource.Resource;
40 import org.iotivity.cloud.ciserver.Constants;
41 import org.iotivity.cloud.util.Cbor;
43 public class ResourceDirectory extends Resource {
44 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
45 IRequestChannel mASServer = null;
47 public ResourceDirectory() {
48 super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_URI));
50 mASServer = ConnectorPool.getConnection("account");
53 class AccountReceiveHandler implements IResponseEventHandler {
55 IRequestChannel mRDServer = null;
56 private Device mSrcDevice;
57 private IRequest mRequest;
59 public AccountReceiveHandler(IRequest request, Device srcDevice) {
60 mRDServer = ConnectorPool.getConnection("rd");
61 mSrcDevice = srcDevice;
66 public void onResponseReceived(IResponse response)
67 throws ClientException {
69 switch (response.getStatus()) {
72 mRDServer.sendRequest(mRequest, mSrcDevice);
76 throw new BadResponseException(
77 response.getStatus().toString()
78 + " response type is not supported");
84 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
85 throws ServerException {
87 StringBuffer uriPath = new StringBuffer();
88 uriPath.append(Constants.PREFIX_WELL_KNOWN + "/");
89 uriPath.append(Constants.PREFIX_OCF + "/");
90 uriPath.append(Constants.ACL_URI + "/");
91 uriPath.append(Constants.GROUP_URI + "/");
92 uriPath.append(srcDevice.getUserId());
94 HashMap<String, Object> payloadData = mCbor
95 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
97 String di = payloadData.get(Constants.REQ_DEVICE_ID).toString();
99 HashMap<String, Object> requestPayload = new HashMap<>();
101 requestPayload.put(Constants.REQ_DEVICE_LIST, Arrays.asList(di));
102 IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.POST,
103 uriPath.toString(), null, ContentFormat.APPLICATION_CBOR,
104 mCbor.encodingPayloadToCbor(requestPayload));
106 mASServer.sendRequest(requestToAS,
107 new AccountReceiveHandler(request, srcDevice));