diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-09-11 02:57:01 +0000 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-09-11 02:57:05 +0000 |
commit | 6f03acfb7ab96ba2363eabf2b5cc0cf63e31fe4f (patch) | |
tree | 517dd53949307adf9eb3ec4145ad7cd361587a10 /apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper | |
parent | baa01e13c73002978a9d19756a5e77e6830b8287 (diff) |
Route service requests to discovery service
Route service registration request to discovery service
Route service discovery request to discovery service
Issue-Id: MSB-73
Change-Id: I88a37a075245f7d6923d96e2b59307d2b1caf4dc
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper')
-rw-r--r-- | apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java index cb80159..163387f 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java @@ -14,13 +14,22 @@ package org.onap.msb.apiroute.wrapper.util; import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +40,91 @@ public class HttpClientUtil { private static int connectionTimeOut = 2 * 1000; + public static HttpPost createHttpPost(String url, String params){ + HttpPost httpPost = new HttpPost(url); + httpPost.addHeader("Content-type", "application/json; charset=utf-8"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + return httpPost; + } + + public static HttpGet createHttpGet(String url){ + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + return httpGet; + } + + public static void closeHttpClient(CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(httpClient + ":close httpClient faild"); + } + } + } + + public static void closeHttpResponse(CloseableHttpResponse response) { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + logger.error(response + ":close response faild"); + } + } + } + + public static CloseableHttpResponse httpGetWithResponse(String url) throws Exception { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + try { + return httpClient.execute(httpGet); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(url + ":close httpClient faild"); + } + } + } + + public static void delete(String url, String parameter) throws Exception { + String result = null; + String baseUrl; + if (parameter != null) { + List<NameValuePair> params = new ArrayList<NameValuePair>(); + params.add(new BasicNameValuePair("serviceName", parameter)); + baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8"); + } else { + baseUrl = url; + } + + CloseableHttpClient httpClient = HttpClients.createDefault();; + try { + + HttpDelete httpDelete = new HttpDelete(baseUrl); + CloseableHttpResponse res = httpClient.execute(httpDelete); + + if (res.getStatusLine().getStatusCode() != 200) { + throw new Exception("delete fail"); + } + + res.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":delete connect faild"; + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":close httpClient faild"; + } + } + + } + public static String httpGet(String url) { String result = null; |