fixed CI unit test.
[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.ServerException;
33 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
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.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.base.resource.Resource;
41 import org.iotivity.cloud.ciserver.Constants;
42 import org.iotivity.cloud.util.Cbor;
43
44 public class ResourceDirectory extends Resource {
45     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
46     IRequestChannel                       mRDServer = null;
47     IRequestChannel                       mASServer = null;
48
49     public ResourceDirectory() {
50         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_URI));
51         mRDServer = ConnectorPool.getConnection("rd");
52         mASServer = ConnectorPool.getConnection("account");
53     }
54
55     class AccountReceiveHandler implements IResponseEventHandler {
56         private Device   mSrcDevice;
57         private IRequest mRequest;
58
59         public AccountReceiveHandler(IRequest request, Device srcDevice) {
60             mSrcDevice = srcDevice;
61             mRequest = request;
62         }
63
64         @Override
65         public void onResponseReceived(IResponse response)
66                 throws ClientException {
67
68             switch (response.getStatus()) {
69                 case CHANGED:
70                     mRDServer.sendRequest(mRequest, mSrcDevice);
71                     break;
72
73                 default:
74                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
75                             mRequest, ResponseStatus.BAD_REQUEST));
76             }
77         }
78     }
79
80     @Override
81     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
82             throws ServerException {
83         switch (request.getMethod()) {
84             case POST:
85                 HashMap<String, Object> payloadData = mCbor
86                         .parsePayloadFromCbor(request.getPayload(),
87                                 HashMap.class);
88
89                 StringBuffer uriPath = new StringBuffer();
90                 uriPath.append(Constants.PREFIX_OIC + "/");
91                 uriPath.append(Constants.ACL_URI + "/");
92                 uriPath.append(Constants.GROUP_URI + "/");
93                 uriPath.append(srcDevice.getUserId());
94
95                 String di = payloadData.get(Constants.REQ_DEVICE_ID).toString();
96
97                 HashMap<String, Object> requestPayload = new HashMap<>();
98
99                 requestPayload.put(Constants.REQ_DEVICE_LIST,
100                         Arrays.asList(di));
101                 IRequest requestToAS = MessageBuilder.createRequest(
102                         RequestMethod.POST, uriPath.toString(), null,
103                         ContentFormat.APPLICATION_CBOR,
104                         mCbor.encodingPayloadToCbor(requestPayload));
105
106                 mASServer.sendRequest(requestToAS,
107                         new AccountReceiveHandler(request, srcDevice));
108                 break;
109
110             case DELETE:
111                 mRDServer.sendRequest(request, srcDevice);
112                 break;
113             default:
114                 throw new BadRequestException(
115                         request.getMethod() + " request type is not support");
116         }
117     }
118 }