maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdca...@apache.org
Subject svn commit: r783053 - in /maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http: AbstractHttpClientWagon.java HttpConfiguration.java HttpMethodConfiguration.java
Date Tue, 09 Jun 2009 16:02:23 GMT
Author: jdcasey
Date: Tue Jun  9 16:02:23 2009
New Revision: 783053

URL: http://svn.apache.org/viewvc?rev=783053&view=rev
Log:
[WAGON-269][WAGON-264] Allow advanced configuration of underlying httpclient instance from
settings.xml, including useDefaultHeaders == false to avoid using the default 'gzip' setting
for Accept-Encoding, and allowing the user to ignore cookies when those cookies are unnecessary
and incorrect.

Added:
    maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java
  (with props)
    maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java
  (with props)
Modified:
    maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java

Modified: maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java?rev=783053&r1=783052&r2=783053&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
(original)
+++ maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
Tue Jun  9 16:02:23 2009
@@ -19,21 +19,6 @@
  * under the License.
  */
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URLEncoder;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Properties;
-import java.util.TimeZone;
-import java.util.zip.GZIPInputStream;
-
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HostConfiguration;
@@ -49,6 +34,7 @@
 import org.apache.commons.httpclient.methods.HeadMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.apache.commons.httpclient.util.DateParseException;
 import org.apache.commons.httpclient.util.DateParser;
 import org.apache.maven.wagon.InputData;
@@ -66,6 +52,21 @@
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Properties;
+import java.util.TimeZone;
+import java.util.zip.GZIPInputStream;
+
 /**
  * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
@@ -143,7 +144,15 @@
 
     protected HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
 
+    /**
+     * @deprecated Use httpConfiguration instead.
+     */
     private Properties httpHeaders;
+    
+    /**
+     * @since 1.0-beta-6
+     */
+    private HttpConfiguration httpConfiguration;
 
     private HttpMethod getMethod;
 
@@ -400,20 +409,44 @@
     protected int execute( HttpMethod httpMethod ) throws HttpException, IOException
     {
         int statusCode = SC_NULL;
-        httpMethod.getParams().setSoTimeout( getTimeout() );
+        
+        setParameters( httpMethod );
         setHeaders( httpMethod );
+        
         statusCode = client.executeMethod( httpMethod );
         return statusCode;
     }
+    
+    protected void setParameters( HttpMethod method )
+    {
+        HttpMethodConfiguration config = httpConfiguration == null ? null : httpConfiguration.getMethodConfiguration(
method );
+        if ( config != null )
+        {
+            HttpMethodParams params = config.asMethodParams( method.getParams() );
+            if ( params != null )
+            {
+                method.setParams( params );
+            }
+        }
+        
+        if ( config == null || config.getConnectionTimeout() == HttpMethodConfiguration.DEFAULT_CONNECTION_TIMEOUT
)
+        {
+            method.getParams().setSoTimeout( getTimeout() );
+        }
+    }
 
     protected void setHeaders( HttpMethod method )
     {
-        // TODO: merge with the other headers and have some better defaults, unify with lightweight
headers
-        method.addRequestHeader( "Cache-control", "no-cache" );
-        method.addRequestHeader( "Cache-store", "no-store" );
-        method.addRequestHeader( "Pragma", "no-cache" );
-        method.addRequestHeader( "Expires", "0" );
-        method.addRequestHeader( "Accept-Encoding", "gzip" );
+        HttpMethodConfiguration config = httpConfiguration == null ? null : httpConfiguration.getMethodConfiguration(
method );
+        if ( config == null || config.isUseDefaultHeaders() )
+        {
+            // TODO: merge with the other headers and have some better defaults, unify with
lightweight headers
+            method.addRequestHeader( "Cache-control", "no-cache" );
+            method.addRequestHeader( "Cache-store", "no-store" );
+            method.addRequestHeader( "Pragma", "no-cache" );
+            method.addRequestHeader( "Expires", "0" );
+            method.addRequestHeader( "Accept-Encoding", "gzip" );
+        }
 
         if ( httpHeaders != null )
         {
@@ -423,6 +456,15 @@
                 method.addRequestHeader( header, httpHeaders.getProperty( header ) );
             }                
         }
