View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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      // get proxy from HttpAccess properties
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              // add request header
72              if ( headers != null )
73              {
74                  for ( String headerType : headers.keySet( ) )
75                  {
76                      method.setHeader( headerType, headers.get( headerType ) );
77                  }
78              }
79  
80              // add proxy
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              // Execute method
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              // Release the connection.
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             // Add headershttpPost
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             // add proxy
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             // If error
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             // Get response in String
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             // Release the connection.
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             // Add headershttpPut
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             // add proxy
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             // If error
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             // Get response in String
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             // Release the connection.
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             // Add headers
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             // add proxy
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             // Release the connection.
255             method.releaseConnection( );
256         }
257 
258         return httpResponse;
259     }
260 
261     /**
262      * Stringify httpResponse
263      * 
264      * @param httpResponse
265      * @return the response as string
266      * @throws IOException
267      */
268     private String httpToStrResponse( HttpResponse httpResponse ) throws IOException
269     {
270         StringBuilder strResponse = new StringBuilder( );
271 
272         // Get response data in string
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 }