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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
31 * @version Aug 9, 2016
33 public class HttpRest extends HttpBaseRest {
35 private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class);
38 * Initializing Rest options.<br/>
40 * @param options: rest options.
41 * @throws ServiceException
44 public void initHttpRest(final RestfulOptions option) throws ServiceException {
47 throw new ServiceException("option is null.");
52 iValue = option.getIntOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME);
53 // max 200 concurrent,connections to every address
54 client.setMaxConnectionsPerAddress(iValue);
56 iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
58 client.setThreadPool(new QueuedThreadPool(iValue));
59 iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
60 client.setConnectTimeout(iValue);
61 iValue = option.getRestTimeout();
62 defaultTimeout = iValue;
63 client.setTimeout(iValue);
65 iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
66 client.setIdleTimeout(iValue);
67 iValue = option.getIntOption(RestfulClientConst.MAX_RESPONSE_HEADER_SIZE);
68 client.setResponseHeaderSize(iValue);
69 iValue = option.getIntOption(RestfulClientConst.MAX_REQUEST_HEADER_SIZE);
70 client.setRequestHeaderSize(iValue);
71 // HttpClient.CONNECTOR_SOCKET
72 client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
74 defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME);
75 defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME);
76 } catch(final Exception e) {
77 LOG.error("start httpclient error", e);
79 throw new ServiceException("http client init failed.");
84 public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
85 return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, null);
89 public RestfulResponse get(final String servicePath, final RestfulParametes restParametes,
90 final RestfulOptions option) throws ServiceException {
91 return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, null);
95 public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
96 throws ServiceException {
97 return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, null, null);
101 public RestfulResponse head(final String servicePath, final RestfulParametes restParametes,
102 final RestfulOptions option) throws ServiceException {
103 return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, option, null);
107 public void asyncGet(final String servicePath, final RestfulParametes restParametes,
108 final RestfulAsyncCallback callback) throws ServiceException {
109 if(callback == null) {
110 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, new DefaultAsyncCallback());
112 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, callback);
117 public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
118 final RestfulAsyncCallback callback) throws ServiceException {
119 if(callback == null) {
120 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, new DefaultAsyncCallback());
122 this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, callback);
127 public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
128 return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, null);
132 public RestfulResponse put(final String servicePath, final RestfulParametes restParametes,
133 final RestfulOptions option) throws ServiceException {
134 return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, null);
138 public void asyncPut(final String servicePath, final RestfulParametes restParametes,
139 final RestfulAsyncCallback callback) throws ServiceException {
140 if(callback == null) {
141 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, new DefaultAsyncCallback());
143 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, callback);
148 public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
149 final RestfulAsyncCallback callback) throws ServiceException {
150 if(callback == null) {
151 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, new DefaultAsyncCallback());
153 this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, callback);
158 public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
159 throws ServiceException {
160 return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, null);
164 public RestfulResponse post(final String servicePath, final RestfulParametes restParametes,
165 final RestfulOptions option) throws ServiceException {
166 return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, null);
170 public void asyncPost(final String servicePath, final RestfulParametes restParametes,
171 final RestfulAsyncCallback callback) throws ServiceException {
172 if(callback == null) {
173 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, new DefaultAsyncCallback());
175 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, callback);
180 public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
181 final RestfulAsyncCallback callback) throws ServiceException {
182 if(callback == null) {
183 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, new DefaultAsyncCallback());
185 this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, callback);
190 public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
191 throws ServiceException {
192 return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, null);
196 public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes,
197 final RestfulOptions option) throws ServiceException {
198 return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, null);
202 public void asyncDelete(final String servicePath, final RestfulParametes restParametes,
203 final RestfulAsyncCallback callback) throws ServiceException {
204 if(callback == null) {
205 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, new DefaultAsyncCallback());
207 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, callback);
212 public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
213 final RestfulAsyncCallback callback) throws ServiceException {
214 if(callback == null) {
215 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, new DefaultAsyncCallback());
217 this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, callback);
222 public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
223 throws ServiceException {
224 return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
228 public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes,
229 final RestfulOptions option) throws ServiceException {
230 return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null);
234 public void asyncPatch(final String servicePath, final RestfulParametes restParametes,
235 final RestfulAsyncCallback callback) throws ServiceException {
236 if(callback == null) {
237 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback());
239 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
244 public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
245 final RestfulAsyncCallback callback) throws ServiceException {
246 if(callback == null) {
247 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback());
249 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);