maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hbout...@apache.org
Subject [1/2] [WAGON-388] moved code dependant on HttpComponents 3.x from wagon-http-shared to wagon-webdav-jackrabbit
Date Sat, 09 Feb 2013 07:25:27 GMT
http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/24a54ebe/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/HttpMethodConfiguration.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/HttpMethodConfiguration.java
b/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/HttpMethodConfiguration.java
new file mode 100644
index 0000000..f752697
--- /dev/null
+++ b/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/HttpMethodConfiguration.java
@@ -0,0 +1,322 @@
+package org.apache.maven.wagon.providers.webdav;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+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 Properties headers = new Properties();
+
+    private Properties params = new Properties();
+
+    private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
+
+    public boolean isUseDefaultHeaders()
+    {
+        return useDefaultHeaders == null ? true : useDefaultHeaders.booleanValue();
+    }
+
+    public HttpMethodConfiguration setUseDefaultHeaders( boolean useDefaultHeaders )
+    {
+        this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders );
+        return this;
+    }
+
+    public Boolean getUseDefaultHeaders()
+    {
+        return useDefaultHeaders;
+    }
+
+    public HttpMethodConfiguration addHeader( String header, String value )
+    {
+        headers.setProperty( header, value );
+        return this;
+    }
+
+    public Properties getHeaders()
+    {
+        return headers;
+    }
+
+    public HttpMethodConfiguration setHeaders( Properties headers )
+    {
+        this.headers = headers;
+        return this;
+    }
+
+    public HttpMethodConfiguration addParam( String param, String value )
+    {
+        params.setProperty( param, value );
+        return this;
+    }
+
+    public Properties getParams()
+    {
+        return params;
+    }
+
+    public HttpMethodConfiguration setParams( Properties params )
+    {
+        this.params = params;
+        return this;
+    }
+
+    public int getConnectionTimeout()
+    {
+        return connectionTimeout;
+    }
+
+    public HttpMethodConfiguration setConnectionTimeout( int connectionTimeout )
+    {
+        this.connectionTimeout = connectionTimeout;
+        return this;
+    }
+
+    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( key, Integer.parseInt( value ) );
+                            break;
+                        }
+                        case 'd':
+                        {
+                            p.setDoubleParameter( key, Double.parseDouble( value ) );
+                            break;
+                        }
+                        case 'l':
+                        {
+                            p.setLongParameter( key, Long.parseLong( value ) );
+                            break;
+                        }
+                        case 'b':
+                        {
+                            p.setBooleanParameter( key, Boolean.valueOf( value ).booleanValue()
);
+                            break;
+                        }
+                        case 'c':
+                        {
+                            String[] entries = value.split( "," );
+                            List collection = new ArrayList();
+                            for ( int i = 0; i < entries.length; i++ )
+                            {
+                                collection.add( entries[i].trim() );
+                            }
+
+                            p.setParameter( key, collection );
+                            break;
+                        }
+                        case 'm':
+                        {
+                            String[] entries = value.split( "," );
+
+                            Map map = new LinkedHashMap();
+                            for ( int i = 0; i < entries.length; i++ )
+                            {
+                                int idx = entries[i].indexOf( "=>" );
+                                if ( idx < 1 )
+                                {
+                                    break;
+                                }
+
+                                String mapKey = entries[i].substring( 0, idx );
+                                String mapVal = entries[i].substring( idx + 1, entries[i].length()
);
+                                map.put( mapKey.trim(), mapVal.trim() );
+                            }
+
+                            p.setParameter( key, map );
+                            break;
+                        }
+                    }
+                }
+                else
+                {
+                    p.setParameter( key, value );
+                }
+            }
+        }
+    }
+
+    public Header[] asRequestHeaders()
+    {
+        if ( headers == null )
+        {
+            return new Header[0];
+        }
+
+        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 defaults, HttpMethodConfiguration
base,
+                                                 HttpMethodConfiguration local )
+    {
+        HttpMethodConfiguration result = merge( defaults, base );
+        return merge( result, local );
+    }
+
+    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;
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/24a54ebe/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
b/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
index 5256b89..a545d9e 100644
--- a/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
+++ b/wagon-providers/wagon-webdav-jackrabbit/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
@@ -37,7 +37,6 @@ import org.apache.maven.wagon.TransferFailedException;
 import org.apache.maven.wagon.WagonConstants;
 import org.apache.maven.wagon.authorization.AuthorizationException;
 import org.apache.maven.wagon.repository.Repository;
-import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.StringUtils;
 import org.w3c.dom.Node;

