Author: nicolas Date: Sun Aug 28 14:24:39 2011 New Revision: 1162531 URL: http://svn.apache.org/viewvc?rev=1162531&view=rev Log: [WAGON-346] callback Wagon from java.net.Authenticator singleton using a ThreadLocal
Added: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonAuthenticator.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=1162531&r1=1162530&r2=1162531&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:24:39 2011 @@ -36,7 +36,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; @@ -80,6 +79,11 @@ public class LightweightHttpWagon */ private Properties httpHeaders; + /** + * @plexus.requirement + */ + private LightweightHttpWagonAuthenticator authenticator; + /** * Builds a complete URL string from the repository URL and the relative path passed. * @@ -255,43 +259,35 @@ public class LightweightHttpWagon { this.proxy = getProxy( proxyInfo ); } + authenticator.setWagon( this ); + } - final boolean hasProxy = ( proxyInfo != null && proxyInfo.getUserName() != null ); - final boolean hasAuthentication = ( authenticationInfo != null && authenticationInfo.getUserName() != null ); - if ( hasProxy || hasAuthentication ) + public PasswordAuthentication requestProxyAuthentication() + { + if ( proxyInfo != null && proxyInfo.getUserName() != null ) { - Authenticator.setDefault( new Authenticator() + String password = ""; + if ( proxyInfo.getPassword() != null ) { - protected PasswordAuthentication getPasswordAuthentication() - { - if ( getRequestorType() == RequestorType.PROXY ) - { - String password = ""; - if ( proxyInfo.getPassword() != null ) - { - password = proxyInfo.getPassword(); - } - return new PasswordAuthentication( proxyInfo.getUserName(), password.toCharArray() ); - } - - if ( hasAuthentication ) - { - String password = ""; - if ( authenticationInfo.getPassword() != null ) - { - password = authenticationInfo.getPassword(); - } - return new PasswordAuthentication( authenticationInfo.getUserName(), password.toCharArray() ); - } - - return super.getPasswordAuthentication(); - } - } ); + password = proxyInfo.getPassword(); + } + return new PasswordAuthentication( proxyInfo.getUserName(), password.toCharArray() ); } - else + return null; + } + + public PasswordAuthentication requestServerAuthentication() + { + if ( authenticationInfo != null && authenticationInfo.getUserName() != null ) { - Authenticator.setDefault( null ); + String password = ""; + if ( authenticationInfo.getPassword() != null ) + { + password = authenticationInfo.getPassword(); + } + return new PasswordAuthentication( authenticationInfo.getUserName(), password.toCharArray() ); } + return null; } private Proxy getProxy( ProxyInfo proxyInfo ) @@ -319,6 +315,7 @@ public class LightweightHttpWagon { putConnection.disconnect(); } + authenticator.resetWagon(); } public List getFileList( String destinationDirectory ) @@ -423,5 +420,4 @@ public class LightweightHttpWagon System.getProperties().remove( key ); } } - } Added: maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonAuthenticator.java URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonAuthenticator.java?rev=1162531&view=auto ============================================================================== --- maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonAuthenticator.java (added) +++ maven/wagon/trunk/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonAuthenticator.java Sun Aug 28 14:24:39 2011 @@ -0,0 +1,61 @@ +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 java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * @author <a href="mailto:nicolas.del...@cloudbees.com">Nicolas De loof</a> + * @plexus.component role="org.apache.maven.wagon.providers.http.LightweightHttpWagonAuthenticator" + */ +public class LightweightHttpWagonAuthenticator + extends Authenticator +{ + ThreadLocal<LightweightHttpWagon> localWagon = new ThreadLocal<LightweightHttpWagon>(); + + protected PasswordAuthentication getPasswordAuthentication() + { + LightweightHttpWagon wagon = localWagon.get(); + if ( wagon != null ) + { + if ( getRequestorType() == RequestorType.PROXY ) + { + return wagon.requestProxyAuthentication(); + } + else + { + return wagon.requestServerAuthentication(); + } + } + return null; + } + + public void setWagon( LightweightHttpWagon wagon ) + { + localWagon.set( wagon ); + Authenticator.setDefault( this ); + } + + public void resetWagon() + { + localWagon.remove(); + } +}