1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.broadcastproxy.business.providers.dolist;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.util.Map;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.apache.http.HttpHost;
43 import org.apache.http.HttpResponse;
44 import org.apache.http.client.config.RequestConfig;
45 import org.apache.http.client.methods.HttpDelete;
46 import org.apache.http.client.methods.HttpGet;
47 import org.apache.http.client.methods.HttpPost;
48 import org.apache.http.client.methods.HttpPut;
49 import org.apache.http.entity.StringEntity;
50 import org.apache.http.impl.client.CloseableHttpClient;
51 import org.apache.http.impl.client.HttpClientBuilder;
52
53 import fr.paris.lutece.portal.service.util.AppException;
54 import fr.paris.lutece.portal.service.util.AppLogService;
55 import fr.paris.lutece.portal.service.util.AppPropertiesService;
56
57 public class DolistHttpAccess
58 {
59
60 private static final String PROXY_ADR = AppPropertiesService.getProperty( "broadcastproxy.proxyHost" );
61 private static final int PROXY_PORT = AppPropertiesService.getPropertyInt( "broadcastproxy.proxyPort", 3128 );
62
63 public HttpResponse doGet( String strUrl, Map<String, String> headers )
64 {
65 CloseableHttpClient client = HttpClientBuilder.create( ).build( );
66 HttpGet method = new HttpGet( strUrl );
67 HttpResponse httpResponse = null;
68
69 try
70 {
71
72 if ( headers != null )
73 {
74 for ( String headerType : headers.keySet( ) )
75 {
76 method.setHeader( headerType, headers.get( headerType ) );
77 }
78 }
79
80
81 if( StringUtils.isNotEmpty( PROXY_ADR ) )
82 {
83 HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
84 RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
85 method.setConfig( config );
86 }
87
88 httpResponse = client.execute( method );
89
90 }
91 catch( IOException e )
92 {
93 String strError = "HttpGet - Error connecting to '" + strUrl + "' : ";
94 AppLogService.error( strError + e.getMessage( ), e );
95 throw new AppException( "strError", e );
96 }
97 finally
98 {
99
100 method.releaseConnection( );
101 }
102
103 return httpResponse;
104 }
105
106 public String doPost( String strUrl, String jsonParams, Map<String, String> headers )
107 {
108 CloseableHttpClient client = HttpClientBuilder.create( ).build( );
109 HttpResponse httpResponse = null;
110 String strResponse = null;
111 HttpPost method = new HttpPost( strUrl );
112
113 try
114 {
115
116 if ( headers != null )
117 {
118 for ( String headerType : headers.keySet( ) )
119 {
120 method.setHeader( headerType, headers.get( headerType ) );
121 }
122 }
123
124 StringEntity entity = new StringEntity( jsonParams );
125 method.setEntity( entity );
126
127
128 if( StringUtils.isNotEmpty( PROXY_ADR ) )
129 {
130 HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
131 RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
132 method.setConfig( config );
133 }
134 httpResponse = client.execute( method );
135
136
137 if ( httpResponse != null && httpResponse.getStatusLine( ).getStatusCode( ) < 200
138 && httpResponse.getStatusLine( ).getStatusCode( ) >= 300 )
139 {
140 AppLogService.error( "Returned Dolist error code : " + httpResponse.getStatusLine( ).getStatusCode( ) );
141 }
142
143
144 strResponse = httpToStrResponse( httpResponse );
145 }
146 catch( IOException e )
147 {
148 String strError = "HttpPost - Error connecting to '" + strUrl + "' : ";
149 AppLogService.error( strError + e.getMessage( ), e );
150 throw new AppException( "strError", e );
151 }
152 finally
153 {
154
155 method.releaseConnection( );
156 }
157
158 return strResponse;
159 }
160
161 public String doPut( String strUrl, String jsonParams, Map<String, String> headers )
162 {
163 CloseableHttpClient client = HttpClientBuilder.create( ).build( );
164 HttpResponse httpResponse = null;
165 String strResponse = null;
166 HttpPut method = new HttpPut( strUrl );
167
168 try
169 {
170
171 if ( headers != null )
172 {
173 for ( String headerType : headers.keySet( ) )
174 {
175 method.setHeader( headerType, headers.get( headerType ) );
176 }
177 }
178
179 StringEntity entity = new StringEntity( jsonParams );
180 method.setEntity( entity );
181
182
183 if( StringUtils.isNotEmpty( PROXY_ADR ) )
184 {
185 HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
186 RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
187 method.setConfig( config );
188 }
189
190 httpResponse = client.execute( method );
191
192
193 if ( httpResponse != null && httpResponse.getStatusLine( ).getStatusCode( ) < 200
194 && httpResponse.getStatusLine( ).getStatusCode( ) >= 300 )
195 {
196 AppLogService.error( "Returned Dolist error code : " + httpResponse.getStatusLine( ).getStatusCode( ) );
197 }
198
199
200 strResponse = httpToStrResponse( httpResponse );
201 }
202 catch( IOException e )
203 {
204 String strError = "HttpPut - Error connecting to '" + strUrl + "' : ";
205 AppLogService.error( strError + e.getMessage( ), e );
206 throw new AppException( "strError", e );
207 }
208 finally
209 {
210
211 method.releaseConnection( );
212 }
213
214 return strResponse;
215 }
216
217 public HttpResponse doDelete( String strUrl, Map<String, String> headers )
218 {
219 CloseableHttpClient client = HttpClientBuilder.create( ).build( );
220 HttpResponse httpResponse = null;
221 HttpDelete method = new HttpDelete( strUrl );
222
223 try
224 {
225
226 if ( headers != null )
227 {
228 for ( String headerType : headers.keySet( ) )
229 {
230 method.setHeader( headerType, headers.get( headerType ) );
231 }
232 }
233
234 method.setHeader( "Content-Type", "application/x-www-form-urlencoded" );
235
236
237 if( StringUtils.isNotEmpty( PROXY_ADR ) )
238 {
239 HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
240 RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
241 method.setConfig( config );
242 }
243
244 httpResponse = client.execute( method );
245 }
246 catch( IOException e )
247 {
248 String strError = "HttpDelete - Error connecting to '" + strUrl + "' : ";
249 AppLogService.error( strError + e.getMessage( ), e );
250 throw new AppException( "strError", e );
251 }
252 finally
253 {
254
255 method.releaseConnection( );
256 }
257
258 return httpResponse;
259 }
260
261
262
263
264
265
266
267
268 private String httpToStrResponse( HttpResponse httpResponse ) throws IOException
269 {
270 StringBuilder strResponse = new StringBuilder( );
271
272
273 if ( httpResponse != null && httpResponse.getEntity( ) != null && httpResponse.getEntity( ).getContent( ) != null )
274 {
275 BufferedReader bufferedreader = new BufferedReader( new InputStreamReader( httpResponse.getEntity( ).getContent( ) ) );
276 String line = "";
277 while ( ( line = bufferedreader.readLine( ) ) != null )
278 {
279 strResponse.append( line );
280 }
281 }
282
283 return strResponse.toString( );
284 }
285 }