2 * Copyright 2016 ZTE, Inc. and others.
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.msb.sdclient.wrapper.util;
18 import java.io.IOException;
19 import java.math.BigInteger;
20 import java.nio.charset.Charset;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.apache.http.NameValuePair;
25 import org.apache.http.client.ClientProtocolException;
26 import org.apache.http.client.config.RequestConfig;
27 import org.apache.http.client.methods.CloseableHttpResponse;
28 import org.apache.http.client.methods.HttpDelete;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.client.methods.HttpPost;
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.onap.msb.sdclient.core.ConsulResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class HttpClientUtil {
43 private static final Logger logger = LoggerFactory
44 .getLogger(HttpClientUtil.class);
46 public static int httpPostWithJSON(String url, String params) {
48 CloseableHttpClient httpClient = HttpClients.createDefault();
49 HttpPost httpPost = new HttpPost(url);
50 httpPost.addHeader("Content-type", "application/json; charset=utf-8");
51 httpPost.setHeader("Accept", "application/json");
52 httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
54 CloseableHttpResponse res = httpClient.execute(httpPost);
55 result = res.getStatusLine().getStatusCode();
56 if (res.getStatusLine().getStatusCode() != 200) {
57 logger.error(String.valueOf(result));
60 } catch (IOException e) {
61 String errorMsg = url + ":httpPostWithJSON connect faild";
65 } catch (IOException e) {
66 String errorMsg = url + ":close httpClient faild";
74 public static void delete(String url, String parameter) throws Exception {
77 if (parameter != null) {
78 List<NameValuePair> params = new ArrayList<NameValuePair>();
79 params.add(new BasicNameValuePair("serviceName", parameter));
80 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
85 CloseableHttpClient httpClient = HttpClients.createDefault();
89 HttpDelete httpDelete = new HttpDelete(baseUrl);
90 CloseableHttpResponse res = httpClient.execute(httpDelete);
92 if (res.getStatusLine().getStatusCode() != 200) {
93 throw new Exception("delete fail");
97 } catch (IOException e) {
98 String errorMsg = baseUrl + ":delete connect faild";
102 } catch (IOException e) {
103 String errorMsg = baseUrl + ":close httpClient faild";
109 public static String httpGet(String url) {
110 String result = null;
111 CloseableHttpClient httpClient = HttpClients.createDefault();
112 HttpGet httpGet = new HttpGet(url);
113 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
114 httpGet.setHeader("Accept", "application/json");
116 CloseableHttpResponse res = httpClient.execute(httpGet);
118 res.getLastHeader("X-Consul-Index");
119 result = EntityUtils.toString(res.getEntity());
120 if (res.getStatusLine().getStatusCode() != 200) {
121 logger.error(result);
124 } catch (ClientProtocolException e) {
125 String errorMsg = url + ":httpGetWithJSON connect faild";
126 logger.error(errorMsg);
127 } catch (IOException e) {
128 String errorMsg = url + ":httpGetWithJSON connect faild";
129 logger.error(errorMsg);
133 } catch (IOException e) {
134 String errorMsg = url + ":close httpClient faild";
135 logger.error(errorMsg);
143 @SuppressWarnings("unchecked")
144 public static <T> ConsulResponse<T> httpWaitGet(String url) {
145 CloseableHttpClient httpClient = HttpClients.createDefault();
146 HttpGet httpGet = new HttpGet(url);
147 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
148 httpGet.setHeader("Accept", "application/json");
150 CloseableHttpResponse res = httpClient.execute(httpGet);
151 String result = EntityUtils.toString(res.getEntity());
153 if (res.getStatusLine().getStatusCode() != 200) {
154 logger.error(result);
156 String indexHeaderValue = res.getLastHeader("X-Consul-Index")
158 BigInteger index = new BigInteger(indexHeaderValue);
160 return new ConsulResponse<T>((T) result, index);
165 } catch (ClientProtocolException e) {
166 String errorMsg = url + ":httpGetWithJSON connect faild "
168 logger.error(errorMsg);
169 } catch (IOException e) {
170 String errorMsg = url + ":httpGetWithJSON connect faild "
172 logger.error(errorMsg);
176 } catch (IOException e) {
177 String errorMsg = url + ":close httpClient faild "
179 logger.error(errorMsg);
187 public static int httpGetStatus(String url) throws Exception {
189 CloseableHttpClient httpClient = HttpClients.createDefault();
191 HttpGet httpGet = new HttpGet(url);
192 RequestConfig requestConfig = RequestConfig.custom()
193 .setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
194 httpGet.setConfig(requestConfig);
195 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
196 httpGet.setHeader("Accept", "application/json");
198 CloseableHttpResponse res = httpClient.execute(httpGet);
200 iStatus = res.getStatusLine().getStatusCode();
202 } catch (ClientProtocolException e) {
203 logger.error(url + " httpGet connect faild:" + e.getMessage());
204 } catch (IOException e) {
205 logger.error(url + " httpGet connect faild:" + e.getMessage());
209 } catch (IOException e) {
210 logger.error(url + " httpGet close faild:" + e.getMessage());