fixed CI unit test.
[iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / account / Account.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.account;
23
24 import java.util.Arrays;
25
26 import org.iotivity.cloud.base.connector.ConnectorPool;
27 import org.iotivity.cloud.base.device.Device;
28 import org.iotivity.cloud.base.device.IRequestChannel;
29 import org.iotivity.cloud.base.device.IResponseEventHandler;
30 import org.iotivity.cloud.base.exception.ClientException;
31 import org.iotivity.cloud.base.exception.ServerException;
32 import org.iotivity.cloud.base.protocols.IRequest;
33 import org.iotivity.cloud.base.protocols.IResponse;
34 import org.iotivity.cloud.base.protocols.MessageBuilder;
35 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
36 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
37 import org.iotivity.cloud.base.resource.Resource;
38 import org.iotivity.cloud.ciserver.Constants;
39
40 public class Account extends Resource {
41     IRequestChannel mASServer = null;
42
43     public Account() {
44         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
45
46         mASServer = ConnectorPool.getConnection("account");
47     }
48
49     class AccountReceiveHandler implements IResponseEventHandler {
50
51         IRequestChannel  mRDServer = null;
52         private Device   mSrcDevice;
53         private IRequest mRequest;
54
55         public AccountReceiveHandler(IRequest request, Device srcDevice) {
56             mRDServer = ConnectorPool.getConnection("rd");
57             mSrcDevice = srcDevice;
58             mRequest = request;
59         }
60
61         @Override
62         public void onResponseReceived(IResponse response)
63                 throws ClientException {
64             switch (response.getStatus()) {
65                 case DELETED:
66                     StringBuffer uriPath = new StringBuffer();
67                     uriPath.append(Constants.PREFIX_OIC + "/");
68                     uriPath.append(Constants.RD_URI);
69                     mRDServer.sendRequest(
70                             MessageBuilder.createRequest(RequestMethod.POST,
71                                     uriPath.toString(), mRequest.getUriQuery()),
72                             mSrcDevice);
73                     break;
74                 default:
75                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
76                             mRequest, ResponseStatus.BAD_REQUEST));
77             }
78         }
79     }
80
81     @Override
82     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
83             throws ServerException {
84         if (request.getMethod().equals(RequestMethod.DELETE)) {
85             StringBuffer additionalQuery = new StringBuffer();
86             additionalQuery
87                     .append(Constants.USER_ID + "=" + srcDevice.getUserId());
88             String uriQuery = request.getUriQuery() + ";"
89                     + additionalQuery.toString();
90             request = MessageBuilder.modifyRequest(request, null, uriQuery,
91                     null, null);
92         }
93         mASServer.sendRequest(request,
94                 new AccountReceiveHandler(request, srcDevice));
95     }
96 }