1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.common.utility.http;
\r
19 import com.google.common.base.Strings;
\r
20 import java.io.UnsupportedEncodingException;
\r
21 import java.util.HashMap;
\r
22 import java.util.Map;
\r
23 import java.util.Timer;
\r
24 import java.util.TimerTask;
\r
25 import java.util.concurrent.Future;
\r
26 import org.apache.http.HttpHost;
\r
27 import org.apache.http.HttpResponse;
\r
28 import org.apache.http.client.config.RequestConfig;
\r
29 import org.apache.http.client.methods.HttpGet;
\r
30 import org.apache.http.client.methods.HttpPost;
\r
31 import org.apache.http.client.methods.HttpRequestBase;
\r
32 import org.apache.http.client.protocol.HttpClientContext;
\r
33 import org.apache.http.entity.StringEntity;
\r
34 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
\r
35 import org.apache.http.impl.nio.client.HttpAsyncClients;
\r
36 import org.apache.http.protocol.BasicHttpContext;
\r
37 import org.slf4j.Logger;
\r
38 import org.slf4j.LoggerFactory;
\r
40 public class RequestUtility {
\r
42 private static final Logger logger = LoggerFactory.getLogger(RequestUtility.class);
\r
44 public static void postAsync(String url, String body, Map<String, String> headers, Boolean proxy)
\r
46 HttpPost post = buildPost(url, body, headers);
\r
47 executeAsync(post, proxy);
\r
50 public static HttpResponse postSync(
\r
51 String url, String body, Map<String, String> headers, Boolean proxy) throws Exception {
\r
52 HttpPost post = buildPost(url, body, headers);
\r
53 return executeSync(post, proxy);
\r
56 public static HttpResponse postSync(
\r
57 String url, String body, Map<String, String> headers, int timeoutInMillis, Boolean proxy)
\r
59 HttpPost post = buildPost(url, body, headers);
\r
60 return executeSync(post, timeoutInMillis, proxy);
\r
63 public static HttpResponse getSync(String url, Map<String, String> headers, Boolean proxy)
\r
65 HttpGet get = buildGet(url, headers);
\r
66 return executeSync(get, proxy);
\r
69 public static HttpResponse getSync(
\r
70 String url, Map<String, String> headers, int timeoutInMillis, Boolean proxy)
\r
72 if (timeoutInMillis < 0) {
\r
73 throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");
\r
76 HttpGet get = buildGet(url, headers);
\r
77 return executeSync(get, timeoutInMillis, proxy);
\r
80 public static void getAsync(String url, Map<String, String> headers, Boolean proxy)
\r
82 HttpGet get = buildGet(url, headers);
\r
83 executeAsync(get, proxy);
\r
86 private static HttpPost buildPost(String url, String body, Map<String, String> headers)
\r
87 throws UnsupportedEncodingException {
\r
88 if (Strings.isNullOrEmpty(url) || Strings.isNullOrEmpty(body)) {
\r
90 } else if (headers == null) {
\r
91 headers = new HashMap<>();
\r
94 HttpPost post = new HttpPost(url);
\r
95 headers.forEach(post::setHeader);
\r
96 post.setEntity(new StringEntity(body));
\r
100 private static HttpGet buildGet(String url, Map<String, String> headers) {
\r
101 if (Strings.isNullOrEmpty(url)) {
\r
103 } else if (headers == null) {
\r
104 headers = new HashMap<>();
\r
107 HttpGet get = new HttpGet(url);
\r
108 headers.forEach(get::setHeader);
\r
112 private static HttpResponse executeSync(HttpRequestBase request, Boolean proxy) throws Exception {
\r
113 CloseableHttpAsyncClient httpClient = createHttpAsyncClient();
\r
115 httpClient.start();
\r
116 Future<HttpResponse> future =
\r
118 ? httpClient.execute(request, createHttpClientContext(), null)
\r
119 : httpClient.execute(request, null);
\r
120 return future.get();
\r
121 } catch (Exception e) {
\r
124 httpClient.close();
\r
128 private static HttpResponse executeSync(
\r
129 HttpRequestBase request, int timeoutInMillis, Boolean proxy) throws Exception {
\r
130 if (timeoutInMillis < 0) {
\r
131 throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");
\r
134 // Create a timer task that will abort the task (the request) after the specified time. This
\r
135 // task will run *timeoutInMillis* ms
\r
139 public void run() {
\r
140 if (request != null) {
\r
146 CloseableHttpAsyncClient httpClient = createHttpAsyncClient();
\r
148 httpClient.start();
\r
149 // Start the timer before making the request.
\r
150 new Timer(true).schedule(task, timeoutInMillis);
\r
151 Future<HttpResponse> future =
\r
153 ? httpClient.execute(request, createHttpClientContext(), null)
\r
154 : httpClient.execute(request, null);
\r
156 return future.get();
\r
157 } catch (Exception e) {
\r
160 httpClient.close();
\r
164 private static void executeAsync(HttpRequestBase request, Boolean proxy) throws Exception {
\r
165 CloseableHttpAsyncClient httpClient = createHttpAsyncClient();
\r
167 httpClient.start();
\r
168 Future<HttpResponse> future =
\r
170 ? httpClient.execute(request, createHttpClientContext(), null)
\r
171 : httpClient.execute(request, null);
\r
172 logger.debug("Sent asynchronous request.");
\r
173 } catch (Exception e) {
\r
176 httpClient.close();
\r
180 private static RequestConfig configureProxy() {
\r
182 proxy = new HttpHost("localhost", 8080, "http");
\r
183 return RequestConfig.custom().setProxy(proxy).build();
\r
186 private static HttpClientContext createHttpClientContext() {
\r
187 HttpClientContext localContext = HttpClientContext.adapt(new BasicHttpContext());
\r
188 localContext.setRequestConfig(configureProxy());
\r
189 return localContext;
\r
192 private static CloseableHttpAsyncClient createHttpAsyncClient() throws Exception {
\r
193 return HttpAsyncClients.createDefault();
\r