Merge "50% Code Coverage-MSB Java SDK"
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / util / HttpClientUtil.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.discovery.util;
15
16 import java.io.IOException;
17 import java.nio.charset.Charset;
18 import java.util.ArrayList;
19 import java.util.List;
20
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;
38
39 public class HttpClientUtil {
40
41   private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
42
43   public static String httpPostWithJSON(String url, String params) throws RouteException {
44     String result = null;
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")));
50     try {
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");
55       }
56       res.close();
57     } catch (IOException e) {
58       throwsRouteException(url + ":httpPostWithJSON connect faild", e, "POST_CONNECT_FAILD");
59     } finally {
60       try {
61         httpClient.close();
62       } catch (IOException e) {
63         throwsRouteException(url + ":close  httpClient faild", e, "CLOSE_CONNECT_FAILD");
64       }
65     }
66
67     return result;
68
69   }
70
71   public static void delete(String url, String parameter) throws RouteException {
72     String result = null;
73     String baseUrl;
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");
78     } else {
79       baseUrl = url;
80     }
81
82     CloseableHttpClient httpClient = HttpClients.createDefault();;
83     try {
84
85       HttpDelete httpDelete = new HttpDelete(baseUrl);
86       CloseableHttpResponse res = httpClient.execute(httpDelete);
87
88       if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) {
89         throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR");
90       }
91
92       res.close();
93     } catch (IOException e) {
94       throwsRouteException(baseUrl + ":delete connect faild", e, "DELETE_CONNECT_FAILD");
95     } finally {
96       try {
97         httpClient.close();
98       } catch (IOException e) {
99         throwsRouteException(baseUrl + ":close  httpClient faild", e, "CLOSE_CONNECT_FAILD");
100       }
101     }
102
103
104   }
105
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");
112     try {
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");
117       }
118       res.close();
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");
123     } finally {
124       try {
125         httpClient.close();
126       } catch (IOException e) {
127         throwsRouteException(url + ":close  httpClient faild", e, "CLOSE_CONNECT_FAILD");
128       }
129     }
130
131     return result;
132
133   }
134
135
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")));
143     try {
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");
148       }
149       res.close();
150     } catch (IOException e) {
151       String errorMsg = url + ":httpPostWithJSON connect faild";
152       throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
153     } finally {
154       try {
155         httpClient.close();
156       } catch (IOException e) {
157         String errorMsg = url + ":close  httpClient faild";
158         throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
159       }
160     }
161
162     return result;
163
164   }
165
166
167
168   private static void throwsRouteException(String errorMsg, Exception e, String errorCode)
169       throws RouteException {
170     String msg = errorMsg + ".errorMsg:" + e.getMessage();
171     logger.error(msg);
172     throw new RouteException(errorMsg, errorCode);
173   }
174
175 }