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.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.eclipse.jetty.client.ContentExchange;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.client.HttpExchange;
27 import org.eclipse.jetty.io.ByteArrayBuffer;
28 import org.eclipse.jetty.util.ssl.SslContextFactory;
29 import org.eclipse.jetty.util.thread.QueuedThreadPool;
30 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
32 public class HttpsRest extends HttpBaseRest {
34 private static final Logger LOG = LogManager.getLogger(HttpsRest.class);
35 public static final String IOEXCEPTION = "IOException";
37 public void initHttpsRest() {
38 SslContextFactory sslContextFactory = new SslContextFactory();
39 client = new HttpClient(sslContextFactory);
40 client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
41 client.setMaxConnectionsPerAddress(200); // max 200 concurrent connections to every address
42 client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
43 client.setTimeout(30000); // 30 seconds timeout; if no server reply, the request expires
46 } catch(Exception e) {
47 LOG.error("Exception", e);
52 public RestfulResponse get(String servicePath, RestfulParametes restParametes) throws ServiceException {
57 public RestfulResponse head(String servicePath, RestfulParametes restParametes, RestfulOptions options)
58 throws ServiceException {
63 public RestfulResponse head(String servicePath, RestfulParametes restParametes) throws ServiceException {
68 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
69 throws ServiceException {
71 //This functionality has not been implemented yet
76 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options,
77 RestfulAsyncCallback callback) throws ServiceException {
79 //This functionality has not been implemented yet
84 public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
89 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
90 throws ServiceException {
92 //This functionality has not been implemented yet
97 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options,
98 RestfulAsyncCallback callback) throws ServiceException {
100 //This functionality has not been implemented yet
105 public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
110 public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options)
111 throws ServiceException {
116 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
117 throws ServiceException {
119 //This functionality has not been implemented yet
124 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options,
125 RestfulAsyncCallback callback) throws ServiceException {
127 //This functionality has not been implemented yet
132 public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
137 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
138 throws ServiceException {
140 //This functionality has not been implemented yet
145 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options,
146 RestfulAsyncCallback callback) throws ServiceException {
148 //This functionality has not been implemented yet
153 public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
158 public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options)
159 throws ServiceException {
164 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
165 throws ServiceException {
167 //This functionality has not been implemented yet
172 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options,
173 RestfulAsyncCallback callback) throws ServiceException {
175 //This functionality has not been implemented yet
180 public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option)
181 throws ServiceException {
182 ContentExchange exchange = new ContentExchange(true);
183 exchange.setURL(servicePath);
184 exchange.setMethod("GET");
185 restParametes.getHeaderMap().entrySet().stream()
186 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
189 client.send(exchange);
190 } catch(IOException e) {
191 LOG.error(IOEXCEPTION, e);
194 int exchangeState = exchange.waitForDone();
195 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
196 String res = exchange.getResponseContent();
199 RestfulResponse restfulResponse = new RestfulResponse();
200 restfulResponse.setResponseJson(exchange.getResponseContent());
201 restfulResponse.setStatus(exchange.getResponseStatus());
202 return restfulResponse;
203 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
204 throw new ServiceException(
205 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
206 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
207 throw new ServiceException(
208 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
210 } catch(InterruptedException e) {
211 LOG.error("InterruptedException", e);
212 // Restore interrupted state...
213 Thread.currentThread().interrupt();
214 } catch(UnsupportedEncodingException e) {
215 LOG.error("UnsupportedEncodingException", e);
221 public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
222 throws ServiceException {
223 ContentExchange exchange = new ContentExchange(true);
224 exchange.setURL(servicePath);
225 exchange.setMethod("PUT");
226 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
228 restParametes.getHeaderMap().entrySet().stream()
229 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
232 client.send(exchange);
233 } catch(IOException e) {
234 LOG.error(IOEXCEPTION, e);
238 int exchangeState = exchange.waitForDone();
239 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
240 String res = exchange.getResponseContent();
243 RestfulResponse restfulResponse = new RestfulResponse();
244 restfulResponse.setResponseJson(exchange.getResponseContent());
245 restfulResponse.setStatus(exchange.getResponseStatus());
246 return restfulResponse;
247 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
248 throw new ServiceException(
249 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
250 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
251 throw new ServiceException(
252 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
254 } catch(InterruptedException e) {
255 LOG.error("InterruptedException", e);
256 // Restore interrupted state...
257 Thread.currentThread().interrupt();
258 } catch(UnsupportedEncodingException e) {
259 LOG.error("UnsupportedEncodingException", e);
265 public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
266 throws ServiceException {
267 ContentExchange exchange = new ContentExchange(true);
269 String encodeParams = RequestUtil.encodeParams(restParametes);
270 if(encodeParams.isEmpty()) {
271 exchange.setURL(servicePath);
273 exchange.setURL(servicePath + "?" + encodeParams);
275 exchange.setMethod("DELETE");
276 if(restParametes.getRawData() != null) {
277 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
280 restParametes.getHeaderMap().entrySet().stream()
281 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
284 client.send(exchange);
285 } catch(IOException e) {
286 LOG.error(IOEXCEPTION, e);
290 int exchangeState = exchange.waitForDone();
291 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
292 String res = exchange.getResponseContent();
295 RestfulResponse restfulResponse = new RestfulResponse();
296 restfulResponse.setResponseJson(exchange.getResponseContent());
297 restfulResponse.setStatus(exchange.getResponseStatus());
298 return restfulResponse;
299 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
300 throw new ServiceException(
301 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
302 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
303 throw new ServiceException(
304 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
306 } catch(InterruptedException e) {
307 LOG.error("InterruptedException", e);
308 // Restore interrupted state...
309 Thread.currentThread().interrupt();
310 } catch(UnsupportedEncodingException e) {
311 LOG.error("InterruptedException", e);