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 // Restore interrupted state...
192 Thread.currentThread().interrupt();
193 } catch(UnsupportedEncodingException e) {
194 LOG.error("UnsupportedEncodingException", e);
200 public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
201 throws ServiceException {
202 ContentExchange exchange = new ContentExchange(true);
203 exchange.setURL(servicePath);
204 exchange.setMethod("PUT");
205 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
207 restParametes.getHeaderMap().entrySet().stream()
208 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
211 client.send(exchange);
212 } catch(IOException e) {
213 LOG.error("IOException", e);
217 int exchangeState = exchange.waitForDone();
218 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
219 String res = exchange.getResponseContent();
222 RestfulResponse restfulResponse = new RestfulResponse();
223 restfulResponse.setResponseJson(exchange.getResponseContent());
224 restfulResponse.setStatus(exchange.getResponseStatus());
225 return restfulResponse;
226 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
227 throw new ServiceException(
228 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
229 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
230 throw new ServiceException(
231 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
233 } catch(InterruptedException e) {
234 LOG.error("InterruptedException", e);
235 // Restore interrupted state...
236 Thread.currentThread().interrupt();
237 } catch(UnsupportedEncodingException e) {
238 LOG.error("UnsupportedEncodingException", e);
244 public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
245 throws ServiceException {
246 ContentExchange exchange = new ContentExchange(true);
248 String encodeParams = RequestUtil.encodeParams(restParametes);
249 if(encodeParams.isEmpty()) {
250 exchange.setURL(servicePath);
252 exchange.setURL(servicePath + "?" + encodeParams);
254 exchange.setMethod("DELETE");
255 if(restParametes.getRawData() != null) {
256 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
259 restParametes.getHeaderMap().entrySet().stream()
260 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
263 client.send(exchange);
264 } catch(IOException e) {
265 LOG.error("IOException", e);
269 int exchangeState = exchange.waitForDone();
270 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
271 String res = exchange.getResponseContent();
274 RestfulResponse restfulResponse = new RestfulResponse();
275 restfulResponse.setResponseJson(exchange.getResponseContent());
276 restfulResponse.setStatus(exchange.getResponseStatus());
277 return restfulResponse;
278 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
279 throw new ServiceException(
280 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
281 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
282 throw new ServiceException(
283 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
285 } catch(InterruptedException e) {
286 LOG.error("InterruptedException", e);
287 // Restore interrupted state...
288 Thread.currentThread().interrupt();
289 } catch(UnsupportedEncodingException e) {
290 LOG.error("InterruptedException", e);