maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject cvs commit: maven-wagon/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http HttpWagon.java
Date Wed, 09 Feb 2005 09:54:48 GMT
brett       2005/02/09 01:54:48

  Modified:    wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http
                        HttpWagon.java
  Log:
  backport fixes from maven 1
  
  Revision  Changes    Path
  1.6       +162 -109  maven-wagon/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpWagon.java
  
  Index: HttpWagon.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpWagon.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HttpWagon.java	8 Feb 2005 06:42:31 -0000	1.5
  +++ HttpWagon.java	9 Feb 2005 09:54:48 -0000	1.6
  @@ -1,7 +1,7 @@
   package org.apache.maven.wagon.providers.http;
   
   /*
  - * Copyright 2001-2004 The Apache Software Foundation.
  + * Copyright 2001-2005 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -24,6 +24,7 @@
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
   import org.apache.commons.httpclient.UsernamePasswordCredentials;
  +import org.apache.commons.httpclient.NTCredentials;
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.PutMethod;
   import org.apache.commons.httpclient.util.DateParseException;
  @@ -40,6 +41,10 @@
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.io.InputStream;
  +import java.util.Locale;
  +import java.util.Date;
  +import java.util.TimeZone;
  +import java.text.SimpleDateFormat;
   
   /**
    * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  @@ -56,6 +61,8 @@
   
       private int numberOfAttempts = DEFAULT_NUMBER_OF_ATTEMPTS;
   
  +    private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
  +
       public void openConnection()
       {
           client = new HttpClient( new MultiThreadedHttpConnectionManager() );
  @@ -80,6 +87,7 @@
               Credentials creds = new UsernamePasswordCredentials( username, password );
   
               client.getState().setCredentials( null, host, creds );
  +            client.getState().setAuthenticationPreemptive( true );
           }
   
           HostConfiguration hc = new HostConfiguration();
  @@ -92,13 +100,31 @@
   
               String proxyHost = proxyInfo.getHost();
   
  -            if ( StringUtils.isNotEmpty( proxyUsername )
  -                && StringUtils.isNotEmpty( proxyPassword )
  -                && StringUtils.isNotEmpty( proxyHost ) )
  +            int proxyPort = proxyInfo.getPort();
  +
  +            String proxyNtlmHost = proxyInfo.getNtlmHost();
  +
  +            String proxyNtlmDomain = proxyInfo.getNtlmDomain();
  +
  +            if ( proxyHost != null )
               {
  -                Credentials creds = new UsernamePasswordCredentials( username, password
);
  +                hc.setProxy( proxyHost, proxyPort );
  +
  +                if ( proxyUsername != null && proxyPassword != null )
  +                {
  +                    Credentials creds;
  +                    if ( proxyNtlmHost != null || proxyNtlmDomain != null )
  +                    {
  +                        creds = new NTCredentials( proxyUsername, proxyPassword, proxyNtlmHost,
proxyNtlmDomain );
  +                    }
  +                    else
  +                    {
  +                        creds = new UsernamePasswordCredentials( proxyUsername, proxyPassword
);
  +                    }
   
  -                client.getState().setProxyCredentials( null, proxyHost, creds );
  +                    client.getState().setProxyCredentials( null, proxyHost, creds );
  +                    client.getState().setAuthenticationPreemptive( true );
  +                }
               }
           }
   
  @@ -173,7 +199,8 @@
                   break;
   
               case SC_NULL:
  -                throw new ResourceDoesNotExistException( "File: " + url + " does not extist"
);
  +                throw new TransferFailedException( "Failed to transfer file: " + url +
" after " + attempt +
  +                                                   " attempts" );
   
               case HttpStatus.SC_FORBIDDEN:
                   throw new AuthorizationException( "Access denided to: " + url );
  @@ -183,7 +210,8 @@
   
                   //add more entries here
               default :
  -                throw new TransferFailedException( "Failed to trasfer file: " + url + ".
Return code is: " + statusCode );
  +                throw new TransferFailedException( "Failed to transfer file: " + url +
". Return code is: " +
  +                                                   statusCode );
           }
   
           putMethod.releaseConnection();
  @@ -198,20 +226,19 @@
       public void get( String resourceName, File destination )
          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
       {
  -        get( resourceName, destination, 0, false );
  +        get( resourceName, destination, 0 );
       }
   
       public boolean getIfNewer( String resourceName, File destination, long timestamp )
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
       {
  -        return get( resourceName, destination, timestamp, true );
  +        return get( resourceName, destination, timestamp );
       }
   
       /**
        *
        * @param resourceName
        * @param destination
  -     * @param timestamp
  -     * @param newerRequired
  +     * @param timestamp the timestamp to check against, only downloading if newer. If <code>0</code>,
always download
        * @return
        * @throws TransferFailedException
        * @throws ResourceDoesNotExistException
  @@ -219,7 +246,7 @@
        *
        * @return <code>true</code> if newer version was downloaded, <code>false</code>
otherwise.
        */
  -    public boolean get( String resourceName, File destination, long timestamp, boolean
newerRequired )
  +    public boolean get( String resourceName, File destination, long timestamp )
           throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
       {
   
  @@ -229,143 +256,169 @@
   
           GetMethod getMethod = new GetMethod( url );
   
  -        getMethod.addRequestHeader( "Cache-control", "no-cache" );
  -
  -        getMethod.addRequestHeader( "Cache-store", "no-store" );
  -
  -        getMethod.addRequestHeader( "Pragma", "no-cache" );
  +        try
  +        {
  +            getMethod.addRequestHeader( "Cache-control", "no-cache" );
   
  -        getMethod.addRequestHeader( "Expires", "0" );
  +            getMethod.addRequestHeader( "Cache-store", "no-store" );
   
  -        int statusCode = SC_NULL;
  +            getMethod.addRequestHeader( "Pragma", "no-cache" );
   
  -        int attempt = 0;
  +            getMethod.addRequestHeader( "Expires", "0" );
   
  -        // We will retry up to NumberOfAttempts times.
  -        while ( ( statusCode == SC_NULL ) && ( attempt < getNumberOfAttempts()
) )
  -        {
  -            try
  +            if ( timestamp > 0 )
               {
  -                // execute the getMethod.
  -                statusCode = client.executeMethod( getMethod );
  +                SimpleDateFormat fmt = new SimpleDateFormat( "EEE, dd-MMM-yy HH:mm:ss zzz",
Locale.US );
  +                fmt.setTimeZone( GMT_TIME_ZONE );
  +                Header hdr = new Header( "If-Modified-Since", fmt.format( new Date( timestamp
) ) );
  +                fireTransferDebug( "sending ==> " + hdr + "(" + timestamp + ")" );
  +                getMethod.addRequestHeader( hdr );
               }
  -            catch ( HttpRecoverableException e )
  -            {
  -                attempt++;
   
  -                continue;
  -            }
  -            catch ( IOException e )
  -            {
  -                throw new TransferFailedException( e.getMessage(), e );
  -            }
  -        }
  +            int statusCode = SC_NULL;
   
  -        fireTransferDebug( url + " - Status code: " + statusCode );
  +            int attempt = 0;
   
  -        // Check that we didn't run out of retries.
  -        switch ( statusCode )
  -        {
  -            case HttpStatus.SC_OK:
  -                break;
  +            // We will retry up to NumberOfAttempts times.
  +            while ( ( statusCode == SC_NULL ) && ( attempt < getNumberOfAttempts()
) )
  +            {
  +                try
  +                {
  +                    // execute the getMethod.
  +                    statusCode = client.executeMethod( getMethod );
  +                }
  +                catch ( HttpRecoverableException e )
  +                {
  +                    attempt++;
   
  -            case SC_NULL:
  -                throw new ResourceDoesNotExistException( "File: " + url + " does not extist"
);
  +                    continue;
  +                }
  +                catch ( IOException e )
  +                {
  +                    throw new TransferFailedException( e.getMessage(), e );
  +                }
  +            }
   
  -            case HttpStatus.SC_FORBIDDEN:
  -                throw new AuthorizationException( "Access denided to: " + url );
  +            fireTransferDebug( url + " - Status code: " + statusCode );
   
  -            case HttpStatus.SC_NOT_FOUND:
  -                throw new ResourceDoesNotExistException( "File: " + url + " does not exist"
);
  +            // TODO [BP]: according to httpclient docs, really should swallow the output
on error. verify if that is required
  +            switch ( statusCode )
  +            {
  +                case HttpStatus.SC_OK:
  +                    break;
   
  -                //add more entries here
  -            default :
  -                throw new TransferFailedException( "Failed to trasfer file: "
  -                                                   + url
  -                                                   + ". Return code is: "
  -                                                   + statusCode );
  -        }
  +                case HttpStatus.SC_NOT_MODIFIED:
  +                    return false;
   
  -        Resource resource = new Resource( resourceName );
  +                case SC_NULL:
  +                    throw new TransferFailedException( "Failed to transfer file: " + url
+ " after " + attempt +
  +                                                       " attempts" );
   
  -        InputStream is = null;
  +                case HttpStatus.SC_FORBIDDEN:
  +                    throw new AuthorizationException( "Access denided to: " + url );
   
  -        Header contentLengthHeader = getMethod.getResponseHeader( "Content-Length" );
  +                case HttpStatus.SC_UNAUTHORIZED:
  +                    throw new AuthorizationException( "Not authorized." );
   
  -        if ( contentLengthHeader != null )
  -        {
  -            try
  -            {
  -                long contentLength = Integer.valueOf( contentLengthHeader.getValue() ).intValue();
  +                case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
  +                    throw new AuthorizationException( "Not authorized by proxy." );
   
  -                resource.setContentLength( contentLength );
  -            }
  -            catch ( NumberFormatException e )
  -            {
  -                fireTransferDebug( "error parsing content length header '" + contentLengthHeader.getValue()
+ "' " + e );
  +                case HttpStatus.SC_NOT_FOUND:
  +                    throw new ResourceDoesNotExistException( "File: " + url + " does not
exist" );
  +
  +                    //add more entries here
  +                default :
  +                    throw new TransferFailedException( "Failed to trasfer file: " + url
+ ". Return code is: " +
  +                                                       statusCode );
               }
  -        }
   
  -        Header lastModifiedHeader = getMethod.getResponseHeader( "Last-Modified" );
  +            Resource resource = new Resource( resourceName );
   
  -        long lastModified = 0;
  +            InputStream is = null;
   
  -        if ( lastModifiedHeader != null )
  -        {
  -             try
  -             {
  -                lastModified = DateParser.parseDate( lastModifiedHeader.getValue() ).getTime();
  -
  -                resource.setLastModified(  lastModified );
  -             }
  -             catch ( DateParseException e )
  -             {
  -                fireTransferDebug( "Unable to parse last modified header" );
  -             }
  +            Header contentLengthHeader = getMethod.getResponseHeader( "Content-Length"
);
   
  -            fireTransferDebug( "last-modified = " + lastModifiedHeader.getValue() + " ("
+ lastModified + ")" );
  -        }
  +            if ( contentLengthHeader != null )
  +            {
  +                try
  +                {
  +                    long contentLength = Integer.valueOf( contentLengthHeader.getValue()
).intValue();
  +
  +                    resource.setContentLength( contentLength );
  +                }
  +                catch ( NumberFormatException e )
  +                {
  +                    fireTransferDebug( "error parsing content length header '" + contentLengthHeader.getValue()
+ "' " + e );
  +                }
  +            }
   
  -        //@todo have to check how m1 does it
  -        boolean isNewer = timestamp < lastModified;
  +            Header lastModifiedHeader = getMethod.getResponseHeader( "Last-Modified" );
   
  -        if(  ( isNewer && newerRequired )  || ( !newerRequired )  )
  -        {
  -            retValue = true;
  +            long lastModified = 0;
   
  -            try
  +            if ( lastModifiedHeader != null )
               {
  -                is = getMethod.getResponseBodyAsStream();
  +                try
  +                {
  +                    lastModified = DateParser.parseDate( lastModifiedHeader.getValue()
).getTime();
  +                }
  +                catch ( DateParseException e )
  +                {
  +                    fireTransferDebug( "Unable to parse last modified header" );
  +                }
   
  -                getTransfer( resource, destination, is );
  +                fireTransferDebug( "last-modified = " + lastModifiedHeader.getValue() +
" (" + lastModified + ")" );
               }
  -            catch ( Exception e )
  +
  +            if ( timestamp < lastModified )
               {
  -                fireTransferError( resource, e );
  +                retValue = true;
   
  -                if ( destination.exists() )
  +                try
  +                {
  +                    is = getMethod.getResponseBodyAsStream();
  +
  +                    getTransfer( resource, destination, is );
  +                }
  +                catch ( Exception e )
                   {
  -                    boolean deleted = destination.delete();
  +                    fireTransferError( resource, e );
   
  -                    if ( ! deleted )
  +                    if ( destination.exists() )
                       {
  -                        destination.deleteOnExit();
  +                        boolean deleted = destination.delete();
  +
  +                        if ( ! deleted )
  +                        {
  +                            destination.deleteOnExit();
  +                        }
                       }
  -                }
   
  -                String msg = "Error occured while deploying to remote repository:" + getRepository();
  +                    String msg = "Error occured while deploying to remote repository:"
+ getRepository();
  +
  +                    throw new TransferFailedException( msg, e );
  +                }
  +                finally
  +                {
  +                    shutdownStream( is );
  +                }
   
  -                throw new TransferFailedException( msg, e );
  +                if ( lastModified > 0 )
  +                {
  +                    resource.setLastModified( lastModified );
  +                }
               }
  -            finally
  +            else
               {
  -                shutdownStream( is );
  +                fireTransferDebug( "Local file is newer: not downloaded" );
               }
   
  +            return retValue;
  +        }
  +        finally
  +        {
  +            getMethod.releaseConnection();
           }
  -        getMethod.releaseConnection();
  -
  -        return retValue;
       }
   
       public int getNumberOfAttempts()
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org


Mime
View raw message