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 {
70 //This functionality has not been implemented yet
75 public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options,
76 RestfulAsyncCallback callback) throws ServiceException {
78 //This functionality has not been implemented yet
83 public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
88 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
89 throws ServiceException {
91 //This functionality has not been implemented yet
96 public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options,
97 RestfulAsyncCallback callback) throws ServiceException {
99 //This functionality has not been implemented yet
104 public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
109 public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options)
110 throws ServiceException {
115 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
116 throws ServiceException {
118 //This functionality has not been implemented yet
123 public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options,
124 RestfulAsyncCallback callback) throws ServiceException {
126 //This functionality has not been implemented yet
131 public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
136 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
137 throws ServiceException {
139 //This functionality has not been implemented yet
144 public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options,
145 RestfulAsyncCallback callback) throws ServiceException {
147 //This functionality has not been implemented yet
152 public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
157 public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options)
158 throws ServiceException {
163 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
164 throws ServiceException {
166 //This functionality has not been implemented yet
171 public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options,
172 RestfulAsyncCallback callback) throws ServiceException {
174 //This functionality has not been implemented yet
179 public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option)
180 throws ServiceException {
181 ContentExchange exchange = new ContentExchange(true);
182 exchange.setURL(servicePath);
183 exchange.setMethod("GET");
184 restParametes.getHeaderMap().entrySet().stream()
185 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
188 client.send(exchange);
189 } catch(IOException e) {
190 LOG.error("IOException", e);
193 int exchangeState = exchange.waitForDone();
194 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
195 String res = exchange.getResponseContent();
198 RestfulResponse restfulResponse = new RestfulResponse();
199 restfulResponse.setResponseJson(exchange.getResponseContent());
200 restfulResponse.setStatus(exchange.getResponseStatus());
201 return restfulResponse;
202 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
203 throw new ServiceException(
204 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
205 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
206 throw new ServiceException(
207 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
209 } catch(InterruptedException e) {
210 LOG.error("InterruptedException", e);
211 // Restore interrupted state...
212 Thread.currentThread().interrupt();
213 } catch(UnsupportedEncodingException e) {
214 LOG.error("UnsupportedEncodingException", e);
220 public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
221 throws ServiceException {
222 ContentExchange exchange = new ContentExchange(true);
223 exchange.setURL(servicePath);
224 exchange.setMethod("PUT");
225 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
227 restParametes.getHeaderMap().entrySet().stream()
228 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
231 client.send(exchange);
232 } catch(IOException e) {
233 LOG.error("IOException", e);
237 int exchangeState = exchange.waitForDone();
238 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
239 String res = exchange.getResponseContent();
242 RestfulResponse restfulResponse = new RestfulResponse();
243 restfulResponse.setResponseJson(exchange.getResponseContent());
244 restfulResponse.setStatus(exchange.getResponseStatus());
245 return restfulResponse;
246 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
247 throw new ServiceException(
248 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
249 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
250 throw new ServiceException(
251 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
253 } catch(InterruptedException e) {
254 LOG.error("InterruptedException", e);
255 // Restore interrupted state...
256 Thread.currentThread().interrupt();
257 } catch(UnsupportedEncodingException e) {
258 LOG.error("UnsupportedEncodingException", e);
264 public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
265 throws ServiceException {
266 ContentExchange exchange = new ContentExchange(true);
268 String encodeParams = RequestUtil.encodeParams(restParametes);
269 if(encodeParams.isEmpty()) {
270 exchange.setURL(servicePath);
272 exchange.setURL(servicePath + "?" + encodeParams);
274 exchange.setMethod("DELETE");
275 if(restParametes.getRawData() != null) {
276 exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
279 restParametes.getHeaderMap().entrySet().stream()
280 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
283 client.send(exchange);
284 } catch(IOException e) {
285 LOG.error("IOException", e);
289 int exchangeState = exchange.waitForDone();
290 if(exchangeState == HttpExchange.STATUS_COMPLETED) {
291 String res = exchange.getResponseContent();
294 RestfulResponse restfulResponse = new RestfulResponse();
295 restfulResponse.setResponseJson(exchange.getResponseContent());
296 restfulResponse.setStatus(exchange.getResponseStatus());
297 return restfulResponse;
298 } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
299 throw new ServiceException(
300 "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
301 } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
302 throw new ServiceException(
303 "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
305 } catch(InterruptedException e) {
306 LOG.error("InterruptedException", e);
307 // Restore interrupted state...
308 Thread.currentThread().interrupt();
309 } catch(UnsupportedEncodingException e) {
310 LOG.error("InterruptedException", e);