Author: aheritier Date: Tue Jan 17 06:15:18 2006 New Revision: 369784 URL: http://svn.apache.org/viewcvs?rev=369784&view=rev Log: - Add a maximum number of redirections (10) to prevent loops - Release the connection before to check a redirection
Modified: maven/maven-1/plugins/trunk/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java Modified: maven/maven-1/plugins/trunk/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java URL: http://svn.apache.org/viewcvs/maven/maven-1/plugins/trunk/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java?rev=369784&r1=369783&r2=369784&view=diff ============================================================================== --- maven/maven-1/plugins/trunk/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java (original) +++ maven/maven-1/plugins/trunk/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java Tue Jan 17 06:15:18 2006 @@ -14,6 +14,7 @@ import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.NTCredentials; import org.apache.commons.httpclient.StatusLine; import org.apache.commons.httpclient.UsernamePasswordCredentials; @@ -35,6 +36,9 @@ */ private final static Log LOG = LogFactory.getLog( OnlineHTTPLinkValidator.class ); + /** The maximum number of redirections for a link */ + private final static int MAX_NB_REDIRECT = 10; + private String proxyHost; private int proxyPort; @@ -96,7 +100,7 @@ HttpMethod hm = null; try { - hm = checkLink( cl, link ); + hm = checkLink( cl, link, 0 ); } catch ( Throwable t ) { @@ -147,7 +151,6 @@ LOG.error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]" ); return new LinkValidationResult( LinkValidationResult.ERROR, false, t.getMessage() ); } - } private void initHttpClient() @@ -155,8 +158,7 @@ LOG.debug( "A new HttpClient instance is needed ..." ); // Some web servers doesn't allow the default user-agent sent by httpClient System.setProperty( "httpclient.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); - //cl = new HttpClient( new MultiThreadedHttpConnectionManager() ); - cl = new HttpClient(); + cl = new HttpClient( new MultiThreadedHttpConnectionManager() ); HostConfiguration hc = new HostConfiguration(); HttpState state = new HttpState(); if ( this.proxyHost != null ) @@ -194,9 +196,13 @@ LOG.debug( "New HttpClient instance created." ); } - private HttpMethod checkLink( HttpClient cl, String link ) + private HttpMethod checkLink( HttpClient cl, String link, int nbRedirect ) throws HttpException, IOException { + if ( nbRedirect > MAX_NB_REDIRECT ) + { + throw new HttpException( "Maximum number of redirections (" + MAX_NB_REDIRECT + ") exceeded" ); + } // execute the HEAD (a GET without the body returned) HttpMethod hm = new HeadMethod( link ); try @@ -243,7 +249,8 @@ } HttpMethod oldHm = hm; LOG.info( "[" + link + "] is redirected to [" + newLink + "]" ); - hm = checkLink( cl, newLink ); + hm.releaseConnection(); + hm = checkLink( cl, newLink, nbRedirect + 1 ); // Restore the hm to "Moved permanently" if the new location is found to allow us to report it if ( oldHm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY && hm.getStatusCode() == HttpStatus.SC_OK )