added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / common / utility / http / RequestUtility.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.common.utility.http;\r
18 \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
37 \r
38 public class RequestUtility {\r
39 \r
40   private static Logger logger = LoggerFactory.getLogger(RequestUtility.class);\r
41 \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
45     executeAsync(post);\r
46   }\r
47 \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
52   }\r
53 \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
59   }\r
60 \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
65   }\r
66 \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
71     }\r
72 \r
73     HttpGet get = buildGet(url, headers);\r
74     return executeSync(get, timeoutInMillis);\r
75   }\r
76 \r
77   public static void getAsync(String url, Map<String, String> headers) throws IOException {\r
78     HttpGet get = buildGet(url, headers);\r
79     executeAsync(get);\r
80   }\r
81 \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
85       return null;\r
86     } else if (headers == null) {\r
87       headers = new HashMap<>();\r
88     }\r
89 \r
90     HttpPost post = new HttpPost(url);\r
91     headers.forEach(post::setHeader);\r
92     post.setEntity(new StringEntity(body));\r
93     return post;\r
94   }\r
95 \r
96   private static HttpGet buildGet(String url, Map<String, String> headers) {\r
97     if (Strings.isNullOrEmpty(url)) {\r
98       return null;\r
99     } else if (headers == null) {\r
100       headers = new HashMap<>();\r
101     }\r
102 \r
103     HttpGet get = new HttpGet(url);\r
104     headers.forEach(get::setHeader);\r
105     return get;\r
106   }\r
107 \r
108   private static HttpResponse executeSync(HttpRequestBase request)\r
109       throws IOException, InterruptedException, ExecutionException {\r
110     CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
111     try {\r
112       httpClient.start();\r
113       Future<HttpResponse> future = httpClient.execute(request, null);\r
114       return future.get();\r
115     } finally {\r
116       httpClient.close();\r
117     }\r
118   }\r
119 \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
124     }\r
125 \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
128     TimerTask task =\r
129         new TimerTask() {\r
130           @Override\r
131           public void run() {\r
132             if (request != null) {\r
133               request.abort();\r
134             }\r
135           }\r
136         };\r
137 \r
138     CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
139     try {\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
145     } finally {\r
146       httpClient.close();\r
147     }\r
148   }\r
149 \r
150   private static void executeAsync(HttpRequestBase request) throws IOException {\r
151     CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();\r
152     try {\r
153       httpClient.start();\r
154       httpClient.execute(request, null);\r
155       logger.debug("Sent asynchronous request.");\r
156     } finally {\r
157       httpClient.close();\r
158     }\r
159   }\r
160 }\r