Clover coverage report - Cactus 1.4.1 for J2EE API 13
Coverage timestamp: Sat Aug 31 2002 22:02:23 BST
file stats: LOC: 153   Methods: 5
NCLOC: 89   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BasicAuthentication.java 71.4% 88.7% 100% 83.7%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus.client.authentication;
 3   
 import org.apache.cactus.WebRequest;
 4   
 import java.text.CharacterIterator;
 5   
 import java.text.StringCharacterIterator;
 6   
 
 7   
 /** 
 8   
  * Basic Authentication support. 
 9   
  * 
 10   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 11   
  * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a> 
 12   
  * 
 13   
  * @since 1.3 
 14   
  * @see AbstractAuthentication 
 15   
  * 
 16   
  * @version $Id: BasicAuthentication.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 17   
  */
 18   
 public class BasicAuthentication extends AbstractAuthentication {
 19   
   /** 
 20   
        * Provides encoding of raw bytes to base64-encoded characters, and 
 21   
        * decoding of base64 characters to raw bytes. 
 22   
        */
 23   
   private static char[] alphabet;
 24   
   /** 
 25   
        * Lookup table for converting base64 characters to value in range 0..63 
 26   
        */
 27   
   private static byte[] codes;
 28   
   /** 
 29   
        * @param theName user name of the Credential 
 30   
        * @param thePassword user password of the Credential 
 31   
        */
 32  5
   public BasicAuthentication(String theName, String thePassword) {
 33  5
     super(theName, thePassword);
 34   
     ;
 35   
   } 
 36   
   /** 
 37   
        * @see AbstractAuthentication#validateName(String) 
 38   
        */
 39  5
   protected void validateName(String theName) {
 40  5
     if (theName == null) {
 41  0
       return;
 42   
     } 
 43  5
     String illegalChars = "()<>@,;:\\\"/[]?={} \t";
 44  5
     StringCharacterIterator iter = new StringCharacterIterator(theName);
 45  5
     for (char c = iter.first(); c != ((char)65535); c = iter.next()) {
 46  40
       if ((illegalChars.indexOf(c) != -1) || ((c >= 0) && (c <= 31)) || (c == 127)) {
 47  0
         throw new IllegalArgumentException("Given theName contains illegal characters.");
 48   
       } 
 49   
     } 
 50   
   } 
 51   
 
 52   
   /** 
 53   
        * @see AbstractAuthentication#validatePassword(String) 
 54   
        */
 55  5
   protected void validatePassword(String thePassword) {
 56  5
     if (thePassword == null) {
 57  0
       return;
 58   
     } 
 59  5
     String exceptionChars = "\r\n \t";
 60  5
     StringCharacterIterator iter = new StringCharacterIterator(thePassword);
 61  5
     for (char c = iter.first(); c != ((char)65535); c = iter.next()) {
 62  60
       if (((c >= 0) && (c <= 31)) || (c == 127)) {
 63  0
         if (exceptionChars.indexOf(c) != -1) {
 64  0
           continue;
 65   
         } 
 66  0
         throw new IllegalArgumentException("Given thePassword contains illegal characters.");
 67   
       } 
 68   
     } 
 69   
   } 
 70   
 
 71   
   /** 
 72   
        * @see AbstractAuthentication#configure(WebRequest) 
 73   
        */
 74  10
   public void configure(WebRequest theRequest) {
 75  10
     String basicCookie = this.getName() + ":" + this.getPassword();
 76  10
     String basicCredentials = "Basic " + new String(BasicAuthentication.base64Encode(
 77   
         basicCookie.getBytes()));
 78  10
     theRequest.addHeader("Authorization", basicCredentials);
 79   
   } 
 80   
 
 81   
   /** 
 82   
        * returns an array of base64-encoded characters to represent the 
 83   
        * passed theData array. 
 84   
        * 
 85   
        * @param theData the array of bytes to encode 
 86   
        * @return base64-coded character array. 
 87   
        */
 88  10
   private static char[] base64Encode(byte[] theData) {
 89  10
     char[] out = new char[((theData.length + 2) / 3) * 4];
 90  10
     for (int i = 0,  index = 0; i < theData.length; i += 3, index += 4) {
 91  70
       boolean quad = false;
 92  70
       boolean trip = false;
 93  70
       int val = (255 & (int)theData[i]);
 94  70
       val <<= 8;
 95  70
       if ((i + 1) < theData.length) {
 96  70
         val |= (255 & (int)theData[i + 1]);
 97  70
         trip = true;
 98   
       } 
 99  70
       val <<= 8;
 100  70
       if ((i + 2) < theData.length) {
 101  70
         val |= (255 & (int)theData[i + 2]);
 102  70
         quad = true;
 103   
       } 
 104  70
       out[index + 3] = BasicAuthentication.alphabet[(quad ? (val & 63) : 64)];
 105  70
       val >>= 6;
 106  70
       out[index + 2] = BasicAuthentication.alphabet[(trip ? (val & 63) : 64)];
 107  70
       val >>= 6;
 108  70
       out[index + 1] = BasicAuthentication.alphabet[val & 63];
 109  70
       val >>= 6;
 110  70
       out[index + 0] = BasicAuthentication.alphabet[val & 63];
 111   
     } 
 112  10
     return out;
 113   
   } 
 114   
 
 115   
   /** 
 116   
    * Basic Authentication support. 
 117   
    * 
 118   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 119   
    * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a> 
 120   
    * 
 121   
    * @since 1.3 
 122   
    * @see AbstractAuthentication 
 123   
    * 
 124   
    * @version $Id: BasicAuthentication.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 125   
    */
 126   
   static {
 127   
     /** 
 128   
          * Provides encoding of raw bytes to base64-encoded characters, and 
 129   
          * decoding of base64 characters to raw bytes. 
 130   
          */
 131  5
     BasicAuthentication.alphabet = 
 132   
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
 133   
     /** 
 134   
          * Lookup table for converting base64 characters to value in range 0..63 
 135   
          */
 136  5
     BasicAuthentication.codes = new byte[256];
 137  5
     for (int i = 0; i < 256; i++) {
 138  1280
       BasicAuthentication.codes[i] = -1;
 139   
     } 
 140  5
     for (int i = 'A'; i <= 'Z'; i++) {
 141  130
       BasicAuthentication.codes[i] = (byte)(i - 'A');
 142   
     } 
 143  5
     for (int i = 'a'; i <= 'z'; i++) {
 144  130
       BasicAuthentication.codes[i] = (byte)(26 + i - 'a');
 145   
     } 
 146  5
     for (int i = '0'; i <= '9'; i++) {
 147  50
       BasicAuthentication.codes[i] = (byte)(52 + i - '0');
 148   
     } 
 149  5
     BasicAuthentication.codes['+'] = 62;
 150  5
     BasicAuthentication.codes['/'] = 63;
 151   
   } 
 152   
 
 153   
 }