ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Get.java
Date Fri, 07 Sep 2001 03:23:54 GMT
donaldp     01/09/06 20:23:54

  Modified:    src/main/org/apache/tools/ant/taskdefs Get.java
  Log:
  Updated Get task so that it supports authenticated URLS.
  
  Submitted by: Gautam Guliani <gautamg@grassroots-tech.com>
  
  Revision  Changes    Path
  1.9       +138 -0    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Get.java
  
  Index: Get.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Get.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Get.java	2001/01/03 14:18:30	1.8
  +++ Get.java	2001/09/07 03:23:54	1.9
  @@ -66,6 +66,7 @@
    * Java runtime is correctly configured.
    *
    * @author costin@dnt.ro
  + * @author gg@grtmail.com (Added Java 1.1 style HTTP basic auth)
    */
   public class Get extends Task {
       private URL source; // required
  @@ -73,6 +74,9 @@
       private boolean verbose = false;
       private boolean useTimestamp = false; //off by default
       private boolean ignoreErrors = false;
  +    private String uname = null;
  +    private String pword = null;
  +
       
       /**
        * Does the work.
  @@ -123,6 +127,24 @@
               if(useTimestamp && hasTimestamp) {
                   connection.setIfModifiedSince(timestamp);
               }
  +	    // prepare Java 1.1 style credentials
  +	    if (uname != null || pword != null) {
  +	      String up = uname + ":" + pword;
  +	      String encoding;
  +	      // 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) { // sun's base64 encoder isn't available 
  +		Base64Converter encoder = new Base64Converter();
  +		encoding = encoder.encode(up.getBytes());
  +	      }
  +	      connection.setRequestProperty ("Authorization", "Basic " + encoding);
  +
  +	    }
   
               //connect to the remote site (may take some time)
               connection.connect();
  @@ -136,6 +158,12 @@
                       log("Not modified - so not downloaded");
                       return; 
                   }
  +		// test for 401 result (HTTP only)
  +                if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED)
 {
  +                    log("Not authorized - check " + dest + " for details");
  +                    return; 
  +                }
  +
               }
   
               //REVISIT: at this point even non HTTP connections may support the if-modified-since
  @@ -279,5 +307,115 @@
               useTimestamp = v;
           }
       }
  +
  +
  +    /**
  +     * Username for basic auth.
  +     *
  +     * @param u username for authentication
  +     */
  +    public void setUsername(String u) {
  +      this.uname = u;
  +    }
  +
  +    /**
  +     * password for the basic auth.
  +     *
  +     * @param p password for authentication
  +     */
  +    public void setPassword(String p) {
  +      this.pword = p;
  +    }
  +
  +     /*********************************************************************
  +     * BASE 64 encoding of a String or an array of bytes.
  +     *
  +     * Based on RFC 1421.
  +     * 
  +     * @author
  +     *    Unknown
  +     *  @author
  +     *    <a HREF="gg@grtmail.com">Gautam Guliani</a>
  +     *********************************************************************/
  +
  +     class  Base64Converter
  +     {
  +
  +     public final char [ ]  alphabet = {
  +       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',   //  0 to  7
  +       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',   //  8 to 15
  +       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',   // 16 to 23
  +       'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',   // 24 to 31
  +       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',   // 32 to 39
  +       'o', 'p', 'q', 'r', 's', 't', 'u', 'v',   // 40 to 47
  +       'w', 'x', 'y', 'z', '0', '1', '2', '3',   // 48 to 55
  +       '4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
  +
  +
  +     public String  encode ( String  s )
  +     {
  +       return encode ( s.getBytes ( ) );
  +     }
  +
  +     public String  encode ( byte [ ]  octetString )
  +     {
  +       int  bits24;
  +       int  bits6;
  +
  +       char [ ]  out
  +         = new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
  +
  +       int outIndex = 0;
  +       int i        = 0;
  +
  +       while ( ( i + 3 ) <= octetString.length ) {
  +       // store the octets 
  +	 bits24=( octetString [ i++ ] & 0xFF ) << 16;
  +         bits24 |=( octetString [ i++ ] & 0xFF ) << 8;
  +
  +         bits6=( bits24 & 0x00FC0000 )>> 18; 
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +         bits6 = ( bits24 & 0x0003F000 ) >> 12; 
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +         bits6 = ( bits24 & 0x00000FC0 ) >> 6; 
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +         bits6 = ( bits24 & 0x0000003F );
  +         out [ outIndex++ ] = alphabet [ bits6 ]; 
  +       }
  +
  +       if ( octetString.length - i == 2 )
  +       {
  +         // store the octets 
  +         bits24  = ( octetString [ i     ] & 0xFF ) << 16;
  +         bits24 |=( octetString [ i + 1 ] & 0xFF ) << 8;
  +         bits6=( bits24 & 0x00FC0000 )>> 18;
  +         out [ outIndex++ ] = alphabet [ bits6 ]; 
  +         bits6 = ( bits24 & 0x0003F000 ) >> 12; 
  +         out [ outIndex++ ] = alphabet [ bits6 ]; 
  +         bits6 = ( bits24 & 0x00000FC0 ) >> 6; 
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +
  +         // padding
  +         out [ outIndex++ ] = '='; 
  +       }
  +       else if ( octetString.length - i == 1 )
  +       {
  +         // store the octets 
  +         bits24 = ( octetString [ i ] & 0xFF ) << 16;
  +         bits6=( bits24 & 0x00FC0000 )>> 18;
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +         bits6 = ( bits24 & 0x0003F000 ) >> 12; 
  +         out [ outIndex++ ] = alphabet [ bits6 ];
  +
  +         // padding
  +         out [ outIndex++ ] = '='; 
  +         out [ outIndex++ ] = '='; 
  +       }
  +
  +       return new String ( out );
  +     }
  +
  +     }
  +
   
   }
  
  
  

Mime
View raw message