1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import java.io.IOException;
17 import java.nio.charset.Charset;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.apache.http.HttpMessage;
22 import org.apache.http.NameValuePair;
23 import org.apache.http.client.ClientProtocolException;
24 import org.apache.http.client.config.RequestConfig;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.client.methods.HttpPut;
31 import org.apache.http.client.utils.URLEncodedUtils;
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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class HttpClientUtil {
42 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
44 private static int connectionTimeOut = 2 * 1000;
46 public static HttpPost createHttpPost(String url, Object bean) throws Exception {
47 HttpPost httpPost = new HttpPost(url);
48 setCommonHeader(httpPost);
49 setStringEntity(httpPost, bean);
53 public static HttpGet createHttpGet(String url) {
54 HttpGet httpGet = new HttpGet(url);
55 setCommonHeader(httpGet);
59 public static HttpPut createHttpPut(String url, Object bean) throws Exception {
60 HttpPut httpPut = new HttpPut(url);
61 setCommonHeader(httpPut);
62 setStringEntity(httpPut, bean);
66 public static HttpPut createHttpPut(String url) throws Exception {
67 HttpPut httpPut = new HttpPut(url);
68 setCommonHeader(httpPut);
72 private static void setCommonHeader(HttpMessage httpMessage) {
73 httpMessage.addHeader("Content-type", "application/json; charset=utf-8");
74 httpMessage.setHeader("Accept", "application/json");
77 private static void setStringEntity(HttpEntityEnclosingRequestBase httpMessage, Object bean) throws Exception {
78 String entity = JacksonJsonUtil.beanToJson(bean);
79 httpMessage.setEntity(new StringEntity(entity, Charset.forName("UTF-8")));
82 public static void closeHttpClient(CloseableHttpClient httpClient, CloseableHttpResponse response) {
83 closeHttpClient(httpClient);
84 closeHttpResponse(response);
87 private static void closeHttpClient(CloseableHttpClient httpClient) {
88 if (httpClient != null) {
91 } catch (IOException e) {
92 logger.error(httpClient + ":close httpClient faild");
97 private static void closeHttpResponse(CloseableHttpResponse response) {
98 if (response != null) {
101 } catch (IOException e) {
102 logger.error(response + ":close response faild");
107 public static CloseableHttpResponse httpGetWithResponse(String url) throws Exception {
108 CloseableHttpClient httpClient = HttpClients.createDefault();
109 HttpGet httpGet = new HttpGet(url);
110 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
111 httpGet.setHeader("Accept", "application/json");
113 return httpClient.execute(httpGet);
117 } catch (IOException e) {
118 logger.error(url + ":close httpClient faild");
123 public static void delete(String url, String parameter) throws Exception {
124 String result = null;
126 if (parameter != null) {
127 List<NameValuePair> params = new ArrayList<NameValuePair>();
128 params.add(new BasicNameValuePair("serviceName", parameter));
129 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
134 CloseableHttpClient httpClient = HttpClients.createDefault();;
137 HttpDelete httpDelete = new HttpDelete(baseUrl);
138 CloseableHttpResponse res = httpClient.execute(httpDelete);
140 if (res.getStatusLine().getStatusCode() != 200) {
141 throw new Exception("delete fail");
145 } catch (IOException e) {
146 String errorMsg = baseUrl + ":delete connect faild";
150 } catch (IOException e) {
151 String errorMsg = baseUrl + ":close httpClient faild";
158 public static String httpGet(String url) {
159 String result = null;
160 CloseableHttpClient httpClient = HttpClients.createDefault();
161 HttpGet httpGet = new HttpGet(url);
162 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
163 httpGet.setHeader("Accept", "application/json");
165 CloseableHttpResponse res = httpClient.execute(httpGet);
166 result = EntityUtils.toString(res.getEntity());
167 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
168 logger.error(result);
171 } catch (ClientProtocolException e) {
172 logger.error(url + ":httpGetWithJSON connect faild");
173 } catch (IOException e) {
174 logger.error(url + ":httpGetWithJSON connect faild");
178 } catch (IOException e) {
179 logger.error(url + ":close httpClient faild");
187 public static HttpGetResult httpGetStatusAndBody(String url) {
188 HttpGetResult result = new HttpGetResult();
190 CloseableHttpClient httpClient = HttpClients.createDefault();
191 HttpGet httpGet = new HttpGet(url);
192 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
193 httpGet.setHeader("Accept", "application/json");
195 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
196 httpGet.setConfig(requestConfig);
199 CloseableHttpResponse res = httpClient.execute(httpGet);
200 body = EntityUtils.toString(res.getEntity());
201 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
204 result.setBody(body);
205 result.setStatusCode(res.getStatusLine().getStatusCode());
207 } catch (ClientProtocolException e) {
208 logger.error(url + ":httpGetWithJSON connect faild", e);
209 } catch (IOException e) {
210 logger.error(url + ":httpGetWithJSON connect faild", e);
214 } catch (IOException e) {
215 logger.error(url + ":close httpClient faild");
223 public static int httpGetStatus(String url) throws Exception {
225 CloseableHttpClient httpClient = HttpClients.createDefault();
228 HttpGet httpGet = new HttpGet(url);
229 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
230 httpGet.setConfig(requestConfig);
231 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
232 httpGet.setHeader("Accept", "application/json");
234 CloseableHttpResponse res = httpClient.execute(httpGet);
236 iStatus = res.getStatusLine().getStatusCode();
238 } catch (ClientProtocolException e) {
239 logger.error(url + " httpGet connect faild:" + e.getMessage());
240 } catch (IOException e) {
241 logger.error(url + " httpGet connect faild:" + e.getMessage());
245 } catch (IOException e) {
246 logger.error(url + " httpGet close faild:" + e.getMessage());