Author: nicolas Date: Sun Aug 28 14:25:01 2011 New Revision: 1162532 URL: http://svn.apache.org/viewvc?rev=1162532&view=rev Log: [WAGON-347] Support preemtive authentication
Added: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java Modified: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java Modified: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java?rev=1162532&r1=1162531&r2=1162532&view=diff ============================================================================== --- maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java (original) +++ maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java Sun Aug 28 14:25:01 2011 @@ -31,6 +31,7 @@ import org.apache.maven.wagon.events.Tra import org.apache.maven.wagon.proxy.ProxyInfo; import org.apache.maven.wagon.resource.Resource; import org.apache.maven.wagon.shared.http.HtmlFileListParser; +import org.codehaus.plexus.util.Base64; import java.io.FileNotFoundException; import java.io.IOException; @@ -45,12 +46,25 @@ import java.net.Proxy.Type; import java.net.SocketAddress; import java.net.URL; import java.net.URLConnection; -import java.util.ArrayList; import java.util.Iterator; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.zip.GZIPInputStream; +import org.apache.maven.wagon.ConnectionException; +import org.apache.maven.wagon.InputData; +import org.apache.maven.wagon.OutputData; +import org.apache.maven.wagon.ResourceDoesNotExistException; +import org.apache.maven.wagon.StreamWagon; +import org.apache.maven.wagon.TransferFailedException; +import org.apache.maven.wagon.authentication.AuthenticationException; +import org.apache.maven.wagon.authorization.AuthorizationException; +import org.apache.maven.wagon.events.TransferEvent; +import org.apache.maven.wagon.proxy.ProxyInfo; +import org.apache.maven.wagon.resource.Resource; +import org.apache.maven.wagon.shared.http.HtmlFileListParser; + /** * LightweightHttpWagon * @@ -61,6 +75,8 @@ import java.util.zip.GZIPInputStream; public class LightweightHttpWagon extends StreamWagon { + private boolean preemptiveAuthentication; + private HttpURLConnection putConnection; private Proxy proxy = Proxy.NO_PROXY; @@ -180,7 +196,7 @@ public class LightweightHttpWagon } } - private void addHeaders( URLConnection urlConnection ) + private void addHeaders( HttpURLConnection urlConnection ) { if ( httpHeaders != null ) { @@ -190,6 +206,17 @@ public class LightweightHttpWagon urlConnection.setRequestProperty( header, httpHeaders.getProperty( header ) ); } } + setAuthorization( urlConnection ); + } + + private void setAuthorization( HttpURLConnection urlConnection ) + { + if ( preemptiveAuthentication && authenticationInfo != null && authenticationInfo.getUserName() != null ) + { + String credentials = authenticationInfo.getUserName() + ":" + authenticationInfo.getPassword(); + String encoded = new String( Base64.encodeBase64( credentials.getBytes() ) ); + urlConnection.setRequestProperty( "Authorization", "Basic " + encoded ); + } } public void fillOutputData( OutputData outputData ) @@ -259,7 +286,11 @@ public class LightweightHttpWagon { this.proxy = getProxy( proxyInfo ); } - authenticator.setWagon( this ); + authenticator.setWagon(this); + + setPreemptiveAuthentication( + Boolean.getBoolean( "maven.wagon.http.preemptiveAuthentication" ) + || Boolean.parseBoolean( repository.getParameter( "preemptiveAuthentication" ) ) ); } public PasswordAuthentication requestProxyAuthentication() @@ -420,4 +451,9 @@ public class LightweightHttpWagon System.getProperties().remove( key ); } } + + public void setPreemptiveAuthentication( boolean preemptiveAuthentication ) + { + this.preemptiveAuthentication |= preemptiveAuthentication; + } } Added: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java?rev=1162532&view=auto ============================================================================== --- maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java (added) +++ maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java Sun Aug 28 14:25:01 2011 @@ -0,0 +1,37 @@ +package org.apache.maven.wagon.providers.http; + +/* + * 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.maven.wagon.Wagon; + +/** + * @author <a href="michal.mac...@dimatics.com">Michal Maczka</a> + * @version $Id$ + */ +public class LightweightHttpWagonWithPreemptiveAuthenticationTest + extends LightweightHttpWagonTest +{ + @Override + protected Wagon getWagon() throws Exception { + LightweightHttpWagon wagon = (LightweightHttpWagon) super.getWagon(); + wagon.setPreemptiveAuthentication( true ); + return wagon; + } +}