50a3538d87339cc45336317ea3957ced1004f46f
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / main / java / org / openo / baseservice / roa / util / restclient / HttpRest.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openo.baseservice.roa.util.restclient;
18
19 import org.eclipse.jetty.client.HttpClient;
20 import org.eclipse.jetty.http.HttpMethods;
21 import org.eclipse.jetty.util.thread.QueuedThreadPool;
22 import org.openo.baseservice.remoteservice.exception.ServiceException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * <br/>
28  * <p>
29  * </p>
30  * 
31  * @author
32  * @version SDNO 0.5 Aug 9, 2016
33  */
34 public class HttpRest extends HttpBaseRest {
35
36     private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class);
37
38     /**
39      * Initializing Rest options.<br/>
40      * 
41      * @param options: rest options.
42      * @throws ServiceException
43      * @since SDNO 0.5
44      */
45     public void initHttpRest(final RestfulOptions option) throws ServiceException {
46         if(option == null) {
47             client = null;
48             throw new ServiceException("option is null.");
49         }
50         createHttpClient();
51         try {
52             int iValue;
53             iValue = option.getIntOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME);
54             // max 200 concurrent,connections to every address
55             client.setMaxConnectionsPerAddress(iValue);
56
57             iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
58             // max threads
59             client.setThreadPool(new QueuedThreadPool(iValue));
60             iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
61             client.setConnectTimeout(iValue);
62             iValue = option.getRestTimeout();
63             defaultTimeout = iValue;
64             client.setTimeout(iValue);
65
66             iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
67             client.setIdleTimeout(iValue);
68             iValue = option.getIntOption(RestfulClientConst.MAX_RESPONSE_HEADER_SIZE);
69             client.setResponseHeaderSize(iValue);
70             iValue = option.getIntOption(RestfulClientConst.MAX_REQUEST_HEADER_SIZE);
71             client.setRequestHeaderSize(iValue);
72             // HttpClient.CONNECTOR_SOCKET
73             client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
74             client.start();
75             defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME);
76             defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME);
77         } catch(final Exception e) {
78             LOG.error("start httpclient error", e);
79             client = null;
80             throw new ServiceException("http client init failed.");
81         }
82     }
83
84     @Override
85     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
86         return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, null);
87     }
88
89     @Override
90     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes,
91             final RestfulOptions option) throws ServiceException {
92         return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, null);
93     }
94
95     @Override
96     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
97             throws ServiceException {
98         return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, null, null);
99     }
100
101     @Override
102     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes,
103             final RestfulOptions option) throws ServiceException {
104         return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, option, null);
105     }
106
107     @Override
108     public void asyncGet(final String servicePath, final RestfulParametes restParametes,
109             final RestfulAsyncCallback callback) throws ServiceException {
110         if(callback == null) {
111             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, new DefaultAsyncCallback());
112         } else {
113             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, callback);
114         }
115     }
116
117     @Override
118     public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
119             final RestfulAsyncCallback callback) throws ServiceException {
120         if(callback == null) {
121             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, new DefaultAsyncCallback());
122         } else {
123             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, callback);
124         }
125     }
126
127     @Override
128     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
129         return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, null);
130     }
131
132     @Override
133     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes,
134             final RestfulOptions option) throws ServiceException {
135         return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, null);
136     }
137
138     @Override
139     public void asyncPut(final String servicePath, final RestfulParametes restParametes,
140             final RestfulAsyncCallback callback) throws ServiceException {
141         if(callback == null) {
142             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, new DefaultAsyncCallback());
143         } else {
144             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, callback);
145         }
146     }
147
148     @Override
149     public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
150             final RestfulAsyncCallback callback) throws ServiceException {
151         if(callback == null) {
152             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, new DefaultAsyncCallback());
153         } else {
154             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, callback);
155         }
156     }
157
158     @Override
159     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
160             throws ServiceException {
161         return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, null);
162     }
163
164     @Override
165     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes,
166             final RestfulOptions option) throws ServiceException {
167         return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, null);
168     }
169
170     @Override
171     public void asyncPost(final String servicePath, final RestfulParametes restParametes,
172             final RestfulAsyncCallback callback) throws ServiceException {
173         if(callback == null) {
174             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, new DefaultAsyncCallback());
175         } else {
176             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, callback);
177         }
178     }
179
180     @Override
181     public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
182             final RestfulAsyncCallback callback) throws ServiceException {
183         if(callback == null) {
184             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, new DefaultAsyncCallback());
185         } else {
186             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, callback);
187         }
188     }
189
190     @Override
191     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
192             throws ServiceException {
193         return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, null);
194     }
195
196     @Override
197     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes,
198             final RestfulOptions option) throws ServiceException {
199         return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, null);
200     }
201
202     @Override
203     public void asyncDelete(final String servicePath, final RestfulParametes restParametes,
204             final RestfulAsyncCallback callback) throws ServiceException {
205         if(callback == null) {
206             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, new DefaultAsyncCallback());
207         } else {
208             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, callback);
209         }
210     }
211
212     @Override
213     public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
214             final RestfulAsyncCallback callback) throws ServiceException {
215         if(callback == null) {
216             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, new DefaultAsyncCallback());
217         } else {
218             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, callback);
219         }
220     }
221
222     @Override
223     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
224             throws ServiceException {
225         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
226     }
227
228     @Override
229     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes,
230             final RestfulOptions option) throws ServiceException {
231         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null);
232     }
233
234     @Override
235     public void asyncPatch(final String servicePath, final RestfulParametes restParametes,
236             final RestfulAsyncCallback callback) throws ServiceException {
237         if(callback == null) {
238             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback());
239         } else {
240             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
241         }
242     }
243
244     @Override
245     public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
246             final RestfulAsyncCallback callback) throws ServiceException {
247         if(callback == null) {
248             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback());
249         } else {
250             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);
251         }
252     }
253
254 }