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
14 package org.onap.msb.sdclient.wrapper.util;
16 import java.io.IOException;
17 import java.math.BigInteger;
18 import java.nio.charset.Charset;
19 import java.util.ArrayList;
20 import java.util.List;
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.HttpGet;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.client.methods.HttpPut;
30 import org.apache.http.client.utils.URLEncodedUtils;
31 import org.apache.http.entity.StringEntity;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClients;
34 import org.apache.http.message.BasicNameValuePair;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.msb.sdclient.core.ConsulResponse;
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 public static int httpPutWithJSON(String url, String params) {
46 CloseableHttpClient httpClient = HttpClients.createDefault();
47 HttpPut httpPut = new HttpPut(url);
48 httpPut.addHeader("Content-type", "application/json; charset=utf-8");
49 httpPut.setHeader("Accept", "application/json");
50 httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
52 CloseableHttpResponse res = httpClient.execute(httpPut);
53 result = res.getStatusLine().getStatusCode();
54 if (res.getStatusLine().getStatusCode() != 200) {
55 logger.error(String.valueOf(result));
58 } catch (IOException e) {
59 String errorMsg = url + ":httpPutWithJSON connect faild";
63 } catch (IOException e) {
64 String errorMsg = url + ":close httpClient faild";
72 public static int httpPostWithJSON(String url, String params) {
74 CloseableHttpClient httpClient = HttpClients.createDefault();
75 HttpPost httpPost = new HttpPost(url);
76 httpPost.addHeader("Content-type", "application/json; charset=utf-8");
77 httpPost.setHeader("Accept", "application/json");
78 httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
80 CloseableHttpResponse res = httpClient.execute(httpPost);
81 result = res.getStatusLine().getStatusCode();
82 if (res.getStatusLine().getStatusCode() != 200) {
83 logger.error(String.valueOf(result));
86 } catch (IOException e) {
87 String errorMsg = url + ":httpPostWithJSON connect faild";
91 } catch (IOException e) {
92 String errorMsg = url + ":close httpClient faild";
100 public static void delete(String url, String parameter) throws Exception {
101 String result = null;
103 if (parameter != null) {
104 List<NameValuePair> params = new ArrayList<NameValuePair>();
105 params.add(new BasicNameValuePair("serviceName", parameter));
106 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
111 CloseableHttpClient httpClient = HttpClients.createDefault();;
114 HttpDelete httpDelete = new HttpDelete(baseUrl);
115 CloseableHttpResponse res = httpClient.execute(httpDelete);
117 if (res.getStatusLine().getStatusCode() != 200) {
118 throw new Exception("delete fail");
122 } catch (IOException e) {
123 String errorMsg = baseUrl + ":delete connect faild";
127 } catch (IOException e) {
128 String errorMsg = baseUrl + ":close httpClient faild";
134 public static String httpGet(String url) {
135 String result = null;
136 CloseableHttpClient httpClient = HttpClients.createDefault();
137 HttpGet httpGet = new HttpGet(url);
138 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
139 httpGet.setHeader("Accept", "application/json");
141 CloseableHttpResponse res = httpClient.execute(httpGet);
143 res.getLastHeader("X-Consul-Index");
144 result = EntityUtils.toString(res.getEntity());
145 if (res.getStatusLine().getStatusCode() != 200) {
146 logger.error(result);
149 } catch (ClientProtocolException e) {
150 String errorMsg = url + ":httpGetWithJSON connect faild";
151 logger.error(errorMsg);
152 } catch (IOException e) {
153 String errorMsg = url + ":httpGetWithJSON connect faild";
154 logger.error(errorMsg);
158 } catch (IOException e) {
159 String errorMsg = url + ":close httpClient faild";
160 logger.error(errorMsg);
168 @SuppressWarnings("unchecked")
169 public static <T> ConsulResponse<T> httpWaitGet(String url) {
170 CloseableHttpClient httpClient = HttpClients.createDefault();
171 HttpGet httpGet = new HttpGet(url);
172 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
173 httpGet.setHeader("Accept", "application/json");
175 CloseableHttpResponse res = httpClient.execute(httpGet);
176 String result = EntityUtils.toString(res.getEntity());
178 if (res.getStatusLine().getStatusCode() != 200) {
179 logger.error(result);
181 String indexHeaderValue = res.getLastHeader("X-Consul-Index").getValue();
182 BigInteger index = new BigInteger(indexHeaderValue);
184 return new ConsulResponse<T>((T) result, index);
189 } catch (ClientProtocolException e) {
190 String errorMsg = url + ":httpGetWithJSON connect faild " + e.getMessage();
191 logger.error(errorMsg);
192 } catch (IOException e) {
193 String errorMsg = url + ":httpGetWithJSON connect faild " + e.getMessage();
194 logger.error(errorMsg);
198 } catch (IOException e) {
199 String errorMsg = url + ":close httpClient faild " + e.getMessage();
200 logger.error(errorMsg);
208 public static int httpGetStatus(String url) throws Exception {
210 CloseableHttpClient httpClient = HttpClients.createDefault();
212 HttpGet httpGet = new HttpGet(url);
213 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
214 httpGet.setConfig(requestConfig);
215 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
216 httpGet.setHeader("Accept", "application/json");
218 CloseableHttpResponse res = httpClient.execute(httpGet);
220 iStatus = res.getStatusLine().getStatusCode();
222 } catch (ClientProtocolException e) {
223 logger.error(url + " httpGet connect faild:" + e.getMessage());
224 } catch (IOException e) {
225 logger.error(url + " httpGet connect faild:" + e.getMessage());
229 } catch (IOException e) {
230 logger.error(url + " httpGet close faild:" + e.getMessage());