Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / HttpClientUtil.java
1 package org.onap.msb.apiroute.wrapper.util;
2
3 import java.io.IOException;
4
5 import org.apache.http.client.ClientProtocolException;
6 import org.apache.http.client.config.RequestConfig;
7 import org.apache.http.client.methods.CloseableHttpResponse;
8 import org.apache.http.client.methods.HttpGet;
9 import org.apache.http.impl.client.CloseableHttpClient;
10 import org.apache.http.impl.client.HttpClients;
11 import org.apache.http.util.EntityUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class HttpClientUtil {
16
17   private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
18   
19   private static int connectionTimeOut = 2*1000;
20   
21
22   public static String httpGet(String url){
23     String result = null;
24     CloseableHttpClient httpClient = HttpClients.createDefault();
25     HttpGet httpGet = new HttpGet(url);
26     httpGet.addHeader("Content-type", "application/json; charset=utf-8");
27     httpGet.setHeader("Accept", "application/json");
28     try {
29       CloseableHttpResponse res = httpClient.execute(httpGet);
30       result = EntityUtils.toString(res.getEntity());
31       if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
32         logger.error(result);
33       }
34       res.close();
35     } catch (ClientProtocolException e) {
36       logger.error(url + ":httpGetWithJSON connect faild");
37     } catch (IOException e) {
38       logger.error( url + ":httpGetWithJSON connect faild");
39     } finally {
40       try {
41         httpClient.close();
42       } catch (IOException e) {
43         logger.error(url + ":close  httpClient faild");
44       }
45     }
46
47     return result;
48
49   }
50   
51   public static HttpGetResult httpGetStatusAndBody(String url){
52             HttpGetResult result= new HttpGetResult();
53             String body = null;
54             CloseableHttpClient httpClient = HttpClients.createDefault();
55             HttpGet httpGet = new HttpGet(url);
56             httpGet.addHeader("Content-type", "application/json; charset=utf-8");
57             httpGet.setHeader("Accept", "application/json");
58             
59             RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
60             httpGet.setConfig(requestConfig);
61             
62             try {
63               CloseableHttpResponse res = httpClient.execute(httpGet);
64               body = EntityUtils.toString(res.getEntity());
65               if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
66                 logger.error(body);
67               }
68               result.setBody(body);
69               result.setStatusCode(res.getStatusLine().getStatusCode());
70               res.close();
71             } catch (ClientProtocolException e) {
72               logger.error(url + ":httpGetWithJSON connect faild",e);
73             } catch (IOException e) {
74               logger.error( url + ":httpGetWithJSON connect faild",e);
75             } finally {
76               try {
77                 httpClient.close();
78               } catch (IOException e) {
79                 logger.error(url + ":close  httpClient faild");
80               }
81             }
82
83             return result;
84
85           }
86
87   public static int httpGetStatus(String url)  throws Exception{
88     int iStatus=500;
89     CloseableHttpClient httpClient = HttpClients.createDefault();
90
91
92     HttpGet httpGet = new HttpGet(url);
93     RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();//设置请求和传输超时时间
94     httpGet.setConfig(requestConfig);
95     httpGet.addHeader("Content-type", "application/json; charset=utf-8");
96     httpGet.setHeader("Accept", "application/json");
97     try {
98       CloseableHttpResponse res = httpClient.execute(httpGet);
99     
100       iStatus=res.getStatusLine().getStatusCode();
101       res.close();
102     } catch (ClientProtocolException e) {
103       logger.error(url + " httpGet connect faild:"+e.getMessage());
104     } catch (IOException e) {
105       logger.error(url + " httpGet connect faild:"+e.getMessage());
106     } finally {
107       try {
108         httpClient.close();
109       } catch (IOException e) {
110         logger.error(url + " httpGet close faild:"+e.getMessage());
111       }
112     }
113
114     return iStatus;
115
116   }
117 }