Source for gnu.java.net.protocol.http.Headers

   1: /* Headers.java --
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.net.protocol.http;
  40: 
  41: import gnu.java.net.LineInputStream;
  42: 
  43: import java.io.IOException;
  44: import java.io.InputStream;
  45: import java.text.DateFormat;
  46: import java.text.ParseException;
  47: import java.util.Collection;
  48: import java.util.Date;
  49: import java.util.Iterator;
  50: import java.util.LinkedHashMap;
  51: import java.util.LinkedHashSet;
  52: import java.util.Map;
  53: import java.util.Set;
  54: 
  55: /**
  56:  * A collection of HTTP header names and associated values.
  57:  * Retrieval of values is case insensitive. An iteration over the keys
  58:  * returns the header names in the order they were received.
  59:  *
  60:  * @author Chris Burdess (dog@gnu.org)
  61:  */
  62: public class Headers
  63:   extends LinkedHashMap
  64: {
  65: 
  66:   static final DateFormat dateFormat = new HTTPDateFormat();
  67: 
  68:   static class Header
  69:   {
  70: 
  71:     final String name;
  72: 
  73:     Header(String name)
  74:     {
  75:       if (name == null || name.length() == 0)
  76:         {
  77:           throw new IllegalArgumentException(name);
  78:         }
  79:       this.name = name;
  80:     }
  81: 
  82:     public int hashCode()
  83:     {
  84:       return name.toLowerCase().hashCode();
  85:     }
  86: 
  87:     public boolean equals(Object other)
  88:     {
  89:       if (other instanceof Header)
  90:         {
  91:           return ((Header) other).name.equalsIgnoreCase(name);
  92:         }
  93:       return false;
  94:     }
  95: 
  96:     public String toString()
  97:     {
  98:       return name;
  99:     }
 100:     
 101:   }
 102: 
 103:   static class HeaderEntry
 104:     implements Map.Entry
 105:   {
 106: 
 107:     final Map.Entry entry;
 108: 
 109:     HeaderEntry(Map.Entry entry)
 110:     {
 111:       this.entry = entry;
 112:     }
 113: 
 114:     public Object getKey()
 115:     {
 116:       return ((Header) entry.getKey()).name;
 117:     }
 118: 
 119:     public Object getValue()
 120:     {
 121:       return entry.getValue();
 122:     }
 123: 
 124:     public Object setValue(Object value)
 125:     {
 126:       return entry.setValue(value);
 127:     }
 128: 
 129:     public int hashCode()
 130:     {
 131:       return entry.hashCode();
 132:     }
 133: 
 134:     public boolean equals(Object other)
 135:     {
 136:       return entry.equals(other);
 137:     }
 138: 
 139:     public String toString()
 140:     {
 141:       return getKey().toString() + "=" + getValue();
 142:     }
 143:     
 144:   }
 145: 
 146:   public Headers()
 147:   {
 148:   }
 149: 
 150:   public boolean containsKey(Object key)
 151:   {
 152:     return super.containsKey(new Header((String) key));
 153:   }
 154: 
 155:   public Object get(Object key)
 156:   {
 157:     return super.get(new Header((String) key));
 158:   }
 159: 
 160:   /**
 161:    * Returns the value of the specified header as a string.
 162:    */
 163:   public String getValue(String header)
 164:   {
 165:     return (String) super.get(new Header(header));
 166:   }
 167: 
 168:   /**
 169:    * Returns the value of the specified header as an integer,
 170:    * or -1 if the header is not present or not an integer.
 171:    */
 172:   public int getIntValue(String header)
 173:   {
 174:     String val = getValue(header);
 175:     if (val == null)
 176:       {
 177:         return -1;
 178:       }
 179:     try
 180:       {
 181:         return Integer.parseInt(val);
 182:       }
 183:     catch (NumberFormatException e)
 184:       {
 185:       }
 186:     return -1;
 187:   }
 188: 
 189:   /**
 190:    * Returns the value of the specified header as a long, or -1 if the
 191:    * header is not present or cannot be parsed as a long.
 192:    */
 193:   public long getLongValue(String header)
 194:   {
 195:     String val = getValue(header);
 196:     if (val == null)
 197:       {
 198:         return -1;
 199:       }
 200:     try
 201:       {
 202:         return Long.parseLong(val);
 203:       }
 204:     catch (NumberFormatException e)
 205:       {
 206:       }
 207:     return -1;
 208:   }
 209: 
 210:   /**
 211:    * Returns the value of the specified header as a date,
 212:    * or <code>null</code> if the header is not present or not a date.
 213:    */
 214:   public Date getDateValue(String header)
 215:   {
 216:     String val = getValue(header);
 217:     if (val == null)
 218:       {
 219:         return null;
 220:       }
 221:     try
 222:       {
 223:         return dateFormat.parse(val);
 224:       }
 225:     catch (ParseException e)
 226:       {
 227:         return null;
 228:       }
 229:   }
 230: 
 231:   public Object put(Object key, Object value)
 232:   {
 233:     return super.put(new Header((String) key), value);
 234:   }
 235: 
 236:   public Object remove(Object key)
 237:   {
 238:     return super.remove(new Header((String) key));
 239:   }
 240: 
 241:   public void putAll(Map t)
 242:   {
 243:     for (Iterator i = t.keySet().iterator(); i.hasNext(); )
 244:       {
 245:         String key = (String) i.next();
 246:         String value = (String) t.get(key);
 247:         put(key, value);
 248:       }
 249:   }
 250:   
 251:   public Set keySet()
 252:   {
 253:     Set keys = super.keySet();
 254:     Set ret = new LinkedHashSet();
 255:     for (Iterator i = keys.iterator(); i.hasNext(); )
 256:       {
 257:         ret.add(((Header) i.next()).name);
 258:       }
 259:     return ret;
 260:   }
 261: 
 262:   public Set entrySet()
 263:   {
 264:     Set entries = super.entrySet();
 265:     Set ret = new LinkedHashSet();
 266:     for (Iterator i = entries.iterator(); i.hasNext(); )
 267:       {
 268:         Map.Entry entry = (Map.Entry) i.next();
 269:         ret.add(new HeaderEntry(entry));
 270:       }
 271:     return ret;
 272:   }
 273: 
 274:   /**
 275:    * Parse the specified input stream, adding headers to this collection.
 276:    */
 277:   public void parse(InputStream in)
 278:     throws IOException
 279:   {
 280:     LineInputStream lin = (in instanceof LineInputStream) ?
 281:       (LineInputStream) in : new LineInputStream(in);
 282:     
 283:     String name = null;
 284:     StringBuilder value = new StringBuilder();
 285:     while (true)
 286:       {
 287:         String line = lin.readLine();
 288:         if (line == null)
 289:           {
 290:             if (name != null)
 291:               {
 292:                 addValue(name, value.toString());
 293:               }
 294:             break;
 295:           }
 296:         int len = line.length();
 297:         if (len < 2)
 298:           {
 299:             if (name != null)
 300:               {
 301:                 addValue(name, value.toString());
 302:               }
 303:             break;
 304:           }
 305:         char c1 = line.charAt(0);
 306:         if (c1 == ' ' || c1 == '\t')
 307:           {
 308:             // Continuation
 309:         int last = len - 1;
 310:         if (line.charAt(last) != '\r')
 311:           ++last;
 312:             value.append(line.substring(0, last));
 313:           }
 314:         else
 315:           {
 316:             if (name != null)
 317:               {
 318:                 addValue(name, value.toString());
 319:               }
 320:             
 321:             int di = line.indexOf(':');
 322:             name = line.substring(0, di);
 323:             value.setLength(0);
 324:             do
 325:               {
 326:                 di++;
 327:               }
 328:             while (di < len && line.charAt(di) == ' ');
 329:         int last = len - 1;
 330:         if (line.charAt(last) != '\r')
 331:           ++last;
 332:             value.append(line.substring(di, last));
 333:           }
 334:       }
 335:   }
 336:   
 337:   private void addValue(String name, String value)
 338:   {
 339:     Header key = new Header(name);
 340:     String old = (String) super.get(key);
 341:     if (old == null)
 342:       {
 343:         super.put(key, value);
 344:       }
 345:     else
 346:       {
 347:         super.put(key, old + ", " + value);
 348:       }
 349:   }
 350:   
 351: }