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;
20 import org.eclipse.jetty.client.ContentExchange;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.HttpExchange;
23 import org.eclipse.jetty.io.ByteArrayBuffer;
24 import org.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.eclipse.jetty.util.thread.QueuedThreadPool;
26 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
27 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
28 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.HttpBaseRest;
29 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestHttpContentExchange;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulAsyncCallback;
31 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulOptions;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
33 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.ws.rs.core.Request;
39 import javax.ws.rs.core.UriBuilder;
40 import java.io.IOException;
41 import java.io.UnsupportedEncodingException;
42 import java.net.URLEncoder;
45 public class HttpsRest extends HttpBaseRest {
47 private static final Logger LOG = LoggerFactory.getLogger(HttpsRest.class);
49 public void initHttpsRest() {
50 SslContextFactory sslContextFactory = new SslContextFactory();
51 client = new HttpClient(sslContextFactory);
52 client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
53 client.setMaxConnectionsPerAddress(200); // max 200 concurrent connections to every address
54 client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
55 client.setTimeout(30000); // 30 seconds timeout; if no server reply, the request expires
58 } catch (Exception e) {
64 public RestfulResponse get(String servicePath, RestfulParametes restParametes) throws ServiceException {
69 public RestfulResponse head(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
74 public RestfulResponse head(String servicePath, RestfulParametes restParametes) throws ServiceException {
79 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
84 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
89 public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
94 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
99 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
104 public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
109 public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
114 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
119 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
124 public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
129 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
134 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
139 public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
144 public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
149 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
154 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
159 public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option) throws ServiceException {
160 ContentExchange exchange = new ContentExchange(true);
161 exchange.setURL(servicePath);
162 exchange.setMethod("GET");
163 restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
166 client.send(exchange);
167 } catch (IOException e) {
171 int exchangeState = exchange.waitForDone();
172 if (exchangeState == HttpExchange.STATUS_COMPLETED) {
173 String res = exchange.getResponseContent();
176 RestfulResponse restfulResponse = new RestfulResponse();
177 restfulResponse.setResponseJson(exchange.getResponseContent());
178 restfulResponse.setStatus(exchange.getStatus());
179 return restfulResponse;
180 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
181 throw new ServiceException(
182 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
183 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
184 throw new ServiceException(
185 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
187 } catch (InterruptedException e) {
189 } catch (UnsupportedEncodingException e) {
196 public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
197 ContentExchange exchange = new ContentExchange(true);
198 exchange.setURL(servicePath);
199 exchange.setMethod("PUT");
200 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
202 restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
205 client.send(exchange);
206 } catch (IOException e) {
211 int exchangeState = exchange.waitForDone();
212 if (exchangeState == HttpExchange.STATUS_COMPLETED) {
213 String res = exchange.getResponseContent();
216 RestfulResponse restfulResponse = new RestfulResponse();
217 restfulResponse.setResponseJson(exchange.getResponseContent());
218 restfulResponse.setStatus(exchange.getStatus());
219 return restfulResponse;
220 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
221 throw new ServiceException(
222 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
223 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
224 throw new ServiceException(
225 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
227 } catch (InterruptedException e) {
229 } catch (UnsupportedEncodingException e) {
236 public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
237 ContentExchange exchange = new ContentExchange(true);
239 String encodeParams = RequestUtil.encodeParams(restParametes);
240 if (encodeParams.isEmpty()) {
241 exchange.setURL(servicePath);
243 exchange.setURL(servicePath + "?" + encodeParams);
245 exchange.setMethod("DELETE");
246 if (restParametes.getRawData() != null) {
247 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
250 restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
253 client.send(exchange);
254 } catch (IOException e) {
259 int exchangeState = exchange.waitForDone();
260 if (exchangeState == HttpExchange.STATUS_COMPLETED) {
261 String res = exchange.getResponseContent();
264 RestfulResponse restfulResponse = new RestfulResponse();
265 restfulResponse.setResponseJson(exchange.getResponseContent());
266 restfulResponse.setStatus(exchange.getStatus());
267 return restfulResponse;
268 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
269 throw new ServiceException(
270 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
271 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
272 throw new ServiceException(
273 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
275 } catch (InterruptedException e) {
277 } catch (UnsupportedEncodingException e) {