add license file for CII Badge application
[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       String errorMsg = url + ":httpPostWithJSON connect faild";
59       throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
60     } finally {
61       try {
62         httpClient.close();
63       } catch (IOException e) {
64         String errorMsg = url + ":close  httpClient faild";
65         throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
66       }
67     }
68
69     return result;
70
71   }
72
73   public static void delete(String url, String parameter) throws RouteException {
74     String result = null;
75     String baseUrl;
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");
80     } else {
81       baseUrl = url;
82     }
83
84     CloseableHttpClient httpClient = HttpClients.createDefault();;
85     try {
86
87       HttpDelete httpDelete = new HttpDelete(baseUrl);
88       CloseableHttpResponse res = httpClient.execute(httpDelete);
89
90       if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) {
91         throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR");
92       }
93
94       res.close();
95     } catch (IOException e) {
96       String errorMsg = baseUrl + ":delete connect faild";
97       throwsRouteException(errorMsg, e, "DELETE_CONNECT_FAILD");
98     } finally {
99       try {
100         httpClient.close();
101       } catch (IOException e) {
102         String errorMsg = baseUrl + ":close  httpClient faild";
103         throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
104       }
105     }
106
107
108   }
109
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");
116     try {
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");
121       }
122       res.close();
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");
129     } finally {
130       try {
131         httpClient.close();
132       } catch (IOException e) {
133         String errorMsg = url + ":close  httpClient faild";
134         throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
135       }
136     }
137
138     return result;
139
140   }
141
142
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")));
150     try {
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");
155       }
156       res.close();
157     } catch (IOException e) {
158       String errorMsg = url + ":httpPostWithJSON connect faild";
159       throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD");
160     } finally {
161       try {
162         httpClient.close();
163       } catch (IOException e) {
164         String errorMsg = url + ":close  httpClient faild";
165         throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD");
166       }
167     }
168
169     return result;
170
171   }
172
173
174
175   private static void throwsRouteException(String errorMsg, Exception e, String errorCode)
176       throws RouteException {
177     String msg = errorMsg + ".errorMsg:" + e.getMessage();
178     logger.error(msg);
179     throw new RouteException(errorMsg, errorCode);
180   }
181
182 }