2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.baseservice.roa.util.restclient;
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;
32 * @version Aug 9, 2016
34 public class HttpRest extends HttpBaseRest {
36 private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class);
39 * Initializing Rest options.<br/>
41 * @param options: rest options.
42 * @throws ServiceException
45 public void initHttpRest(final RestfulOptions option) throws ServiceException {
48 throw new ServiceException("option is null.");
53 iValue = option.getIntOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME);
54 // max 200 concurrent,connections to every address
55 client.setMaxConnectionsPerAddress(iValue);
57 iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
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);
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);
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);
80 throw new ServiceException("http client init failed.");
85 public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
86 return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, null);
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);
96 public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
97 throws ServiceException {
98 return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, null, null);
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);
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());
113 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, callback);
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());
123 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, callback);
128 public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
129 return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, null);
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);
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());
144 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, callback);
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());
154 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, callback);
159 public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
160 throws ServiceException {
161 return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, null);
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);
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());
176 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, callback);
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());
186 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, callback);
191 public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
192 throws ServiceException {
193 return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, null);
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);
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());
208 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, callback);
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());
218 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, callback);
223 public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
224 throws ServiceException {
225 return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
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);
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());
240 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
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());
250 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);