View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.IOException;
37  import java.util.Map;
38  
39  import org.apache.http.HttpHost;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.client.config.RequestConfig;
42  import org.apache.http.client.methods.HttpDelete;
43  import org.apache.http.client.methods.HttpGet;
44  import org.apache.http.client.methods.HttpPost;
45  import org.apache.http.client.methods.HttpPut;
46  import org.apache.http.entity.StringEntity;
47  import org.apache.http.impl.client.CloseableHttpClient;
48  import org.apache.http.impl.client.HttpClientBuilder;
49  
50  import fr.paris.lutece.portal.service.util.AppException;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  public class DolistHttpAccess
55  {
56      // get proxy from HttpAccess properties
57      private static final String PROXY_ADR = AppPropertiesService.getProperty( "broadcastproxy.proxyHost" );
58      private static final int PROXY_PORT = AppPropertiesService.getPropertyInt( "broadcastproxy.proxyPort", 3128 );
59  
60      public HttpResponse doGet( String strUrl, Map<String, String> headers )
61      {
62          CloseableHttpClient client = HttpClientBuilder.create( ).build( );
63          HttpGet method = new HttpGet( strUrl );
64          HttpResponse httpResponse = null;
65  
66          try
67          {
68              // add request header
69              if ( headers != null )
70              {
71                  for ( String headerType : headers.keySet( ) )
72                  {
73                      method.setHeader( headerType, headers.get( headerType ) );
74                  }
75              }
76  
77              // add proxy
78              HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
79              RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
80              method.setConfig( config );
81  
82              // Execute method
83              httpResponse = client.execute( method );
84  
85          }
86          catch( IOException e )
87          {
88              String strError = "HttpGet - Error connecting to '" + strUrl + "' : ";
89              AppLogService.error( strError + e.getMessage( ), e );
90              throw new AppException( "strError", e );
91          }
92          finally
93          {
94              // Release the connection.
95              method.releaseConnection( );
96          }
97  
98          return httpResponse;
99      }
100 
101     public HttpResponse doPost( String strUrl, String jsonParams, Map<String, String> headers )
102     {
103         CloseableHttpClient client = HttpClientBuilder.create( ).build( );
104         HttpResponse httpResponse = null;
105         HttpPost method = new HttpPost( strUrl );
106 
107         try
108         {
109             // Add headershttpPost
110             if ( headers != null )
111             {
112                 for ( String headerType : headers.keySet( ) )
113                 {
114                     method.setHeader( headerType, headers.get( headerType ) );
115                 }
116             }
117 
118             StringEntity entity = new StringEntity( jsonParams );
119             method.setEntity( entity );
120 
121             // add proxy
122             HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
123             RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
124             method.setConfig( config );
125 
126             httpResponse = client.execute( method );
127         }
128         catch( IOException e )
129         {
130             String strError = "HttpPost - Error connecting to '" + strUrl + "' : ";
131             AppLogService.error( strError + e.getMessage( ), e );
132             throw new AppException( "strError", e );
133         }
134         finally
135         {
136             // Release the connection.
137             method.releaseConnection( );
138         }
139 
140         return httpResponse;
141     }
142 
143     public HttpResponse doPut( String strUrl, String jsonParams, Map<String, String> headers )
144     {
145         CloseableHttpClient client = HttpClientBuilder.create( ).build( );
146         HttpResponse httpResponse = null;
147         HttpPut method = new HttpPut( strUrl );
148 
149         try
150         {
151             // Add headershttpPut
152             if ( headers != null )
153             {
154                 for ( String headerType : headers.keySet( ) )
155                 {
156                     method.setHeader( headerType, headers.get( headerType ) );
157                 }
158             }
159 
160             StringEntity entity = new StringEntity( jsonParams );
161             method.setEntity( entity );
162 
163             // add proxy
164             HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
165             RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
166             method.setConfig( config );
167 
168             httpResponse = client.execute( method );
169         }
170         catch( IOException e )
171         {
172             String strError = "HttpPut - Error connecting to '" + strUrl + "' : ";
173             AppLogService.error( strError + e.getMessage( ), e );
174             throw new AppException( "strError", e );
175         }
176         finally
177         {
178             // Release the connection.
179             method.releaseConnection( );
180         }
181 
182         return httpResponse;
183     }
184 
185     public HttpResponse doDelete( String strUrl, Map<String, String> headers )
186     {
187         CloseableHttpClient client = HttpClientBuilder.create( ).build( );
188         HttpResponse httpResponse = null;
189         HttpDelete method = new HttpDelete( strUrl );
190 
191         try
192         {
193             // Add headers
194             if ( headers != null )
195             {
196                 for ( String headerType : headers.keySet( ) )
197                 {
198                     method.setHeader( headerType, headers.get( headerType ) );
199                 }
200             }
201 
202             method.setHeader( "Content-Type", "application/x-www-form-urlencoded" );
203 
204             // add proxy
205             HttpHost proxy = new HttpHost( PROXY_ADR, PROXY_PORT );
206             RequestConfig config = RequestConfig.custom( ).setProxy( proxy ).build( );
207             method.setConfig( config );
208 
209             httpResponse = client.execute( method );
210         }
211         catch( IOException e )
212         {
213             String strError = "HttpDelete - Error connecting to '" + strUrl + "' : ";
214             AppLogService.error( strError + e.getMessage( ), e );
215             throw new AppException( "strError", e );
216         }
217         finally
218         {
219             // Release the connection.
220             method.releaseConnection( );
221         }
222 
223         return httpResponse;
224     }
225 }