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.IOException;
\r
21 import java.io.UnsupportedEncodingException;
\r
22 import java.util.HashMap;
\r
23 import java.util.Map;
\r
24 import java.util.Timer;
\r
25 import java.util.TimerTask;
\r
26 import java.util.concurrent.ExecutionException;
\r
27 import java.util.concurrent.Future;
\r
28 import org.apache.http.HttpResponse;
\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.entity.StringEntity;
\r
33 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
\r
34 import org.apache.http.impl.nio.client.HttpAsyncClients;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 public class RequestUtility {
\r
40 private static Logger logger = LoggerFactory.getLogger(RequestUtility.class);
\r
42 public static void postAsync(String url, String body, Map<String, String> headers)
\r
43 throws IOException, InterruptedException, ExecutionException {
\r
44 HttpPost post = buildPost(url, body, headers);
\r
48 public static HttpResponse postSync(String url, String body, Map<String, String> headers)
\r
49 throws IOException, InterruptedException, ExecutionException {
\r
50 HttpPost post = buildPost(url, body, headers);
\r
51 return executeSync(post);
\r
54 public static HttpResponse postSync(
\r
55 String url, String body, Map<String, String> headers, int timeoutInMillis)
\r
56 throws IOException, InterruptedException, ExecutionException {
\r
57 HttpPost post = buildPost(url, body, headers);
\r
58 return executeSync(post, timeoutInMillis);
\r
61 public static HttpResponse getSync(String url, Map<String, String> headers)
\r
62 throws IOException, InterruptedException, ExecutionException {
\r
63 HttpGet get = buildGet(url, headers);
\r
64 return executeSync(get);
\r
67 public static HttpResponse getSync(String url, Map<String, String> headers, int timeoutInMillis)
\r
68 throws IOException, InterruptedException, ExecutionException {
\r
69 if (timeoutInMillis < 0) {
\r
70 throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");
\r
73 HttpGet get = buildGet(url, headers);
\r
74 return executeSync(get, timeoutInMillis);
\r
77 public static void getAsync(String url, Map<String, String> headers) throws IOException {
\r
78 HttpGet get = buildGet(url, headers);
\r
82 private static HttpPost buildPost(String url, String body, Map<String, String> headers)
\r
83 throws UnsupportedEncodingException {
\r
84 if (Strings.isNullOrEmpty(url) || Strings.isNullOrEmpty(body)) {
\r
86 } else if (headers == null) {
\r
87 headers = new HashMap<>();
\r
90 HttpPost post = new HttpPost(url);
\r
91 headers.forEach(post::setHeader);
\r
92 post.setEntity(new StringEntity(body));
\r
96 private static HttpGet buildGet(String url, Map<String, String> headers) {
\r
97 if (Strings.isNullOrEmpty(url)) {
\r
99 } else if (headers == null) {
\r
100 headers = new HashMap<>();
\r
103 HttpGet get = new HttpGet(url);
\r
104 headers.forEach(get::setHeader);
\r
108 private static HttpResponse executeSync(HttpRequestBase request)
\r
109 throws IOException, InterruptedException, ExecutionException {
\r
110 CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
\r
112 httpClient.start();
\r
113 Future<HttpResponse> future = httpClient.execute(request, null);
\r
114 return future.get();
\r
116 httpClient.close();
\r
120 private static HttpResponse executeSync(HttpRequestBase request, int timeoutInMillis)
\r
121 throws IOException, InterruptedException, ExecutionException {
\r
122 if (timeoutInMillis < 0) {
\r
123 throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");
\r
126 // Create a timer task that will abort the task (the request) after the specified time. This
\r
127 // task will run *timeoutInMillis* ms
\r
131 public void run() {
\r
132 if (request != null) {
\r
138 CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
\r
140 httpClient.start();
\r
141 // Start the timer before making the request.
\r
142 new Timer(true).schedule(task, timeoutInMillis);
\r
143 Future<HttpResponse> future = httpClient.execute(request, null);
\r
144 return future.get();
\r
146 httpClient.close();
\r
150 private static void executeAsync(HttpRequestBase request) throws IOException {
\r
151 CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
\r
153 httpClient.start();
\r
154 httpClient.execute(request, null);
\r
155 logger.debug("Sent asynchronous request.");
\r
157 httpClient.close();
\r