http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/24a54ebe/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/AbstractHttpClientWagonTest.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/AbstractHttpClientWagonTest.java
b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/AbstractHttpClientWagonTest.java
new file mode 100644
index 0000000..da55ad7
--- /dev/null
+++ b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/AbstractHttpClientWagonTest.java
@@ -0,0 +1,240 @@
+package org.apache.maven.wagon.providers.webdav;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.NTCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.params.HttpClientParams;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.OutputData;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagon;
+import org.apache.maven.wagon.providers.webdav.HttpConfiguration;
+import org.apache.maven.wagon.providers.webdav.HttpMethodConfiguration;
+import org.apache.maven.wagon.proxy.ProxyInfo;
+import org.apache.maven.wagon.repository.Repository;
+
+public class AbstractHttpClientWagonTest
+    extends TestCase
+{
+
+    public void testSetPreemptiveAuthParamViaConfig()
+    {
+        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
+        methodConfig.addParam( HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true" );
+
+        HttpConfiguration config = new HttpConfiguration();
+        config.setAll( methodConfig );
+
+        TestWagon wagon = new TestWagon();
+        wagon.setHttpConfiguration( config );
+
+        HeadMethod method = new HeadMethod();
+        wagon.setParameters( method );
+
+        HttpMethodParams params = method.getParams();
+        assertNotNull( params );
+        assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION )
);
+    }
+
+    public void testSetMaxRedirectsParamViaConfig()
+    {
+        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
+        int maxRedirects = 2;
+        methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects );
+
+        HttpConfiguration config = new HttpConfiguration();
+        config.setAll( methodConfig );
+
+        TestWagon wagon = new TestWagon();
+        wagon.setHttpConfiguration( config );
+
+        HeadMethod method = new HeadMethod();
+        wagon.setParameters( method );
+
+        HttpMethodParams params = method.getParams();
+        assertNotNull( params );
+        assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS,
-1 ) );
+    }
+
+    public void testDefaultHeadersUsedByDefault()
+    {
+        HttpConfiguration config = new HttpConfiguration();
+        config.setAll( new HttpMethodConfiguration() );
+
+        TestWagon wagon = new TestWagon();
+        wagon.setHttpConfiguration( config );
+
+        HeadMethod method = new HeadMethod();
+        wagon.setHeaders( method );
+
+        // these are the default 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" );
+
+        Header header = method.getRequestHeader( "Cache-control" );
+        assertNotNull( header );
+        assertEquals( "no-cache", header.getValue() );
+
+        header = method.getRequestHeader( "Cache-store" );
+        assertNotNull( header );
+        assertEquals( "no-store", header.getValue() );
+
+        header = method.getRequestHeader( "Pragma" );
+        assertNotNull( header );
+        assertEquals( "no-cache", header.getValue() );
+
+        header = method.getRequestHeader( "Expires" );
+        assertNotNull( header );
+        assertEquals( "0", header.getValue() );
+
+        header = method.getRequestHeader( "Accept-Encoding" );
+        assertNotNull( header );
+        assertEquals( "gzip", header.getValue() );
+    }
+
+    public void testTurnOffDefaultHeaders()
+    {
+        HttpConfiguration config = new HttpConfiguration();
+        config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
+
+        TestWagon wagon = new TestWagon();
+        wagon.setHttpConfiguration( config );
+
+        HeadMethod method = new HeadMethod();
+        wagon.setHeaders( method );
+
+        // these are the default 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" );
+
+        Header header = method.getRequestHeader( "Cache-control" );
+        assertNull( header );
+
+        header = method.getRequestHeader( "Cache-store" );
+        assertNull( header );
+
+        header = method.getRequestHeader( "Pragma" );
+        assertNull( header );
+
+        header = method.getRequestHeader( "Expires" );
+        assertNull( header );
+
+        header = method.getRequestHeader( "Accept-Encoding" );
+        assertNull( header );
+    }
+
+    public void testNTCredentialsWithUsernameNull()
+        throws AuthenticationException, ConnectionException
+    {
+        TestWagon wagon = new TestWagon();
+
+        Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
+        wagon.connect( repository );
+
+        wagon.openConnection();
+
+        assertNull( wagon.getAuthenticationInfo().getUserName() );
+        assertNull( wagon.getAuthenticationInfo().getPassword() );
+
+        assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0
) ) instanceof NTCredentials );
+    }
+
+    public void testNTCredentialsNoNTDomain()
+        throws AuthenticationException, ConnectionException
+    {
+        TestWagon wagon = new TestWagon();
+
+        AuthenticationInfo authenticationInfo = new AuthenticationInfo();
+        String myUsernameNoNTDomain = "myUserNameNoNTDomain";
+        authenticationInfo.setUserName( myUsernameNoNTDomain );
+
+        String myPassword = "myPassword";
+        authenticationInfo.setPassword( myPassword );
+
+        Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
+
+        wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
+
+        wagon.openConnection();
+
+        assertEquals( myUsernameNoNTDomain, wagon.getAuthenticationInfo().getUserName() );
+        assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
+
+        assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0
) ) instanceof NTCredentials );
+    }
+
+    public void testNTCredentialsWithNTDomain()
+        throws AuthenticationException, ConnectionException
+    {
+        TestWagon wagon = new TestWagon();
+
+        AuthenticationInfo authenticationInfo = new AuthenticationInfo();
+        String myNTDomain = "myNTDomain";
+        String myUsername = "myUsername";
+        String myNTDomainAndUser = myNTDomain + "\\" + myUsername;
+        authenticationInfo.setUserName( myNTDomainAndUser );
+
+        String myPassword = "myPassword";
+        authenticationInfo.setPassword( myPassword );
+
+        Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
+
+        wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
+
+        wagon.openConnection();
+
+        assertEquals( myNTDomainAndUser, wagon.getAuthenticationInfo().getUserName() );
+        assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
+
+        Credentials credentials = wagon.getClient().getState().getCredentials( new AuthScope(
null, 0 ) );
+        assertTrue( credentials instanceof NTCredentials );
+
+        NTCredentials ntCredentials = (NTCredentials) credentials;
+        assertEquals( myNTDomain, ntCredentials.getDomain() );
+        assertEquals( myUsername, ntCredentials.getUserName() );
+        assertEquals( myPassword, ntCredentials.getPassword() );
+    }
+
+    private static final class TestWagon
+        extends AbstractHttpClientWagon
+    {
+        @Override
+        public void fillOutputData( OutputData outputData )
+            throws TransferFailedException
+        {
+
+        }
+    }
+
+}


Mime
View raw message