Implement reconnection and ping-pong between cloud services. Make
[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 /**
41  *
42  * This class provides a set of APIs to send requests about account to account
43  *
44  */
45
46 public class Account extends Resource {
47
48     public Account() {
49         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
50     }
51
52     class RDReceiveHandler implements IResponseEventHandler {
53
54         private Device    mSrcDevice;
55         private IResponse mResponse;
56         private IRequest  mRequest;
57
58         public RDReceiveHandler(IRequest request, IResponse response,
59                 Device srcDevice) {
60             mSrcDevice = srcDevice;
61             mRequest = request;
62             mResponse = response;
63         }
64
65         @Override
66         public void onResponseReceived(IResponse response)
67                 throws ClientException {
68             switch (response.getStatus()) {
69                 case DELETED:
70                     mSrcDevice.sendResponse(mResponse);
71                     break;
72                 default:
73                     mSrcDevice.sendResponse(MessageBuilder.createResponse(
74                             mRequest, ResponseStatus.BAD_REQUEST));
75             }
76         }
77     }
78
79     class AccountReceiveHandler implements IResponseEventHandler {
80
81         private Device   mSrcDevice;
82         private IRequest mRequest;
83
84         public AccountReceiveHandler(IRequest request, Device srcDevice) {
85             mSrcDevice = srcDevice;
86             mRequest = request;
87         }
88
89         @Override
90         public void onResponseReceived(IResponse response)
91                 throws ClientException {
92             switch (response.getStatus()) {
93                 case DELETED:
94                     StringBuffer uriPath = new StringBuffer();
95                     uriPath.append(Constants.PREFIX_OIC + "/");
96                     uriPath.append(Constants.RD_URI);
97                     ConnectorPool.getConnection("rd").sendRequest(
98                             MessageBuilder.createRequest(RequestMethod.DELETE,
99                                     uriPath.toString(), mRequest.getUriQuery()),
100                             new RDReceiveHandler(mRequest, response,
101                                     mSrcDevice));
102                     break;
103                 default:
104                     mSrcDevice.sendResponse(response);
105             }
106         }
107     }
108
109     @Override
110     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
111             throws ServerException {
112         switch (request.getMethod()) {
113             case GET:
114                 if (request.getUriQuery() == null) {
115                     StringBuffer additionalQuery = new StringBuffer();
116                     additionalQuery.append(
117                             Constants.USER_ID + "=" + srcDevice.getUserId());
118                     String uriQuery = additionalQuery.toString()
119                             + (request.getUriQuery() != null
120                                     ? (";" + request.getUriQuery()) : "");
121                     request = MessageBuilder.modifyRequest(request, null,
122                             uriQuery, null, null);
123                 }
124                 break;
125             case DELETE:
126                 StringBuffer additionalQuery = new StringBuffer();
127                 additionalQuery.append(
128                         Constants.USER_ID + "=" + srcDevice.getUserId());
129                 String uriQuery = additionalQuery.toString()
130                         + (request.getUriQuery() != null
131                                 ? (";" + request.getUriQuery()) : "");
132                 request = MessageBuilder.modifyRequest(request, null, uriQuery,
133                         null, null);
134                 break;
135             default:
136                 break;
137         }
138         ConnectorPool.getConnection("account").sendRequest(request,
139                 new AccountReceiveHandler(request, srcDevice));
140     }
141 }