cb80159a9fbafa5f76d96e28e0b11992502a28da
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / HttpClientUtil.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
15
16 import java.io.IOException;
17
18 import org.apache.http.client.ClientProtocolException;
19 import org.apache.http.client.config.RequestConfig;
20 import org.apache.http.client.methods.CloseableHttpResponse;
21 import org.apache.http.client.methods.HttpGet;
22 import org.apache.http.impl.client.CloseableHttpClient;
23 import org.apache.http.impl.client.HttpClients;
24 import org.apache.http.util.EntityUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class HttpClientUtil {
29
30     private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
31
32     private static int connectionTimeOut = 2 * 1000;
33
34
35     public static String httpGet(String url) {
36         String result = null;
37         CloseableHttpClient httpClient = HttpClients.createDefault();
38         HttpGet httpGet = new HttpGet(url);
39         httpGet.addHeader("Content-type", "application/json; charset=utf-8");
40         httpGet.setHeader("Accept", "application/json");
41         try {
42             CloseableHttpResponse res = httpClient.execute(httpGet);
43             result = EntityUtils.toString(res.getEntity());
44             if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
45                 logger.error(result);
46             }
47             res.close();
48         } catch (ClientProtocolException e) {
49             logger.error(url + ":httpGetWithJSON connect faild");
50         } catch (IOException e) {
51             logger.error(url + ":httpGetWithJSON connect faild");
52         } finally {
53             try {
54                 httpClient.close();
55             } catch (IOException e) {
56                 logger.error(url + ":close  httpClient faild");
57             }
58         }
59
60         return result;
61
62     }
63
64     public static HttpGetResult httpGetStatusAndBody(String url) {
65         HttpGetResult result = new HttpGetResult();
66         String body = null;
67         CloseableHttpClient httpClient = HttpClients.createDefault();
68         HttpGet httpGet = new HttpGet(url);
69         httpGet.addHeader("Content-type", "application/json; charset=utf-8");
70         httpGet.setHeader("Accept", "application/json");
71
72         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
73         httpGet.setConfig(requestConfig);
74
75         try {
76             CloseableHttpResponse res = httpClient.execute(httpGet);
77             body = EntityUtils.toString(res.getEntity());
78             if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
79                 logger.error(body);
80             }
81             result.setBody(body);
82             result.setStatusCode(res.getStatusLine().getStatusCode());
83             res.close();
84         } catch (ClientProtocolException e) {
85             logger.error(url + ":httpGetWithJSON connect faild", e);
86         } catch (IOException e) {
87             logger.error(url + ":httpGetWithJSON connect faild", e);
88         } finally {
89             try {
90                 httpClient.close();
91             } catch (IOException e) {
92                 logger.error(url + ":close  httpClient faild");
93             }
94         }
95
96         return result;
97
98     }
99
100     public static int httpGetStatus(String url) throws Exception {
101         int iStatus = 500;
102         CloseableHttpClient httpClient = HttpClients.createDefault();
103
104
105         HttpGet httpGet = new HttpGet(url);
106         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
107         httpGet.setConfig(requestConfig);
108         httpGet.addHeader("Content-type", "application/json; charset=utf-8");
109         httpGet.setHeader("Accept", "application/json");
110         try {
111             CloseableHttpResponse res = httpClient.execute(httpGet);
112
113             iStatus = res.getStatusLine().getStatusCode();
114             res.close();
115         } catch (ClientProtocolException e) {
116             logger.error(url + " httpGet connect faild:" + e.getMessage());
117         } catch (IOException e) {
118             logger.error(url + " httpGet connect faild:" + e.getMessage());
119         } finally {
120             try {
121                 httpClient.close();
122             } catch (IOException e) {
123                 logger.error(url + " httpGet close faild:" + e.getMessage());
124             }
125         }
126
127         return iStatus;
128
129     }
130 }