1 /*******************************************************************************
2 * Copyright 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.sdk.discovery.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.NameValuePair;
22 import org.apache.http.client.ClientProtocolException;
23 import org.apache.http.client.methods.CloseableHttpResponse;
24 import org.apache.http.client.methods.HttpDelete;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.client.methods.HttpPost;
27 import org.apache.http.client.methods.HttpPut;
28 import org.apache.http.client.utils.URLEncodedUtils;
29 import org.apache.http.entity.StringEntity;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.apache.http.impl.client.HttpClients;
32 import org.apache.http.message.BasicNameValuePair;
33 import org.apache.http.util.EntityUtils;
34 import org.onap.msb.sdk.discovery.common.RouteConst;
35 import org.onap.msb.sdk.discovery.common.RouteException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class HttpClientUtil {
41 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
43 public static String httpPostWithJSON(String url, String params) throws RouteException {
45 CloseableHttpClient httpClient = HttpClients.createDefault();
46 HttpPost httpPost = new HttpPost(url);
47 httpPost.addHeader("Content-type", "application/json; charset=utf-8");
48 httpPost.setHeader("Accept", "application/json");
49 httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
51 CloseableHttpResponse res = httpClient.execute(httpPost);
52 result = EntityUtils.toString(res.getEntity());
53 if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) {
54 throw new RouteException(result, "SERVICE_GET_ERR");
57 } catch (IOException e) {
58 throwsRouteException(url + ":httpPostWithJSON connect faild", e, "POST_CONNECT_FAILD");
62 } catch (IOException e) {
63 throwsRouteException(url + ":close httpClient faild", e, "CLOSE_CONNECT_FAILD");
71 public static void delete(String url, String parameter) throws RouteException {
74 if (parameter != null) {
75 List<NameValuePair> params = new ArrayList<NameValuePair>();
76 params.add(new BasicNameValuePair("serviceName", parameter));
77 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
82 CloseableHttpClient httpClient = HttpClients.createDefault();;
85 HttpDelete httpDelete = new HttpDelete(baseUrl);
86 CloseableHttpResponse res = httpClient.execute(httpDelete);
88 if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) {
89 throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR");
93 } catch (IOException e) {
94 throwsRouteException(baseUrl + ":delete connect faild", e, "DELETE_CONNECT_FAILD");
98 } catch (IOException e) {
99 throwsRouteException(baseUrl + ":close httpClient faild", e, "CLOSE_CONNECT_FAILD");
106 public static String httpGet(String url) throws RouteException {
107 String result = null;
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 CloseableHttpResponse res = httpClient.execute(httpGet);
114 result = EntityUtils.toString(res.getEntity());
115 if (res.getStatusLine().getStatusCode() != RouteConst.SC_OK) {
116 throw new RouteException(result, "SERVICE_GET_ERR");
119 } catch (ClientProtocolException e) {
120 throwsRouteException(url + ":httpGetWithJSON connect faild", e, "GET_CONNECT_FAILD");
121 } catch (IOException e) {
122 throwsRouteException(url + ":httpGetWithJSON connect faild", e, "GET_CONNECT_FAILD");
126 } catch (IOException e) {
127 throwsRouteException(url + ":close httpClient faild", e, "CLOSE_CONNECT_FAILD");
136 public static String httpPutWithJSON(String url, String params) throws RouteException {
137 String result = null;
138 CloseableHttpClient httpClient = HttpClients.createDefault();
139 HttpPut httpPut = new HttpPut(url);
140 httpPut.addHeader("Content-type", "application/json; charset=utf-8");
141 httpPut.setHeader("Accept", "application/json");
142 httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
144 CloseableHttpResponse res = httpClient.execute(httpPut);
145 result = EntityUtils.toString(res.getEntity());
146 if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) {
147 throw new RouteException(result, "SERVICE_GET_ERR");
150 } catch (IOException e) {
151 String errorMsg = url + ":httpPostWithJSON connect faild";
152 throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
156 } catch (IOException e) {
157 String errorMsg = url + ":close httpClient faild";
158 throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
168 private static void throwsRouteException(String errorMsg, Exception e, String errorCode)
169 throws RouteException {
170 String msg = errorMsg + ".errorMsg:" + e.getMessage();
172 throw new RouteException(errorMsg, errorCode);