--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.common.utility.http;\r
+\r
+import com.google.common.base.Strings;\r
+import java.io.IOException;\r
+import java.io.UnsupportedEncodingException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Timer;\r
+import java.util.TimerTask;\r
+import java.util.concurrent.ExecutionException;\r
+import java.util.concurrent.Future;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpPost;\r
+import org.apache.http.client.methods.HttpRequestBase;\r
+import org.apache.http.entity.StringEntity;\r
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;\r
+import org.apache.http.impl.nio.client.HttpAsyncClients;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class RequestUtility {\r
+\r
+ private static Logger logger = LoggerFactory.getLogger(RequestUtility.class);\r
+\r
+ public static void postAsync(String url, String body, Map<String, String> headers)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ HttpPost post = buildPost(url, body, headers);\r
+ executeAsync(post);\r
+ }\r
+\r
+ public static HttpResponse postSync(String url, String body, Map<String, String> headers)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ HttpPost post = buildPost(url, body, headers);\r
+ return executeSync(post);\r
+ }\r
+\r
+ public static HttpResponse postSync(\r
+ String url, String body, Map<String, String> headers, int timeoutInMillis)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ HttpPost post = buildPost(url, body, headers);\r
+ return executeSync(post, timeoutInMillis);\r
+ }\r
+\r
+ public static HttpResponse getSync(String url, Map<String, String> headers)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ HttpGet get = buildGet(url, headers);\r
+ return executeSync(get);\r
+ }\r
+\r
+ public static HttpResponse getSync(String url, Map<String, String> headers, int timeoutInMillis)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ if (timeoutInMillis < 0) {\r
+ throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");\r
+ }\r
+\r
+ HttpGet get = buildGet(url, headers);\r
+ return executeSync(get, timeoutInMillis);\r
+ }\r
+\r
+ public static void getAsync(String url, Map<String, String> headers) throws IOException {\r
+ HttpGet get = buildGet(url, headers);\r
+ executeAsync(get);\r
+ }\r
+\r
+ private static HttpPost buildPost(String url, String body, Map<String, String> headers)\r
+ throws UnsupportedEncodingException {\r
+ if (Strings.isNullOrEmpty(url) || Strings.isNullOrEmpty(body)) {\r
+ return null;\r
+ } else if (headers == null) {\r
+ headers = new HashMap<>();\r
+ }\r
+\r
+ HttpPost post = new HttpPost(url);\r
+ headers.forEach(post::setHeader);\r
+ post.setEntity(new StringEntity(body));\r
+ return post;\r
+ }\r
+\r
+ private static HttpGet buildGet(String url, Map<String, String> headers) {\r
+ if (Strings.isNullOrEmpty(url)) {\r
+ return null;\r
+ } else if (headers == null) {\r
+ headers = new HashMap<>();\r
+ }\r
+\r
+ HttpGet get = new HttpGet(url);\r
+ headers.forEach(get::setHeader);\r
+ return get;\r
+ }\r
+\r
+ private static HttpResponse executeSync(HttpRequestBase request)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
+ try {\r
+ httpClient.start();\r
+ Future<HttpResponse> future = httpClient.execute(request, null);\r
+ return future.get();\r
+ } finally {\r
+ httpClient.close();\r
+ }\r
+ }\r
+\r
+ private static HttpResponse executeSync(HttpRequestBase request, int timeoutInMillis)\r
+ throws IOException, InterruptedException, ExecutionException {\r
+ if (timeoutInMillis < 0) {\r
+ throw new IllegalArgumentException("The timeoutInMillis must be a value greater than 0.");\r
+ }\r
+\r
+ // Create a timer task that will abort the task (the request) after the specified time. This\r
+ // task will run *timeoutInMillis* ms\r
+ TimerTask task =\r
+ new TimerTask() {\r
+ @Override\r
+ public void run() {\r
+ if (request != null) {\r
+ request.abort();\r
+ }\r
+ }\r
+ };\r
+\r
+ CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
+ try {\r
+ httpClient.start();\r
+ // Start the timer before making the request.\r
+ new Timer(true).schedule(task, timeoutInMillis);\r
+ Future<HttpResponse> future = httpClient.execute(request, null);\r
+ return future.get();\r
+ } finally {\r
+ httpClient.close();\r
+ }\r
+ }\r
+\r
+ private static void executeAsync(HttpRequestBase request) throws IOException {\r
+ CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
+ try {\r
+ httpClient.start();\r
+ httpClient.execute(request, null);\r
+ logger.debug("Sent asynchronous request.");\r
+ } finally {\r
+ httpClient.close();\r
+ }\r
+ }\r
+}\r