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 String errorMsg = url + ":httpPostWithJSON connect faild";
59 throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
63 } catch (IOException e) {
64 String errorMsg = url + ":close httpClient faild";
65 throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
73 public static void delete(String url, String parameter) throws RouteException {
76 if (parameter != null) {
77 List<NameValuePair> params = new ArrayList<NameValuePair>();
78 params.add(new BasicNameValuePair("serviceName", parameter));
79 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
84 CloseableHttpClient httpClient = HttpClients.createDefault();;
87 HttpDelete httpDelete = new HttpDelete(baseUrl);
88 CloseableHttpResponse res = httpClient.execute(httpDelete);
90 if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) {
91 throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR");
95 } catch (IOException e) {
96 String errorMsg = baseUrl + ":delete connect faild";
97 throwsRouteException(errorMsg, e, "DELETE_CONNECT_FAILD");
101 } catch (IOException e) {
102 String errorMsg = baseUrl + ":close httpClient faild";
103 throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
110 public static String httpGet(String url) throws RouteException {
111 String result = null;
112 CloseableHttpClient httpClient = HttpClients.createDefault();
113 HttpGet httpGet = new HttpGet(url);
114 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
115 httpGet.setHeader("Accept", "application/json");
117 CloseableHttpResponse res = httpClient.execute(httpGet);
118 result = EntityUtils.toString(res.getEntity());
119 if (res.getStatusLine().getStatusCode() != RouteConst.SC_OK) {
120 throw new RouteException(result, "SERVICE_GET_ERR");
123 } catch (ClientProtocolException e) {
124 String errorMsg = url + ":httpGetWithJSON connect faild";
125 throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD");
126 } catch (IOException e) {
127 String errorMsg = url + ":httpGetWithJSON connect faild";
128 throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD");
132 } catch (IOException e) {
133 String errorMsg = url + ":close httpClient faild";
134 throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
143 public static String httpPutWithJSON(String url, String params) throws RouteException {
144 String result = null;
145 CloseableHttpClient httpClient = HttpClients.createDefault();
146 HttpPut httpPut = new HttpPut(url);
147 httpPut.addHeader("Content-type", "application/json; charset=utf-8");
148 httpPut.setHeader("Accept", "application/json");
149 httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
151 CloseableHttpResponse res = httpClient.execute(httpPut);
152 result = EntityUtils.toString(res.getEntity());
153 if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) {
154 throw new RouteException(result, "SERVICE_GET_ERR");
157 } catch (IOException e) {
158 String errorMsg = url + ":httpPostWithJSON connect faild";
159 throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
163 } catch (IOException e) {
164 String errorMsg = url + ":close httpClient faild";
165 throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
175 private static void throwsRouteException(String errorMsg, Exception e, String errorCode)
176 throws RouteException {
177 String msg = errorMsg + ".errorMsg:" + e.getMessage();
179 throw new RouteException(errorMsg, errorCode);