- Add a maximum number of redirections (10) to prevent loops

- Release the connection before to check a redirection

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@369784 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
aheritier committed 2006-01-17 14:15:18 +00:00
1 parent 55b958c401
commit 552e0bd1cc
1 file changed
+13 -6
@@ -14,6 +14,7 @@ import org.apache.commons.httpclient.HttpException;
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 @@ public final class OnlineHTTPLinkValidator
*/
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 @@ public final class OnlineHTTPLinkValidator
HttpMethod hm = null;
try
{
hm = checkLink( cl, link );
hm = checkLink( cl, link, 0 );
}
catch ( Throwable t )
{
@@ -147,7 +151,6 @@ public final class OnlineHTTPLinkValidator
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 @@ public final class OnlineHTTPLinkValidator
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 @@ public final class OnlineHTTPLinkValidator
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 @@ public final class OnlineHTTPLinkValidator
}
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 )