You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by jv...@apache.org on 2003/01/31 18:14:47 UTC

cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/util HttpUtils.java

jvanzyl     2003/01/31 09:14:47

  Modified:    src/java/org/apache/maven/util HttpUtils.java
  Log:
  o I masked the exeception by mistake which in turn reported that everything was being downloaded which is just wrong.
  
  Revision  Changes    Path
  1.19      +122 -132  jakarta-turbine-maven/src/java/org/apache/maven/util/HttpUtils.java
  
  Index: HttpUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/util/HttpUtils.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- HttpUtils.java	3 Jan 2003 02:37:04 -0000	1.18
  +++ HttpUtils.java	31 Jan 2003 17:14:47 -0000	1.19
  @@ -145,6 +145,7 @@
                                   String proxyUserName,
                                   String proxyPassword,
                                   boolean useChecksum )
  +        throws Exception
       {
           // Get the requested file.
           getFile( url,
  @@ -162,19 +163,26 @@
           {
               File checksumFile = new File( destinationFile + ".md5" );
   
  -            getFile( url + ".md5",
  -                     checksumFile,
  -                     file + ".md5",
  -                     ignoreErrors,
  -                     useTimestamp,
  -                     proxyHost,
  -                     proxyPort,
  -                     proxyUserName,
  -                     proxyPassword );
  +            try
  +            {
  +                getFile( url + ".md5",
  +                         checksumFile,
  +                         file + ".md5",
  +                         ignoreErrors,
  +                         useTimestamp,
  +                         proxyHost,
  +                         proxyPort,
  +                         proxyUserName,
  +                         proxyPassword );
  +            }
  +            catch ( Exception e )
  +            {
  +                // do nothing we will check later in the process
  +                // for the checksums.
  +            }
           }
       }
   
  -
       /**
        * Retrieve a remote file.  Returns true if the file was successfully
        * retrieved or if it is up to date (when the useTimestamp flag is set).
  @@ -192,159 +200,141 @@
        *        or null
        * @param proxyPassword Proxy Password (if authentification is required),
        *        or null
  -     * @return true if the retrieval succeeded, false otherwise
        */
  -    public static boolean getFile( String url,
  -                                   File destinationFile,
  -                                   String file,
  -                                   boolean ignoreErrors,
  -                                   boolean useTimestamp,
  -                                   String proxyHost,
  -                                   String proxyPort,
  -                                   String proxyUserName,
  -                                   String proxyPassword )
  +    public static void getFile( String url,
  +                                File destinationFile,
  +                                String file,
  +                                boolean ignoreErrors,
  +                                boolean useTimestamp,
  +                                String proxyHost,
  +                                String proxyPort,
  +                                String proxyUserName,
  +                                String proxyPassword )
  +        throws Exception
       {
  -        boolean retrievedFile = false;
  -
           String[] s = parseUrl( url );
           String username = s[0];
           String password = s[1];
           String parsedUrl = s[2];
   
  -        try
  -        {
  -            URL source = new URL( parsedUrl );
  +        URL source = new URL( parsedUrl );
   
  -            //set the timestamp to the file date.
  -            long timestamp = 0;
  -            boolean hasTimestamp = false;
  -            if ( useTimestamp && destinationFile.exists() )
  -            {
  -                timestamp = destinationFile.lastModified();
  -                hasTimestamp = true;
  -            }
  +        //set the timestamp to the file date.
  +        long timestamp = 0;
  +        boolean hasTimestamp = false;
  +        if ( useTimestamp && destinationFile.exists() )
  +        {
  +            timestamp = destinationFile.lastModified();
  +            hasTimestamp = true;
  +        }
   
  -            //set proxy connection
  -            useProxyUser( proxyHost, proxyPort, proxyUserName, proxyPassword );
  +        //set proxy connection
  +        useProxyUser( proxyHost, proxyPort, proxyUserName, proxyPassword );
   
  -            //set up the URL connection
  -            URLConnection connection = source.openConnection();
  -            //modify the headers
  -            //NB: things like user authentication could go in here too.
  -            if ( useTimestamp && hasTimestamp )
  +        //set up the URL connection
  +        URLConnection connection = source.openConnection();
  +        //modify the headers
  +        //NB: things like user authentication could go in here too.
  +        if ( useTimestamp && hasTimestamp )
  +        {
  +            connection.setIfModifiedSince( timestamp );
  +        }
  +        // prepare Java 1.1 style credentials
  +        if ( username != null || password != null )
  +        {
  +            String up = username + ":" + password;
  +            String encoding = null;
  +            // check to see if sun's Base64 encoder is available.
  +            try
               {
  -                connection.setIfModifiedSince( timestamp );
  +                sun.misc.BASE64Encoder encoder =
  +                    (sun.misc.BASE64Encoder) Class.forName(
  +                        "sun.misc.BASE64Encoder" ).newInstance();
  +
  +                encoding = encoder.encode( up.getBytes() );
               }
  -            // prepare Java 1.1 style credentials
  -            if ( username != null || password != null )
  +            catch ( Exception ex )
               {
  -                String up = username + ":" + password;
  -                String encoding = null;
  -                // check to see if sun's Base64 encoder is available.
  -                try
  -                {
  -                    sun.misc.BASE64Encoder encoder =
  -                        (sun.misc.BASE64Encoder) Class.forName(
  -                            "sun.misc.BASE64Encoder" ).newInstance();
  -
  -                    encoding = encoder.encode( up.getBytes() );
  -                }
  -                catch ( Exception ex )
  -                {
  -                    // Do nothing, as for MavenSession we will never use
  -                    // auth and we will eventually move over httpclient
  -                    // in the commons.
  -                }
  -                connection.setRequestProperty( "Authorization", "Basic " + encoding );
  +                // Do nothing, as for MavenSession we will never use
  +                // auth and we will eventually move over httpclient
  +                // in the commons.
               }
  +            connection.setRequestProperty( "Authorization", "Basic " + encoding );
  +        }
   
  -            //connect to the remote site (may take some time)
  -            connection.connect();
  -            //next test for a 304 result (HTTP only)
  -            if ( connection instanceof HttpURLConnection )
  +        //connect to the remote site (may take some time)
  +        connection.connect();
  +        //next test for a 304 result (HTTP only)
  +        if ( connection instanceof HttpURLConnection )
  +        {
  +            HttpURLConnection httpConnection = (HttpURLConnection) connection;
  +            if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED )
               {
  -                HttpURLConnection httpConnection = (HttpURLConnection) connection;
  -                if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED )
  -                {
  -                    return true;
  -                }
  -                // test for 401 result (HTTP only)
  -                if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED )
  -                {
  -                    //!! We need to throw an exception so a useful message can be displayed.
  -                    return false;
  -                }
  +                return;
               }
  +            // test for 401 result (HTTP only)
  +            if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED )
  +            {
  +                throw new Exception( "Not authorized." );
  +            }
  +        }
   
  -            // REVISIT: at this point even non HTTP connections may support the
  -            // if-modified-since behaviour - we just check the date of the
  -            // content and skip the write if it is not newer.
  -            // Some protocols (FTP) dont include dates, of course.
  +        // REVISIT: at this point even non HTTP connections may support the
  +        // if-modified-since behaviour - we just check the date of the
  +        // content and skip the write if it is not newer.
  +        // Some protocols (FTP) dont include dates, of course.
   
  -            InputStream is = null;
  -            for ( int i = 0; i < 3; i++ )
  +        InputStream is = null;
  +        for ( int i = 0; i < 3; i++ )
  +        {
  +            try
               {
  -                try
  -                {
  -                    is = connection.getInputStream();
  -                    break;
  -                }
  -                catch ( IOException ex )
  -                {
  -                    // do nothing
  -                }
  +                is = connection.getInputStream();
  +                break;
               }
  -            if ( is == null )
  +            catch ( IOException ex )
               {
  -                if ( ignoreErrors )
  -                {
  -                    return false;
  -                }
  -
  -                // This will never happen with maven's use of this class.
  -                throw new Exception( "Can't get " + file + " to " + destinationFile );
  +                // do nothing
               }
  -
  -            FileOutputStream fos = new FileOutputStream( destinationFile );
  -
  -            byte[] buffer = new byte[100 * 1024];
  -            int length;
  -
  -            while ( ( length = is.read( buffer ) ) >= 0 )
  +        }
  +        if ( is == null )
  +        {
  +            if ( ignoreErrors )
               {
  -                fos.write( buffer, 0, length );
  -                System.out.print( "." );
  +                return;
               }
   
  -            System.out.println();
  -            fos.close();
  -            is.close();
  +            // This will never happen with maven's use of this class.
  +            throw new Exception( "Can't get " + file + " to " + destinationFile );
  +        }
   
  -            // if (and only if) the use file time option is set, then the
  -            // saved file now has its timestamp set to that of the downloaded
  -            // file
  -            if ( useTimestamp )
  -            {
  -                long remoteTimestamp = connection.getLastModified();
  +        FileOutputStream fos = new FileOutputStream( destinationFile );
   
  -                if ( remoteTimestamp != 0 )
  -                {
  -                    touchFile( destinationFile, remoteTimestamp );
  -                }
  -            }
  +        byte[] buffer = new byte[100 * 1024];
  +        int length;
   
  -            retrievedFile = true;
  -        }
  -        catch ( FileNotFoundException fnfe )
  +        while ( ( length = is.read( buffer ) ) >= 0 )
           {
  -            // do nothing.
  +            fos.write( buffer, 0, length );
  +            System.out.print( "." );
           }
  -        catch ( Exception e )
  +
  +        System.out.println();
  +        fos.close();
  +        is.close();
  +
  +        // if (and only if) the use file time option is set, then the
  +        // saved file now has its timestamp set to that of the downloaded
  +        // file
  +        if ( useTimestamp )
           {
  -            // do nothing.
  -        }
  +            long remoteTimestamp = connection.getLastModified();
   
  -        return retrievedFile;
  +            if ( remoteTimestamp != 0 )
  +            {
  +                touchFile( destinationFile, remoteTimestamp );
  +            }
  +        }
       }
   
       /**
  @@ -371,9 +361,9 @@
           int i = url.indexOf( "@" );
           if ( i > 0 )
           {
  -            String s = url.substring(7, i);
  +            String s = url.substring( 7, i );
               int j = s.indexOf( ":" );
  -            parsedUrl[0] = s.substring(0, j);
  +            parsedUrl[0] = s.substring( 0, j );
               parsedUrl[1] = s.substring( j + 1 );
               parsedUrl[2] = "http://" + url.substring( i + 1 );
           }