maven-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jvan...@apache.org
Subject cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/util HttpUtils.java
Date Tue, 10 Dec 2002 04:33:15 GMT
jvanzyl     2002/12/09 20:33:15

  Modified:    src/java/org/apache/maven/util HttpUtils.java
  Log:
  o Adding support for Basic Auth
  
  Revision  Changes    Path
  1.15      +47 -10    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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- HttpUtils.java	10 Dec 2002 02:19:22 -0000	1.14
  +++ HttpUtils.java	10 Dec 2002 04:33:15 -0000	1.15
  @@ -114,7 +114,7 @@
       /**
        * 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).
  -     * @param source the {@link URL} of the file to retrieve
  +     * @param url the of the file to retrieve
        * @param destinationFile where to store it
        * @param file <strong>doesn't appear to be used</strong>
        * @param ignoreErrors whether to ignore errors during I/O or throw an
  @@ -129,7 +129,7 @@
        *        or null
        * @return true if the retrieval succeeded, false otherwise
        */
  -    public static boolean getFile( URL source,
  +    public static boolean getFile( String url,
                                      File destinationFile,
                                      String file,
                                      boolean ignoreErrors,
  @@ -140,11 +140,16 @@
                                      String proxyPassword )
       {
           boolean retrievedFile = false;
  -        String uname = null;
  -        String pword = null;
  +
  +        String[] s = parseUrl( url );
  +        String username = s[0];
  +        String password = s[1];
  +        String parsedUrl = s[2];
   
           try
           {
  +            URL source = new URL( parsedUrl );
  +
               //set the timestamp to the file date.
               long timestamp = 0;
               boolean hasTimestamp = false;
  @@ -166,9 +171,9 @@
                   connection.setIfModifiedSince( timestamp );
               }
               // prepare Java 1.1 style credentials
  -            if ( uname != null || pword != null )
  +            if ( username != null || password != null )
               {
  -                String up = uname + ":" + pword;
  +                String up = username + ":" + password;
                   String encoding = null;
                   // check to see if sun's Base64 encoder is available.
                   try
  @@ -185,8 +190,7 @@
                       // auth and we will eventually move over httpclient
                       // in the commons.
                   }
  -                connection.setRequestProperty( "Authorization",
  -                                               "Basic " + encoding );
  +                connection.setRequestProperty( "Authorization", "Basic " + encoding );
               }
   
               //connect to the remote site (may take some time)
  @@ -234,8 +238,7 @@
                   }
   
                   // This will never happen with maven's use of this class.
  -                throw new Exception(
  -                    "Can't get " + file + " to " + destinationFile );
  +                throw new Exception( "Can't get " + file + " to " + destinationFile );
               }
   
               FileOutputStream fos = new FileOutputStream( destinationFile );
  @@ -278,6 +281,40 @@
           }
   
           return retrievedFile;
  +    }
  +
  +    /**
  +     * Parse an url which might contain a username and password. If the
  +     * given url doesn't contain a username and password then return the
  +     * origin url unchanged.
  +     *
  +     * @param url The url to parse.
  +     * @return The username, password and url.
  +     */
  +    static String[] parseUrl( String url )
  +    {
  +        String[] parsedUrl = new String[3];
  +        parsedUrl[0] = null;
  +        parsedUrl[1] = null;
  +        parsedUrl[2] = url;
  +
  +        // We want to be able to deal with Basic Auth where the username
  +        // and password are part of the URL. An example of the URL string
  +        // we would like to be able to parse is like the following:
  +        //
  +        // http://username:password@repository.mycompany.com
  +
  +        int i = url.indexOf( "@" );
  +        if ( i > 0 )
  +        {
  +            String s = url.substring(7, i);
  +            int j = s.indexOf( ":" );
  +            parsedUrl[0] = s.substring(0, j);
  +            parsedUrl[1] = s.substring( j + 1 );
  +            parsedUrl[2] = "http://" + url.substring( i + 1 );
  +        }
  +
  +        return parsedUrl;
       }
   
       /**
  
  
  

Mime
View raw message