Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Revert so I can move to a branch and work until race conditions are f…
Browse files Browse the repository at this point in the history
…ixed.

[NEXUS-2684] Fixed JettyResourceFetcher to handle redirects properly, and to avoid caching the transferred content in a buffer in memory. Adding a proof-of-context, which I need to figure out how to use to build an automated test out of.

This reverts commit 12a6317fc9dcf866d1b34acbfbbf4fba74aad1ff.

git-svn-id: file:///opt/svn/repositories/sonatype.org/nexus/trunk/nexus-indexer@5054 2aa8b3fc-8ebb-4439-a84f-95066eaea8ab
  • Loading branch information
jdcasey committed Sep 22, 2009
1 parent 436a370 commit 1265c21
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 653 deletions.
@@ -1,5 +1,8 @@
package org.sonatype.nexus.index.updater.jetty;

import static org.codehaus.plexus.util.IOUtil.close;

import org.apache.maven.wagon.LazyFileOutputStream;
import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
Expand All @@ -17,7 +20,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.zip.GZIPInputStream;

public class JettyResourceFetcher
implements ResourceFetcher
Expand All @@ -36,8 +42,6 @@ public class JettyResourceFetcher

private HttpFields headers;

private int maxRedirects = 4;

// END: configuration fields.

// transient fields.
Expand All @@ -53,29 +57,10 @@ public void retrieve( final String name, final File targetFile )
throws IOException, FileNotFoundException
{
HttpFields exchangeHeaders = buildHeaders();
ResourceExchange exchange = new ResourceExchange( targetFile, exchangeHeaders, maxRedirects, listenerSupport );

ResourceExchange exchange = new ResourceExchange( exchangeHeaders );
exchange.setURL( url );
exchange.setMethod( HttpMethods.GET );

StringBuilder getUrl = new StringBuilder( url );
if ( getUrl.charAt( getUrl.length() - 1 ) != '/' && !name.startsWith( "/" ) )
{
getUrl.append( '/' );
}
getUrl.append( name );

exchange.setURL( getUrl.toString() );

exchange = get( exchange );
while ( exchange.prepareForRedirect() )
{
exchange = get( exchange );
}
}

private ResourceExchange get( final ResourceExchange exchange )
throws IOException
{
httpClient.send( exchange );
try
{
Expand All @@ -94,8 +79,6 @@ private ResourceExchange get( final ResourceExchange exchange )
{
case ServerResponse.SC_OK:
case ServerResponse.SC_NOT_MODIFIED:
case ServerResponse.SC_MOVED_PERMANENTLY:
case ServerResponse.SC_MOVED_TEMPORARILY:
break;

case ServerResponse.SC_FORBIDDEN:
Expand All @@ -119,7 +102,9 @@ private ResourceExchange get( final ResourceExchange exchange )
}
}

return exchange;
getTransfer( targetFile, exchange );
targetFile.setLastModified( exchange.getLastModified() );
listenerSupport.fireGetCompleted( url, targetFile );
}

public void connect( final String id, final String url )
Expand Down Expand Up @@ -278,17 +263,6 @@ public String getId()
}
}

public int getMaxRedirects()
{
return maxRedirects;
}

public JettyResourceFetcher setMaxRedirects( final int maxRedirects )
{
this.maxRedirects = maxRedirects;
return this;
}

public int getMaxConnections()
{
return maxConnections;
Expand Down Expand Up @@ -346,6 +320,78 @@ public JettyResourceFetcher addTransferListener( final TransferListener listener
return this;
}

private void getTransfer( final File targetFile, final ResourceExchange exchange )
throws IOException
{
listenerSupport.fireGetStarted( url, targetFile );

File destinationDirectory = targetFile.getParentFile();
if ( destinationDirectory != null && !destinationDirectory.exists() )
{
destinationDirectory.mkdirs();
if ( !destinationDirectory.exists() )
{
throw new IOException( "Specified destination directory cannot be created: " + destinationDirectory );
}
}

InputStream input = null;
OutputStream output = null;
try
{
input = getResponseContentSource( exchange );
output = new LazyFileOutputStream( targetFile );

byte[] buffer = new byte[4096];

TransferEvent transferEvent =
new TransferEvent( null, listenerSupport.resourceFor( url ), TransferEvent.TRANSFER_PROGRESS,
TransferEvent.REQUEST_GET );

transferEvent.setTimestamp( System.currentTimeMillis() );

int remaining = exchange.getContentLength();
while ( remaining > 0 )
{
int n = input.read( buffer, 0, Math.min( buffer.length, remaining ) );

if ( n == -1 )
{
break;
}

listenerSupport.fireTransferProgress( transferEvent, buffer, n );

output.write( buffer, 0, n );

remaining -= n;
}
output.flush();
}
finally
{
close( input );
close( output );
}
}

private InputStream getResponseContentSource( final ResourceExchange exchange )
throws IOException
{
InputStream source = exchange.getResponseContentSource();

if ( source != null )
{
String contentEncoding = exchange.getContentEncoding();
if ( contentEncoding != null && "gzip".equalsIgnoreCase( contentEncoding ) )
{
source = new GZIPInputStream( source );
}
}

return source;
}

private HttpFields buildHeaders()
{
HttpFields result = new HttpFields();
Expand Down

0 comments on commit 1265c21

Please sign in to comment.