diff --git a/src/main/java/org/sonatype/nexus/index/updater/jetty/JettyResourceFetcher.java b/src/main/java/org/sonatype/nexus/index/updater/jetty/JettyResourceFetcher.java index d2667ef437..84ba4e172d 100644 --- a/src/main/java/org/sonatype/nexus/index/updater/jetty/JettyResourceFetcher.java +++ b/src/main/java/org/sonatype/nexus/index/updater/jetty/JettyResourceFetcher.java @@ -11,6 +11,7 @@ import org.eclipse.jetty.client.security.Realm; import org.eclipse.jetty.client.security.RealmResolver; import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpMethods; import org.sonatype.nexus.index.updater.ResourceFetcher; import java.io.File; @@ -23,7 +24,7 @@ public class JettyResourceFetcher { // configuration fields. - private int maxConnections = 1; + private int maxConnections; private int connectionTimeout; @@ -52,6 +53,10 @@ public void retrieve( final String name, final File targetFile ) throws IOException, FileNotFoundException { HttpFields exchangeHeaders = buildHeaders(); + ResourceExchange exchange = new ResourceExchange( targetFile, exchangeHeaders, maxRedirects, listenerSupport ); + + exchange.setMethod( HttpMethods.GET ); + StringBuilder getUrl = new StringBuilder( url ); if ( getUrl.charAt( getUrl.length() - 1 ) != '/' && !name.startsWith( "/" ) ) { @@ -59,28 +64,16 @@ public void retrieve( final String name, final File targetFile ) } getUrl.append( name ); - ResourceExchange exchange = - new ResourceExchange( targetFile, exchangeHeaders, getUrl.toString(), listenerSupport ); - - get( exchange ); - - int redirCount = 0; - while ( exchange.isRedirectRequested() && redirCount < maxRedirects ) - { - String url = exchange.getRedirectUrl(); - - exchange = new ResourceExchange( targetFile, exchangeHeaders, getUrl.toString(), url, listenerSupport ); - - get( exchange ); - } + exchange.setURL( getUrl.toString() ); - if ( redirCount == maxRedirects ) + exchange = get( exchange ); + while ( exchange.prepareForRedirect() ) { - throw new IOException( "Maximum redirection count (" + maxRedirects + ") exceeded." ); + exchange = get( exchange ); } } - private void get( final ResourceExchange exchange ) + private ResourceExchange get( final ResourceExchange exchange ) throws IOException { httpClient.send( exchange ); @@ -96,15 +89,6 @@ private void get( final ResourceExchange exchange ) throw err; } - try - { - Thread.sleep( 500 ); - } - catch ( InterruptedException e ) - { - e.printStackTrace(); - } - int responseStatus = exchange.getResponseStatus(); switch ( responseStatus ) { @@ -115,7 +99,7 @@ private void get( final ResourceExchange exchange ) break; case ServerResponse.SC_FORBIDDEN: - throw new IOException( "Transfer failed: [" + responseStatus + "] " + exchange.getOriginalUrl() ); + throw new IOException( "Transfer failed: [" + responseStatus + "] " + url ); case ServerResponse.SC_UNAUTHORIZED: throw new IOException( "Transfer failed: Not authorized" ); @@ -124,17 +108,18 @@ private void get( final ResourceExchange exchange ) throw new IOException( "Transfer failed: Not authorized by proxy" ); case ServerResponse.SC_NOT_FOUND: - throw new IOException( "Transfer failed: " + exchange.getOriginalUrl() + " does not exist" ); + throw new IOException( "Transfer failed: " + url + " does not exist" ); default: { - IOException ex = - new IOException( "Transfer failed: [" + responseStatus + "] " + exchange.getOriginalUrl() ); + IOException ex = new IOException( "Transfer failed: [" + responseStatus + "] " + url ); listenerSupport.fireTransferError( url, ex, TransferEvent.REQUEST_GET ); throw ex; } } + + return exchange; } public void connect( final String id, final String url ) diff --git a/src/main/java/org/sonatype/nexus/index/updater/jetty/ResourceExchange.java b/src/main/java/org/sonatype/nexus/index/updater/jetty/ResourceExchange.java index 1426bc5445..9ae4a7f8bb 100644 --- a/src/main/java/org/sonatype/nexus/index/updater/jetty/ResourceExchange.java +++ b/src/main/java/org/sonatype/nexus/index/updater/jetty/ResourceExchange.java @@ -5,7 +5,6 @@ import org.eclipse.jetty.client.ContentExchange; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.http.HttpMethods; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.BufferUtil; import org.eclipse.jetty.io.nio.NIOBuffer; @@ -30,12 +29,14 @@ public class ResourceExchange private final File targetFile; - private final String originalUrl; + private final int maxRedirects; private final TransferListenerSupport listenerSupport; protected long lastModified; + private int redirectCount = 0; + private boolean redirectionRequested; private String redirectionUrl; @@ -44,6 +45,8 @@ public class ResourceExchange private File tmpFile; + private String originalUrl; + private TransferEvent transferEvent; private String contentEncoding; @@ -52,22 +55,12 @@ public class ResourceExchange private int contentLength; - public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final String targetUrl, + public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final int maxRedirects, final TransferListenerSupport listenerSupport ) - { - this( targetFile, httpHeaders, targetUrl, targetUrl, listenerSupport ); - } - - public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final String originalUrl, - final String targetUrl, final TransferListenerSupport listenerSupport ) { super( false ); - - setMethod( HttpMethods.GET ); - setURL( targetUrl ); - this.targetFile = targetFile; - this.originalUrl = originalUrl; + this.maxRedirects = maxRedirects; this.listenerSupport = listenerSupport; addRequestHeaders( httpHeaders ); @@ -114,6 +107,10 @@ public void onResponseHeader( final Buffer name, final Buffer value ) case HttpHeaders.LOCATION_ORDINAL: { redirectionUrl = value.toString(); + + System.out.println( redirectionUrl ); + redirectCount++; + break; } } @@ -130,6 +127,38 @@ public void onResponseStatus( final Buffer version, final int status, final Buff } } + public int getMaxRedirects() + { + return maxRedirects; + } + + public int getRedirectCount() + { + return redirectCount; + } + + public boolean prepareForRedirect() + { + if ( redirectionRequested && redirectCount < maxRedirects ) + { + setURL( redirectionUrl ); + + reset(); + return true; + } + + return false; + } + + @Override + public void reset() + { + super.reset(); + + redirectionRequested = false; + redirectionUrl = null; + } + @Override protected synchronized void onResponseContent( final Buffer content ) throws IOException @@ -171,10 +200,6 @@ protected synchronized void onResponseContent( final Buffer content ) transferEvent.setTimestamp( System.currentTimeMillis() ); listenerSupport.fireTransferProgress( transferEvent, content.array(), contentLength ); } - else - { - System.out.println( "Redirection requested for location: " + redirectionUrl + "\nSkipping response body." ); - } } private void newTempFile() @@ -192,8 +217,6 @@ protected void onResponseComplete() if ( "gzip".equals( contentEncoding ) ) { - System.out.println( "Unpacking: " + tmpFile + "\nto: " + targetFile ); - InputStream in = null; OutputStream out = null; try @@ -212,14 +235,12 @@ protected void onResponseComplete() } else { - System.out.println( "Moving: " + tmpFile + "\nto: " + targetFile ); if ( targetFile.exists() ) { targetFile.delete(); } tmpFile.renameTo( targetFile ); - System.out.println( "Move target exists? " + targetFile.exists() ); targetFile.setLastModified( lastModified ); } @@ -227,10 +248,20 @@ protected void onResponseComplete() listenerSupport.fireGetCompleted( originalUrl, targetFile ); transferEvent = null; - redirectionRequested = false; } } + @Override + public void setURL( final String url ) + { + if ( originalUrl == null ) + { + originalUrl = url; + } + + super.setURL( url ); + } + void setResponseContentStream( final InputStream in ) throws IOException { @@ -308,19 +339,4 @@ void setContentLength( final int contentLength ) this.contentLength = contentLength; } - public String getOriginalUrl() - { - return originalUrl; - } - - public String getRedirectUrl() - { - return redirectionUrl; - } - - public boolean isRedirectRequested() - { - return redirectionRequested; - } - } diff --git a/src/test/java/org/sonatype/nexus/index/updater/FullBootProofOfConcept.java b/src/test/java/org/sonatype/nexus/index/updater/FullBootProofOfConcept.java index 50e3fa1505..25abe3b4d0 100644 --- a/src/test/java/org/sonatype/nexus/index/updater/FullBootProofOfConcept.java +++ b/src/test/java/org/sonatype/nexus/index/updater/FullBootProofOfConcept.java @@ -1,6 +1,5 @@ package org.sonatype.nexus.index.updater; -import org.apache.maven.wagon.authentication.AuthenticationInfo; import org.apache.maven.wagon.events.TransferEvent; import org.apache.maven.wagon.events.TransferListener; import org.codehaus.plexus.DefaultPlexusContainer; @@ -23,53 +22,10 @@ public class FullBootProofOfConcept { public static void main( final String[] args ) - throws IOException - { - for ( int i = 0; i < 2; i++ ) - { - File basedir = File.createTempFile( "nexus-indexer.", ".dir" ); - - try - { - run( basedir ); - } - catch ( IOException e ) - { - e.printStackTrace(); - } - catch ( ComponentLookupException e ) - { - e.printStackTrace(); - } - catch ( PlexusContainerException e ) - { - e.printStackTrace(); - } - catch ( ParseException e ) - { - e.printStackTrace(); - } - catch ( UnsupportedExistingLuceneIndexException e ) - { - e.printStackTrace(); - } - finally - { - try - { - FileUtils.forceDelete( basedir ); - } - catch ( IOException e ) - { - } - } - } - } - - public static void run( final File basedir ) throws IOException, ComponentLookupException, PlexusContainerException, ParseException, UnsupportedExistingLuceneIndexException { + File basedir = File.createTempFile( "nexus-indexer.", ".dir" ); try { FileUtils.forceDelete( basedir ); @@ -91,8 +47,7 @@ public static void run( final File basedir ) creators.add( jar ); String repositoryId = "test"; - String repositoryUrl = "http://repository.sonatype.org/content/groups/sonatype/"; - // String repositoryUrl = "http://repo1.maven.org/maven2/"; + String repositoryUrl = "http://repo1.maven.org/maven2/"; String indexUrl = repositoryUrl + ".index"; IndexingContext ctx = @@ -100,16 +55,6 @@ public static void run( final File basedir ) creators, true ); IndexUpdateRequest updateRequest = new IndexUpdateRequest( ctx ); - updateRequest.setAuthenticationInfo( new AuthenticationInfo() - { - private static final long serialVersionUID = 1L; - - { - setUserName( "deployment" ); - setPassword( "mj21op1" ); - } - } ); - updateRequest.setTransferListener( new TransferListener() {