6b2f493790eb2c8c6579d5bdcd8ac3518a466370
[iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourceDirectory.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
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;
42
43 public class ResourceDirectory extends Resource {
44     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
45     IRequestChannel                       mASServer = null;
46
47     public ResourceDirectory() {
48         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_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
69             switch (response.getStatus()) {
70                 case CHANGED:
71
72                     mRDServer.sendRequest(mRequest, mSrcDevice);
73                     break;
74
75                 default:
76                     throw new BadResponseException(
77                             response.getStatus().toString()
78                                     + " response type is not supported");
79             }
80         }
81     }
82
83     @Override
84     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
85             throws ServerException {
86
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());
93
94         HashMap<String, Object> payloadData = mCbor
95                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
96
97         String di = payloadData.get(Constants.REQ_DEVICE_ID).toString();
98
99         HashMap<String, Object> requestPayload = new HashMap<>();
100
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));
105
106         mASServer.sendRequest(requestToAS,
107                 new AccountReceiveHandler(request, srcDevice));
108     }
109 }