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.http.HttpMethod;
20 import org.eclipse.jetty.util.thread.QueuedThreadPool;
21 import org.slf4j.Logger;
22 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);
39 * Initializing Rest options.<br/>
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.setMaxConnectionsPerDestination(iValue);
55 iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
57 client.setExecutor(new QueuedThreadPool(iValue));
58 iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
59 client.setConnectTimeout(iValue);
61 iValue = option.getRestTimeout();
62 defaultTimeout = iValue;
63 client.setIdleTimeout(iValue);
64 iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
65 client.setIdleTimeout(iValue);
67 defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME);
68 defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME);
69 } catch (final Exception e) {
70 LOG.error("start httpclient error", e);
72 throw new ServiceException("http client init failed.");
77 public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
78 return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, null);
82 public RestfulResponse get(final String servicePath, final RestfulParametes restParametes,
83 final RestfulOptions option) throws ServiceException {
84 return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, null);
88 public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
89 throws ServiceException {
90 return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, null, null);
94 public RestfulResponse head(final String servicePath, final RestfulParametes restParametes,
95 final RestfulOptions option) throws ServiceException {
96 return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, option, null);
100 public void asyncGet(final String servicePath, final RestfulParametes restParametes,
101 final RestfulAsyncCallback callback) throws ServiceException {
102 if (callback == null) {
103 this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
105 this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, callback);
110 public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
111 final RestfulAsyncCallback callback) throws ServiceException {
112 if (callback == null) {
113 this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
115 this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, callback);
120 public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
121 return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, null);
125 public RestfulResponse put(final String servicePath, final RestfulParametes restParametes,
126 final RestfulOptions option) throws ServiceException {
127 return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, null);
131 public void asyncPut(final String servicePath, final RestfulParametes restParametes,
132 final RestfulAsyncCallback callback) throws ServiceException {
133 if (callback == null) {
134 this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
136 this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, callback);
141 public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
142 final RestfulAsyncCallback callback) throws ServiceException {
143 if (callback == null) {
144 this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
146 this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, callback);
151 public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
152 throws ServiceException {
153 return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, null);
157 public RestfulResponse post(final String servicePath, final RestfulParametes restParametes,
158 final RestfulOptions option) throws ServiceException {
159 return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, null);
163 public void asyncPost(final String servicePath, final RestfulParametes restParametes,
164 final RestfulAsyncCallback callback) throws ServiceException {
165 if (callback == null) {
166 this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
168 this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, callback);
173 public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
174 final RestfulAsyncCallback callback) throws ServiceException {
175 if (callback == null) {
176 this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
178 this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, callback);
183 public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
184 throws ServiceException {
185 return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, null);
189 public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes,
190 final RestfulOptions option) throws ServiceException {
191 return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, null);
195 public void asyncDelete(final String servicePath, final RestfulParametes restParametes,
196 final RestfulAsyncCallback callback) throws ServiceException {
197 if (callback == null) {
198 this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
200 this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, callback);
205 public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
206 final RestfulAsyncCallback callback) throws ServiceException {
207 if (callback == null) {
208 this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
210 this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, callback);
215 public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
216 throws ServiceException {
217 return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
221 public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes,
222 final RestfulOptions option) throws ServiceException {
223 return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null);
227 public void asyncPatch(final String servicePath, final RestfulParametes restParametes,
228 final RestfulAsyncCallback callback) throws ServiceException {
229 if (callback == null) {
230 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback());
232 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
237 public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
238 final RestfulAsyncCallback callback) throws ServiceException {
239 if (callback == null) {
240 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback());
242 this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);