+        
+        Header[] headers = config == null ? null : config.asRequestHeaders();
+        if ( headers != null )
+        {
+            for ( int i = 0; i < headers.length; i++ )
+            {
+                method.addRequestHeader( headers[i] );
+            }
+        }
     }
 
     /**
@@ -456,6 +498,16 @@
         this.httpHeaders = httpHeaders;
     }
 
+    public HttpConfiguration getHttpConfiguration()
+    {
+        return httpConfiguration;
+    }
+
+    public void setHttpConfiguration( HttpConfiguration httpConfiguration )
+    {
+        this.httpConfiguration = httpConfiguration;
+    }
+
     public void fillInputData( InputData inputData )
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
     {

Added: maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java?rev=783053&view=auto
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java
(added)
+++ maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java
Tue Jun  9 16:02:23 2009
@@ -0,0 +1,77 @@
+package org.apache.maven.wagon.shared.http;
+
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+
+public class HttpConfiguration
+{
+    
+    private HttpMethodConfiguration all;
+    
+    private HttpMethodConfiguration get;
+    
+    private HttpMethodConfiguration put;
+    
+    private HttpMethodConfiguration head;
+
+    public HttpMethodConfiguration getAll()
+    {
+        return all;
+    }
+
+    public void setAll( HttpMethodConfiguration all )
+    {
+        this.all = all;
+    }
+
+    public HttpMethodConfiguration getGet()
+    {
+        return get;
+    }
+    
+    public void setGet( HttpMethodConfiguration get )
+    {
+        this.get = get;
+    }
+
+    public HttpMethodConfiguration getPut()
+    {
+        return put;
+    }
+
+    public void setPut( HttpMethodConfiguration put )
+    {
+        this.put = put;
+    }
+
+    public HttpMethodConfiguration getHead()
+    {
+        return head;
+    }
+
+    public void setHead( HttpMethodConfiguration head )
+    {
+        this.head = head;
+    }
+    
+    public HttpMethodConfiguration getMethodConfiguration( HttpMethod method )
+    {
+        if ( method instanceof GetMethod )
+        {
+            return HttpMethodConfiguration.merge( all, get );
+        }
+        else if ( method instanceof PutMethod )
+        {
+            return HttpMethodConfiguration.merge( all, put );
+        }
+        else if ( method instanceof HeadMethod )
+        {
+            return HttpMethodConfiguration.merge( all, head );
+        }
+        
+        return all;
+    }
+
+}

Propchange: maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java?rev=783053&view=auto
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java
(added)
+++ maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java
Tue Jun  9 16:02:23 2009
@@ -0,0 +1,255 @@
+package org.apache.maven.wagon.shared.http;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.params.HttpClientParams;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class HttpMethodConfiguration
+{
+    
+    public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
+
+    private static final String COERCE_PATTERN = "\\{%(\\w+ ),([^}]+)\\}";
+    
+    private Boolean useDefaultHeaders;
+    
+    private Map headers = new LinkedHashMap();
+    
+    private Map params = new LinkedHashMap();
+    
+    private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
+
+    public boolean isUseDefaultHeaders()
+    {
+        return useDefaultHeaders == null ? true : useDefaultHeaders.booleanValue();
+    }
+
+    public void setUseDefaultHeaders( boolean useDefaultHeaders )
+    {
+        this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders );
+    }
+    
+    public Boolean getUseDefaultHeaders()
+    {
+        return useDefaultHeaders;
+    }
+    
+    public void addHeader( String header, String value )
+    {
+        headers.put( header, value );
+    }
+
+    public Map getHeaders()
+    {
+        return headers;
+    }
+
+    public void setHeaders( Map headers )
+    {
+        this.headers = headers;
+    }
+    
+    public void addParam( String param, String value )
+    {
+        params.put( param, value );
+    }
+
+    public Map getParams()
+    {
+        return params;
+    }
+
+    public void setParams( Map params )
+    {
+        this.params = params;
+    }
+
+    public int getConnectionTimeout()
+    {
+        return connectionTimeout;
+    }
+
+    public void setConnectionTimeout( int connectionTimeout )
+    {
+        this.connectionTimeout = connectionTimeout;
+    }
+
+    public HttpMethodParams asMethodParams( HttpMethodParams defaults )
+    {
+        if ( !hasParams() )
+        {
+            return null;
+        }
+        
+        HttpMethodParams p = new HttpMethodParams();
+        p.setDefaults( defaults );
+        
+        fillParams( p );
+        
+        return p;
+    }
+
+    private boolean hasParams()
+    {
+        if ( connectionTimeout < 1 && params == null )
+        {
+            return false;
+        }
+        
+        return true;
+    }
+
+    private void fillParams( HttpMethodParams p )
+    {
+        if ( !hasParams() )
+        {
+            return;
+        }
+        
+        if ( connectionTimeout > 0 )
+        {
+            p.setSoTimeout( connectionTimeout );
+        }
+        
+        if ( params != null )
+        {
+            Pattern coercePattern = Pattern.compile( COERCE_PATTERN );
+            
+            for ( Iterator it = params.entrySet().iterator(); it.hasNext(); )
+            {
+                Map.Entry entry = (Map.Entry) it.next();
+                
+                String key = (String) entry.getKey();
+                String value = (String) entry.getValue();
+                
+                Matcher matcher = coercePattern.matcher( value );
+                if ( matcher.matches() )
+                {
+                    char type = matcher.group( 1 ).charAt( 0 );
+                    value = matcher.group( 2 );
+                    
+                    switch( type )
+                    {
+                        case 'i':
+                        {
+                            p.setIntParameter( value, Integer.parseInt( value ) );
+                            break;
+                        }
+                        case 'd':
+                        {
+                            p.setDoubleParameter( value, Double.parseDouble( value ) );
+                            break;
+                        }
+                        case 'l':
+                        {
+                            p.setLongParameter( value, Long.parseLong( value ) );
+                            break;
+                        }
+                        case 'b':
+                        {
+                            p.setBooleanParameter( value, Boolean.valueOf( value ).booleanValue()
);
+                            break;
+                        }
+                    }
+                }
+                else
+                {
+                    p.setParameter( key, value );
+                }
+            }
+        }
+    }
+
+    public Header[] asRequestHeaders()
+    {
+        if ( headers == null )
+        {
+            return null;
+        }
+        
+        Header[] result = new Header[headers.size()];
+        
+        int index = 0;
+        for ( Iterator it = headers.entrySet().iterator(); it.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) it.next();
+            
+            String key = (String) entry.getKey();
+            String value = (String) entry.getValue();
+            
+            Header header = new Header( key, value );
+            result[index++] = header;
+        }
+        
+        return result;
+    }
+    
+    private HttpMethodConfiguration copy()
+    {
+        HttpMethodConfiguration copy = new HttpMethodConfiguration();
+        
+        copy.setConnectionTimeout( getConnectionTimeout() );
+        if ( getHeaders() != null )
+        {
+            copy.setHeaders( getHeaders() );
+        }
+        
+        if ( getParams() != null )
+        {
+            copy.setParams( getParams() );
+        }
+
+        copy.setUseDefaultHeaders( isUseDefaultHeaders() );
+        
+        return copy;
+    }
+
+    public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration
local )
+    {
+        if ( base == null && local == null )
+        {
+            return null;
+        }
+        else if ( base == null )
+        {
+            return local;
+        }
+        else if ( local == null )
+        {
+            return base;
+        }
+        else
+        {
+            HttpMethodConfiguration result = base.copy();
+            
+            if ( local.getConnectionTimeout() != DEFAULT_CONNECTION_TIMEOUT )
+            {
+                result.setConnectionTimeout( local.getConnectionTimeout() );
+            }
+            
+            if ( local.getHeaders() != null )
+            {
+                result.getHeaders().putAll( local.getHeaders() );
+            }
+            
+            if ( local.getParams() != null )
+            {
+                result.getParams().putAll( local.getParams() );
+            }
+            
+            if ( local.getUseDefaultHeaders() != null )
+            {
+                result.setUseDefaultHeaders( local.isUseDefaultHeaders() );
+            }
+            
+            return result;
+        }
+    }
+    
+}

Propchange: maven/wagon/trunk/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/HttpMethodConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-commits-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-commits-help@maven.apache.org


Mime
View raw message