2 * Copyright 2017 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.resmanagement.common.util.restclient;
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
22 import org.eclipse.jetty.client.ContentExchange;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.client.HttpExchange;
25 import org.eclipse.jetty.io.ByteArrayBuffer;
26 import org.eclipse.jetty.util.ssl.SslContextFactory;
27 import org.eclipse.jetty.util.thread.QueuedThreadPool;
28 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class HttpsRest extends HttpBaseRest {
34 private static final Logger LOG = LoggerFactory.getLogger(HttpsRest.class);
36 public void initHttpsRest() {
37 SslContextFactory sslContextFactory = new SslContextFactory();
38 client = new HttpClient(sslContextFactory);
39 client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
40 client.setMaxConnectionsPerAddress(200); // max 200 concurrent connections to every address
41 client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
42 client.setTimeout(30000); // 30 seconds timeout; if no server reply, the request expires
45 } catch(Exception e) {
46 LOG.error("Exception", e);
51 public RestfulResponse get(String servicePath, RestfulParametes restParametes) throws ServiceException {
56 public RestfulResponse head(String servicePath, RestfulParametes restParametes, RestfulOptions options)
57 throws ServiceException {
62 public RestfulResponse head(String servicePath, RestfulParametes restParametes) throws ServiceException {
67 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
68 throws ServiceException {
73 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options,
74 RestfulAsyncCallback callback) throws ServiceException {
79 public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
84 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
85 throws ServiceException {
90 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options,
91 RestfulAsyncCallback callback) throws ServiceException {
96 public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
101 public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options)
102 throws ServiceException {
107 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
108 throws ServiceException {
113 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options,
114 RestfulAsyncCallback callback) throws ServiceException {
119 public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
124 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
125 throws ServiceException {
130 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options,
131 RestfulAsyncCallback callback) throws ServiceException {
136 public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
141 public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options)
142 throws ServiceException {
147 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
148 throws ServiceException {
153 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options,
154 RestfulAsyncCallback callback) throws ServiceException {
159 public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option)
160 throws ServiceException {
161 ContentExchange exchange = new ContentExchange(true);
162 exchange.setURL(servicePath);
163 exchange.setMethod("GET");
164 restParametes.getHeaderMap().entrySet().stream()
165 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
168 client.send(exchange);
169 } catch(IOException e) {
170 LOG.error("IOException", e);
173 int exchangeState = exchange.waitForDone();
174 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
175 String res = exchange.getResponseContent();
178 RestfulResponse restfulResponse = new RestfulResponse();
179 restfulResponse.setResponseJson(exchange.getResponseContent());
180 restfulResponse.setStatus(exchange.getResponseStatus());
181 return restfulResponse;
182 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
183 throw new ServiceException(
184 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
185 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
186 throw new ServiceException(
187 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
189 } catch(InterruptedException e) {
190 LOG.error("InterruptedException", e);
191 } catch(UnsupportedEncodingException e) {
192 LOG.error("UnsupportedEncodingException", e);
198 public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
199 throws ServiceException {
200 ContentExchange exchange = new ContentExchange(true);
201 exchange.setURL(servicePath);
202 exchange.setMethod("PUT");
203 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
205 restParametes.getHeaderMap().entrySet().stream()
206 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
209 client.send(exchange);
210 } catch(IOException e) {
211 LOG.error("IOException", e);
215 int exchangeState = exchange.waitForDone();
216 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
217 String res = exchange.getResponseContent();
220 RestfulResponse restfulResponse = new RestfulResponse();
221 restfulResponse.setResponseJson(exchange.getResponseContent());
222 restfulResponse.setStatus(exchange.getResponseStatus());
223 return restfulResponse;
224 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
225 throw new ServiceException(
226 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
227 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
228 throw new ServiceException(
229 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
231 } catch(InterruptedException e) {
232 LOG.error("InterruptedException", e);
233 // Restore interrupted state...
234 Thread.currentThread().interrupt();
235 } catch(UnsupportedEncodingException e) {
236 LOG.error("UnsupportedEncodingException", e);
242 public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
243 throws ServiceException {
244 ContentExchange exchange = new ContentExchange(true);
246 String encodeParams = RequestUtil.encodeParams(restParametes);
247 if(encodeParams.isEmpty()) {
248 exchange.setURL(servicePath);
250 exchange.setURL(servicePath + "?" + encodeParams);
252 exchange.setMethod("DELETE");
253 if(restParametes.getRawData() != null) {
254 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
257 restParametes.getHeaderMap().entrySet().stream()
258 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
261 client.send(exchange);
262 } catch(IOException e) {
263 LOG.error("IOException", e);
267 int exchangeState = exchange.waitForDone();
268 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
269 String res = exchange.getResponseContent();
272 RestfulResponse restfulResponse = new RestfulResponse();
273 restfulResponse.setResponseJson(exchange.getResponseContent());
274 restfulResponse.setStatus(exchange.getResponseStatus());
275 return restfulResponse;
276 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
277 throw new ServiceException(
278 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
279 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
280 throw new ServiceException(
281 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
283 } catch(InterruptedException e) {
284 LOG.error("InterruptedException", e);
285 // Restore interrupted state...
286 Thread.currentThread().interrupt();
287 } catch(UnsupportedEncodingException e) {
288 LOG.error("InterruptedException", e);