Modify groupId for ems driver
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / nfvo / emsdriver / northbound / client / HttpClientUtil.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.nfvo.emsdriver.northbound.client;
17
18 import java.io.IOException;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.http.HttpEntity;
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.entity.StringEntity;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.util.EntityUtils;
30
31 /*
32  * HttpClient post request
33  */
34 public class HttpClientUtil {
35         
36         private static Log log = LogFactory.getLog(HttpClientUtil.class);
37         
38     public static String doPost(String url,String json,String charset){
39         CloseableHttpClient httpClient = null;
40         HttpPost httpPost = null;
41         String result = null;
42         try{
43             httpClient = HttpClientFactory.getSSLClientFactory();
44             httpPost = new HttpPost(url);
45             if (null != json) {
46                                 StringEntity s = new StringEntity(json);
47                                 s.setContentEncoding("UTF-8");
48                                 s.setContentType("application/json"); // set contentType
49                                 httpPost.setEntity(s);
50                         }
51             CloseableHttpResponse response = httpClient.execute(httpPost);
52             try {
53                                 if(response != null){
54                                     HttpEntity resEntity = response.getEntity();
55                                     if(resEntity != null){
56                                         result = EntityUtils.toString(resEntity,charset);
57                                     }
58                                 }
59                         } catch (Exception e) {
60                                 log.error("httpClient.execute(httpPost) is fail",e);
61                         }finally{
62                                 if(response != null){
63                                         response.close();
64                                 }
65                         }
66         }catch(Exception e){
67                 log.error("doPost is fail ",e);
68         }finally{
69                 if(httpClient != null){
70                         try {
71                                         httpClient.close();
72                                 } catch (IOException e) {
73                                 }
74                 }
75                 
76                 }
77         return result;
78     }
79     
80     public static String doDelete(String url ,String charset){
81         CloseableHttpClient httpClient = null;
82         HttpDelete httpDelete = null;
83         String result = null;
84         try{
85             httpClient = HttpClientFactory.getSSLClientFactory();
86             httpDelete = new HttpDelete(url);
87             
88             CloseableHttpResponse response = httpClient.execute(httpDelete);
89             
90             try {
91                                 if(response != null){
92                                     HttpEntity resEntity = response.getEntity();
93                                     if(resEntity != null){
94                                         result = EntityUtils.toString(resEntity,charset);
95                                     }
96                                 }
97                         } catch (Exception e) {
98                                 log.error("",e);
99                         }finally{
100                                 if(response != null){
101                                         response.close();
102                                 }
103                         }
104         }catch(Exception e){
105                 log.error("doDelete is fail ",e);
106         }finally{
107                 if(httpClient != null){
108                         try {
109                                         httpClient.close();
110                                 } catch (IOException e) {
111                                 }
112                         }
113         }
114         return result;
115     }
116     
117     public static String doGet(String url, String charset){
118         CloseableHttpClient httpClient = null;
119         HttpGet httpGet = null;
120         String result = null;
121         try{
122             httpClient = HttpClientFactory.getSSLClientFactory();
123             httpGet = new HttpGet(url);
124             
125             CloseableHttpResponse response = httpClient.execute(httpGet);
126             
127             try {
128                                 if(response != null){
129                                     HttpEntity resEntity = response.getEntity();
130                                     if(resEntity != null){
131                                         result = EntityUtils.toString(resEntity,charset);
132                                     }
133                                 }
134                         } catch (Exception e) {
135                                 log.error("",e);
136                         }finally{
137                                 if(response != null){
138                                         response.close();
139                                 }
140                         }
141         }catch(Exception e){
142                 log.error("doGet is fail ",e);
143         }finally{
144                 if(httpClient != null){
145                         try {
146                                         httpClient.close();
147                                 } catch (IOException e) {
148                                 }
149                         }
150         }
151         return result;
152     }
153     
154 }