Author: vsiveton Date: Tue Jul 14 11:15:04 2009 New Revision: 793854 URL: http://svn.apache.org/viewvc?rev=793854&view=rev Log: MJAVADOC-238: No timeout set for URLConnection which can cause build to get stuck
o refactor fetchURL() to use HttpClient o added test case Modified: maven/plugins/trunk/maven-javadoc-plugin/pom.xml maven/plugins/trunk/maven-javadoc-plugin/src/main/java/org/apache/maven/plugin/javadoc/JavadocUtil.java maven/plugins/trunk/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/JavadocUtilTest.java Modified: maven/plugins/trunk/maven-javadoc-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-javadoc-plugin/pom.xml?rev=793854&r1=793853&r2=793854&view=diff ============================================================================== --- maven/plugins/trunk/maven-javadoc-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-javadoc-plugin/pom.xml Tue Jul 14 11:15:04 2009 @@ -141,6 +141,11 @@ <version>2.4</version> </dependency> <dependency> + <groupId>commons-httpclient</groupId> + <artifactId>commons-httpclient</artifactId> + <version>3.1</version> + </dependency> + <dependency> <groupId>com.thoughtworks.qdox</groupId> <artifactId>qdox</artifactId> <version>1.9.2</version> Modified: maven/plugins/trunk/maven-javadoc-plugin/src/main/java/org/apache/maven/plugin/javadoc/JavadocUtil.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-javadoc-plugin/src/main/java/org/apache/maven/plugin/javadoc/JavadocUtil.java?rev=793854&r1=793853&r2=793854&view=diff ============================================================================== --- maven/plugins/trunk/maven-javadoc-plugin/src/main/java/org/apache/maven/plugin/javadoc/JavadocUtil.java (original) +++ maven/plugins/trunk/maven-javadoc-plugin/src/main/java/org/apache/maven/plugin/javadoc/JavadocUtil.java Tue Jul 14 11:15:04 2009 @@ -22,14 +22,13 @@ import java.io.ByteArrayOutputStream; 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.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Modifier; -import java.net.Authenticator; -import java.net.PasswordAuthentication; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; @@ -37,7 +36,6 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; -import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; import java.util.jar.JarEntry; @@ -46,6 +44,10 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.maven.artifact.Artifact; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Proxy; @@ -682,60 +684,40 @@ { if ( url == null ) { - throw new IOException( "The url is null" ); + throw new IllegalArgumentException( "The url is null" ); } - Properties oldSystemProperties = new Properties(); - oldSystemProperties.putAll( System.getProperties() ); - - if ( settings != null ) + HttpClient httpClient = null; + if ( !"file".equals( url.getProtocol() ) ) { - String scheme = url.getProtocol(); + httpClient = new HttpClient(); + httpClient.getHttpConnectionManager().getParams().setConnectionTimeout( 1000 ); - if ( !"file".equals( scheme ) ) + if ( settings != null ) { Proxy activeProxy = settings.getActiveProxy(); + if ( activeProxy != null ) { - if ( "http".equals( scheme ) || "https".equals( scheme ) || "ftp".equals( scheme ) ) - { - scheme += "."; - } - else + String proxyHost = settings.getActiveProxy().getHost(); + int proxyPort = settings.getActiveProxy().getPort(); + + String proxyUser = settings.getActiveProxy().getUsername(); + String proxyPass = settings.getActiveProxy().getPassword(); + + if ( StringUtils.isNotEmpty( proxyHost ) ) { - scheme = ""; + httpClient.getHostConfiguration().setProxy( proxyHost, proxyPort ); } - if ( StringUtils.isNotEmpty( activeProxy.getHost() ) ) + if ( StringUtils.isNotEmpty( proxyUser ) ) { - Properties systemProperties = System.getProperties(); - systemProperties.setProperty( scheme + "proxySet", "true" ); - systemProperties.setProperty( scheme + "proxyHost", activeProxy.getHost() ); - - if ( activeProxy.getPort() > 0 ) - { - systemProperties - .setProperty( scheme + "proxyPort", String.valueOf( activeProxy.getPort() ) ); - } - - if ( StringUtils.isNotEmpty( activeProxy.getNonProxyHosts() ) ) - { - systemProperties.setProperty( scheme + "nonProxyHosts", activeProxy.getNonProxyHosts() ); - } - - final String userName = activeProxy.getUsername(); - if ( StringUtils.isNotEmpty( userName ) ) - { - final String pwd = StringUtils.isEmpty( activeProxy.getPassword() ) ? "" : activeProxy - .getPassword(); - Authenticator.setDefault( new Authenticator() - { - protected PasswordAuthentication getPasswordAuthentication() - { - return new PasswordAuthentication( userName, pwd.toCharArray() ); - } - } ); - } + AuthScope authScope = + new AuthScope( AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, + AuthScope.ANY_SCHEME ); + UsernamePasswordCredentials usernamePasswordCredentials = + new UsernamePasswordCredentials( proxyUser, proxyPass ); + httpClient.getState().setProxyCredentials( authScope, usernamePasswordCredentials ); } } } @@ -744,20 +726,31 @@ InputStream in = null; try { - in = url.openStream(); + if ( httpClient != null ) + { + GetMethod getMethod = new GetMethod( url.toString() ); + + try + { + int status = httpClient.executeMethod( getMethod ); + if ( status != 200 ) + { + throw new FileNotFoundException( url.toString() ); + } + } + finally + { + getMethod.releaseConnection(); + } + } + else + { + in = url.openStream(); + } } finally { IOUtil.close( in ); - - // Reset system properties - if ( ( settings != null ) && ( !"file".equals( url.getProtocol() ) ) - && ( settings.getActiveProxy() != null ) - && ( StringUtils.isNotEmpty( settings.getActiveProxy().getHost() ) ) ) - { - System.setProperties( oldSystemProperties ); - Authenticator.setDefault( null ); - } } } Modified: maven/plugins/trunk/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/JavadocUtilTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/JavadocUtilTest.java?rev=793854&r1=793853&r2=793854&view=diff ============================================================================== --- maven/plugins/trunk/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/JavadocUtilTest.java (original) +++ maven/plugins/trunk/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/JavadocUtilTest.java Tue Jul 14 11:15:04 2009 @@ -19,19 +19,21 @@ * under the License. */ +import java.io.File; +import java.io.IOException; +import java.net.URL; import java.util.regex.PatternSyntaxException; import org.apache.maven.settings.Proxy; import org.apache.maven.settings.Settings; - -import junit.framework.TestCase; +import org.codehaus.plexus.PlexusTestCase; /** * @author <a href="mailto:vincent.sive...@gmail.com">Vincent Siveton</a> * @version $Id$ */ public class JavadocUtilTest - extends TestCase + extends PlexusTestCase { /** * Method to test the javadoc version parsing. @@ -249,4 +251,70 @@ cmdLine = JavadocUtil.hideProxyPassword( cmdLine, null ); assertFalse( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 ); } + + /** + * Method to test fetchURL() + * + * @throws Exception if any + */ + public void testFetchURL() + throws Exception + { + Settings settings = null; + URL url = null; + + try + { + JavadocUtil.fetchURL( settings, url ); + assertTrue( false ); + } + catch ( IllegalArgumentException e ) + { + assertTrue( true ); + } + + url = new File( getBasedir(), "/pom.xml" ).toURL(); + JavadocUtil.fetchURL( settings, url ); + assertTrue( true ); + + url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list" ); + JavadocUtil.fetchURL( settings, url ); + assertTrue( true ); + + url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2" ); + try + { + JavadocUtil.fetchURL( settings, url ); + assertTrue( false ); + } + catch ( IOException e ) + { + assertTrue( true ); + } + + settings = new Settings(); + Proxy proxy = new Proxy(); + proxy.setActive( true ); + proxy.setHost( "140.211.11.10" ); // Apache's HTTP proxy + proxy.setPort( 80 ); + proxy.setUsername( "toto" ); + proxy.setPassword( "toto" ); + proxy.setNonProxyHosts( "www.google.com" ); + settings.addProxy( proxy ); + + url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list" ); + JavadocUtil.fetchURL( settings, url ); + assertTrue( true ); + + url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2" ); + try + { + JavadocUtil.fetchURL( settings, url ); + assertTrue( false ); + } + catch ( IOException e ) + { + assertTrue( true ); + } + } }