Remove classpath files and update ignore
[platform/upstream/iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / ResourceFind.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;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27
28 import org.iotivity.cloud.base.connector.ConnectorPool;
29 import org.iotivity.cloud.base.device.CoapDevice;
30 import org.iotivity.cloud.base.device.Device;
31 import org.iotivity.cloud.base.device.IRequestChannel;
32 import org.iotivity.cloud.base.device.IResponseEventHandler;
33 import org.iotivity.cloud.base.exception.ClientException;
34 import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
35 import org.iotivity.cloud.base.exception.ServerException;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.IResponse;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
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 ResourceFind extends Resource {
45     IRequestChannel mASServer = null;
46
47     public ResourceFind() {
48         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_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 VALID:
71                     mRDServer.sendRequest(mRequest, mSrcDevice);
72                     break;
73
74                 case CONTENT:
75                     StringBuilder additionalQuery = new StringBuilder();
76
77                     Cbor<HashMap<String, ArrayList<String>>> responsePayload = new Cbor<>();
78
79                     ArrayList<String> deviceList = responsePayload
80                             .parsePayloadFromCbor(response.getPayload(),
81                                     HashMap.class)
82                             .get("devices");
83
84                     int index = deviceList.size();
85
86                     for (String deviceId : deviceList) {
87                         if (!deviceId.equals(mSrcDevice.getDeviceId())) {
88                             additionalQuery.append("di=");
89                             additionalQuery.append(deviceId);
90                             if (--index > 0) {
91                                 additionalQuery.append("&");
92                             }
93                         }
94                     }
95                     String uriQuery = (mRequest.getUriQuery() == null ? ""
96                             : mRequest.getUriQuery() + "&")
97                             + additionalQuery.toString();
98                     IRequest requestToAS = MessageBuilder.modifyRequest(
99                             mRequest, null, uriQuery, null, null);
100
101                     mRDServer.sendRequest(requestToAS, mSrcDevice);
102                     break;
103
104                 default:
105                     throw new BadResponseException(
106                             response.getStatus().toString()
107                                     + " response type is not supported");
108             }
109         }
110     }
111
112     @Override
113     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
114             throws ServerException {
115         // Token exchange is done by CoapClient
116         CoapDevice coapDevice = (CoapDevice) srcDevice;
117         StringBuffer uriQuery = new StringBuffer();
118         uriQuery.append(Constants.SEARCH_ACCESS_TOKEN + "=");
119         uriQuery.append(coapDevice.getAccessToken());
120
121         if (request.getUriQueryMap().get("di") != null) {
122             String di = request.getUriQueryMap().get("di").get(0);
123             if (di != null) {
124                 uriQuery.append("&");
125                 uriQuery.append("di" + "=");
126                 uriQuery.append(di);
127             }
128         }
129
130         StringBuffer uriPath = new StringBuffer();
131         uriPath.append(Constants.PREFIX_WELL_KNOWN + "/");
132         uriPath.append(Constants.PREFIX_OCF + "/");
133         uriPath.append(Constants.ACCOUNT_URI + "/");
134         uriPath.append(Constants.DEVICE_URI);
135
136         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
137                 uriPath.toString(), uriQuery.toString());
138
139         mASServer.sendRequest(requestToAS,
140                 new AccountReceiveHandler(request, srcDevice));
141     }
142 }