2 * Copyright (C) 2018 CMCC, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.usecaseui.server.util;
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.util.ArrayList;
22 import java.util.List;
26 import org.apache.http.HttpStatus;
27 import org.apache.http.NameValuePair;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.entity.UrlEncodedFormEntity;
30 import org.apache.http.client.methods.*;
31 import org.apache.http.entity.ContentType;
32 import org.apache.http.entity.StringEntity;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.message.BasicNameValuePair;
36 import org.apache.http.util.EntityUtils;
37 import org.onap.usecaseui.server.bean.HttpResponseResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import javax.servlet.http.HttpServletRequest;
43 import static org.onap.usecaseui.server.constant.CommonConstant.BLANK;
44 import static org.onap.usecaseui.server.constant.CommonConstant.ENCODING_UTF8;
46 public class HttpUtil {
47 private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
48 private static final String LOG_FORMATTER = "[ {} ] {} ";
49 private static final String CLIENT_PRTOCOL_EXCEPTION = "Client protocol exception";
50 private static final String IO_EXCEPTION = "IO Exception occured";
51 private static final String EXCEPTION = "Exception occured";
52 private static final String HTTP_CLIENT_CLOSING_EXCEPTION = "Exception occured while closing httpClient";
55 * common POST method for REST API calling by using map request body
59 * @param requestBodyMap
60 * @return HttpResponseResult
62 public static HttpResponseResult sendPostRequestByMap(
64 Map<String, String> headerMap,
65 Map<String, String> requestBodyMap) {
66 logger.info(LOG_FORMATTER ,url , " API POST calling is starting......");
67 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
68 CloseableHttpClient httpClient = HttpClients.createDefault();
71 // set request url and header for API calling
72 HttpPost httpPost = new HttpPost(url);
73 setHeader(httpPost, headerMap);
75 // set request body for API calling
76 httpPost.setEntity(setBodyByMap(requestBodyMap));
78 // execute API calling and set response
79 CloseableHttpResponse response = httpClient.execute(httpPost);
80 setResponse(response, responseResult);
81 } catch (ClientProtocolException cpe) {
82 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
83 } catch (IOException ioe) {
84 logger.error(IO_EXCEPTION,ioe);
85 } catch (Exception e) {
86 logger.error("EXCEPTION",e);
90 } catch (Exception e) {
91 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
95 logger.info(LOG_FORMATTER ,url , " API POST calling has finished!");
96 return responseResult;
100 * common POST method for REST API calling by using json request body
104 * @param requestBodyJson
105 * @return HttpResponseResult
107 public static HttpResponseResult sendPostRequestByJson(
109 Map<String, String> headerMap,
110 String requestBodyJson) {
111 logger.info(LOG_FORMATTER ,url , " API POST calling is starting......");
112 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
113 CloseableHttpClient httpClient = HttpClients.createDefault();
116 // set request url and header for API calling
117 HttpPost httpPost = new HttpPost(url);
118 setHeader(httpPost, headerMap);
120 // set request body for API calling
121 httpPost.setEntity(setBodyByJson(requestBodyJson));
123 // execute API calling and return response
124 CloseableHttpResponse response = httpClient.execute(httpPost);
125 setResponse(response, responseResult);
126 } catch (ClientProtocolException cpe) {
127 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
128 } catch (IOException ioe) {
129 logger.error(IO_EXCEPTION,ioe);
130 } catch (Exception e) {
131 logger.error(EXCEPTION,e);
135 } catch (Exception e) {
136 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
140 logger.info(LOG_FORMATTER ,url , " API POST calling has finished!");
141 return responseResult;
145 * common GET method for REST API calling
149 * @return HttpResponseResult
151 public static HttpResponseResult sendGetRequest(
153 Map<String, String> headerMap) {
154 logger.info(LOG_FORMATTER ,url , "API GET calling is starting......");
155 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
156 CloseableHttpClient httpClient = HttpClients.createDefault();
159 // set request url and header for API calling
160 HttpGet httpGet = new HttpGet(url);
161 setHeader(httpGet, headerMap);
163 // execute API calling and return response
164 CloseableHttpResponse response = httpClient.execute(httpGet);
165 setResponse(response, responseResult);
166 } catch (ClientProtocolException cpe) {
167 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
168 } catch (IOException ioe) {
169 logger.error(IO_EXCEPTION,ioe);
170 } catch (Exception e) {
171 logger.error(EXCEPTION,e);
175 } catch (Exception e) {
176 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
180 logger.info(LOG_FORMATTER ,url , "API GET calling has finished!");
181 return responseResult;
185 * common PUT method for REST API calling by using map request body
188 * @param headerMap AAAAA
189 * @param requestBodyMap AAAAA
190 * @return HttpResponseResult
192 public static HttpResponseResult sendPutRequestByMap(
194 Map<String, String> headerMap,
195 Map<String, String> requestBodyMap) {
196 logger.info(LOG_FORMATTER ,url , "API PUT calling is starting......");
197 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
198 CloseableHttpClient httpClient = HttpClients.createDefault();
201 // set request url and header for API calling
202 HttpPut httpPut = new HttpPut(url);
203 setHeader(httpPut, headerMap);
205 // set request body for API calling
206 httpPut.setEntity(setBodyByMap(requestBodyMap));
208 // execute API calling and set response
209 CloseableHttpResponse response = httpClient.execute(httpPut);
210 setResponse(response, responseResult);
211 } catch (ClientProtocolException cpe) {
212 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
213 } catch (IOException ioe) {
214 logger.error(IO_EXCEPTION,ioe);
215 } catch (Exception e) {
216 logger.error(EXCEPTION,e);
220 } catch (Exception e) {
221 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
225 logger.info(LOG_FORMATTER ,url , " API PUT calling has finished!");
226 return responseResult;
230 * common PUT method for REST API calling by using json request body
234 * @param requestBodyJson
235 * @return HttpResponseResult
237 public static HttpResponseResult sendPutRequestByJson(
239 Map<String, String> headerMap,
240 String requestBodyJson) {
241 logger.info(LOG_FORMATTER, url , "API PUT calling is starting......");
242 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
243 CloseableHttpClient httpClient = HttpClients.createDefault();
246 // set request url and header for API calling
247 HttpPut httpPut = new HttpPut(url);
248 setHeader(httpPut, headerMap);
250 // set request body for API calling
251 httpPut.setEntity(setBodyByJson(requestBodyJson));
253 // execute API calling and return response
254 CloseableHttpResponse response = httpClient.execute(httpPut);
255 setResponse(response, responseResult);
256 } catch (ClientProtocolException cpe) {
257 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
258 } catch (IOException ioe) {
259 logger.error(IO_EXCEPTION,ioe);
260 } catch (Exception e) {
261 logger.error(EXCEPTION,e);
265 } catch (Exception e) {
266 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
270 logger.info(LOG_FORMATTER,url , " API PUT calling has finished!");
271 return responseResult;
275 * common DELETE method for REST API calling
279 * @return HttpResponseResult
281 public HttpResponseResult sendDeleteRequest(
283 Map<String, String> headerMap) {
284 logger.info(LOG_FORMATTER,url , " API DELETE calling is starting......");
285 HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
286 CloseableHttpClient httpClient = HttpClients.createDefault();
289 // set request url and header for API calling
290 HttpDelete httpDelete = new HttpDelete(url);
291 setHeader(httpDelete, headerMap);
293 // execute API calling and return response
294 CloseableHttpResponse response = httpClient.execute(httpDelete);
295 setResponse(response, responseResult);
296 } catch (ClientProtocolException cpe) {
297 logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
298 } catch (IOException ioe) {
299 logger.error(IO_EXCEPTION,ioe);
300 } catch (Exception e) {
301 logger.error(EXCEPTION,e);
305 } catch (Exception e) {
306 logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
310 logger.info(LOG_FORMATTER,url , " API DELETE calling has finished!");
311 return responseResult;
315 * get string content from request body
320 public static String ReadAsChars(HttpServletRequest request) {
321 BufferedReader br = null;
322 StringBuilder sb = new StringBuilder(BLANK);
325 br = request.getReader();
327 while ((tempString = br.readLine()) != null) {
328 sb.append(tempString);
331 } catch (IOException ioe) {
332 logger.error("IO exception occured",ioe);
337 } catch (IOException ioe) {
338 logger.error("IO exception occured",ioe);
343 return sb.toString();
346 private static void setHeader(HttpRequestBase request, Map<String, String> headerMap) {
347 if (headerMap != null) {
348 Set<String> keySet = headerMap.keySet();
349 for (String key : keySet) {
350 request.addHeader(key, headerMap.get(key));
355 private static UrlEncodedFormEntity setBodyByMap(Map<String, String> requestBodyMap) throws UnsupportedEncodingException {
356 List<NameValuePair> nvp = new ArrayList<>();
357 if (requestBodyMap != null) {
358 for (Map.Entry<String, String> entry : requestBodyMap.entrySet()) {
359 nvp.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
362 return new UrlEncodedFormEntity(nvp, ENCODING_UTF8);
365 private static StringEntity setBodyByJson(String requestBodyJson) {
366 StringEntity se = new StringEntity(requestBodyJson, ContentType.APPLICATION_JSON);
367 se.setContentEncoding(ENCODING_UTF8);
371 private static void setResponse(CloseableHttpResponse response, HttpResponseResult responseResult) throws IOException {
372 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
373 responseResult.setResultContent(EntityUtils.toString(response.getEntity(), ENCODING_UTF8));
375 responseResult.setResultCode(response.getStatusLine().getStatusCode());