added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / common / utility / http / RequestUtility.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/common/utility/http/RequestUtility.java b/otf-camunda/src/main/java/org/oran/otf/common/utility/http/RequestUtility.java
new file mode 100644 (file)
index 0000000..8cd2eff
--- /dev/null
@@ -0,0 +1,195 @@
+/*  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.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.Future;\r
+import org.apache.http.HttpHost;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.config.RequestConfig;\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.client.protocol.HttpClientContext;\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.apache.http.protocol.BasicHttpContext;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class RequestUtility {\r
+\r
+  private static final Logger logger = LoggerFactory.getLogger(RequestUtility.class);\r
+\r
+  public static void postAsync(String url, String body, Map<String, String> headers, Boolean proxy)\r
+      throws Exception {\r
+    HttpPost post = buildPost(url, body, headers);\r
+    executeAsync(post, proxy);\r
+  }\r
+\r
+  public static HttpResponse postSync(\r
+      String url, String body, Map<String, String> headers, Boolean proxy) throws Exception {\r
+    HttpPost post = buildPost(url, body, headers);\r
+    return executeSync(post, proxy);\r
+  }\r
+\r
+  public static HttpResponse postSync(\r
+      String url, String body, Map<String, String> headers, int timeoutInMillis, Boolean proxy)\r
+      throws Exception {\r
+    HttpPost post = buildPost(url, body, headers);\r
+    return executeSync(post, timeoutInMillis, proxy);\r
+  }\r
+\r
+  public static HttpResponse getSync(String url, Map<String, String> headers, Boolean proxy)\r
+      throws Exception {\r
+    HttpGet get = buildGet(url, headers);\r
+    return executeSync(get, proxy);\r
+  }\r
+\r
+  public static HttpResponse getSync(\r
+      String url, Map<String, String> headers, int timeoutInMillis, Boolean proxy)\r
+      throws Exception {\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, proxy);\r
+  }\r
+\r
+  public static void getAsync(String url, Map<String, String> headers, Boolean proxy)\r
+      throws Exception {\r
+    HttpGet get = buildGet(url, headers);\r
+    executeAsync(get, proxy);\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, Boolean proxy) throws Exception {\r
+    CloseableHttpAsyncClient httpClient = createHttpAsyncClient();\r
+    try {\r
+      httpClient.start();\r
+      Future<HttpResponse> future =\r
+          proxy\r
+              ? httpClient.execute(request, createHttpClientContext(), null)\r
+              : httpClient.execute(request, null);\r
+      return future.get();\r
+    } catch (Exception e) {\r
+      throw e;\r
+    } finally {\r
+      httpClient.close();\r
+    }\r
+  }\r
+\r
+  private static HttpResponse executeSync(\r
+      HttpRequestBase request, int timeoutInMillis, Boolean proxy) throws Exception {\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 = createHttpAsyncClient();\r
+    try {\r
+      httpClient.start();\r
+      // Start the timer before making the request.\r
+      new Timer(true).schedule(task, timeoutInMillis);\r
+      Future<HttpResponse> future =\r
+          proxy\r
+              ? httpClient.execute(request, createHttpClientContext(), null)\r
+              : httpClient.execute(request, null);\r
+\r
+      return future.get();\r
+    } catch (Exception e) {\r
+      throw e;\r
+    } finally {\r
+      httpClient.close();\r
+    }\r
+  }\r
+\r
+  private static void executeAsync(HttpRequestBase request, Boolean proxy) throws Exception {\r
+    CloseableHttpAsyncClient httpClient = createHttpAsyncClient();\r
+    try {\r
+      httpClient.start();\r
+      Future<HttpResponse> future =\r
+          proxy\r
+              ? httpClient.execute(request, createHttpClientContext(), null)\r
+              : httpClient.execute(request, null);\r
+      logger.debug("Sent asynchronous request.");\r
+    } catch (Exception e) {\r
+      throw e;\r
+    } finally {\r
+      httpClient.close();\r
+    }\r
+  }\r
+\r
+  private static RequestConfig configureProxy() {\r
+    HttpHost proxy;\r
+    proxy = new HttpHost("localhost", 8080, "http");\r
+    return RequestConfig.custom().setProxy(proxy).build();\r
+  }\r
+\r
+  private static HttpClientContext createHttpClientContext() {\r
+    HttpClientContext localContext = HttpClientContext.adapt(new BasicHttpContext());\r
+    localContext.setRequestConfig(configureProxy());\r
+    return localContext;\r
+  }\r
+\r
+  private static CloseableHttpAsyncClient createHttpAsyncClient() throws Exception {\r
+    return HttpAsyncClients.createDefault();\r
+  }\r
+}\r