1 19 20 package com.maverick.http; 21 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Vector ; 25 26 32 public abstract class HttpHeader { 33 34 protected final static String WHITE_SPACE = " \t\r"; 36 private Hashtable fields; 37 private Vector fieldNames; 38 39 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpHeader.class); 41 42 44 protected HttpHeader() { 45 fields = new Hashtable (); 46 fieldNames = new Vector (); 47 } 48 49 56 public void addHeaderField(String headerName, String value) { 57 if(value == null) { 58 throw new IllegalArgumentException ("Null value"); 59 } 60 61 if (!fields.containsKey(headerName.toLowerCase())) { 62 fields.put(headerName.toLowerCase(), new Field(headerName, new Vector ())); 63 fieldNames.addElement(headerName); 64 } 65 66 Vector v = ((Field) fields.get(headerName.toLowerCase())).headerValues; 67 v.addElement(value); 68 } 69 70 76 public Enumeration getHeaderFieldNames() { 77 return fieldNames.elements(); 78 } 79 80 89 public String getHeaderField(String headerName, int num) { 90 Field f = ((Field) fields.get(headerName.toLowerCase())); 91 if (f == null) 92 return null; 93 else { 94 if (f.headerValues.size() > num) 95 return (String ) (f.headerValues.elementAt(num)); 96 else 97 return null; 98 } 99 } 100 101 106 public String getHeaderField(String headerName) { 107 return getHeaderField(headerName, 0); 108 } 109 110 117 public void setHeaderField(String headerName, String value) { 118 if(value == null) { 119 throw new IllegalArgumentException ("Null value"); 120 } 121 Vector v = new Vector (); 122 fields.put(headerName.toLowerCase(), new Field(headerName, v)); 123 if(!fieldNames.contains(headerName)) { 124 fieldNames.addElement(headerName); 125 } 126 v.addElement(value); 127 } 128 129 134 public void removeFields(String header) { 135 if (fields.containsKey(header.toLowerCase())) { 136 Field f = (Field)fields.remove(header.toLowerCase()); 137 fieldNames.removeElement(f.n); 138 } 139 } 140 141 144 public void clearHeaderFields() { 145 fieldNames.removeAllElements(); 146 fields.clear(); 147 } 148 149 155 public int getHeaderFieldCount(String headerName) { 156 Field f = (Field) fields.get(headerName.toLowerCase()); 157 return f.headerValues == null ? 0 : f.headerValues.size(); 158 } 159 160 167 public String [] getHeaderFields(String headerName) { 168 Field f = (Field) fields.get(headerName.toLowerCase()); 169 if (f != null) { 170 String [] values = new String [f.headerValues.size()]; 171 f.headerValues.copyInto(values); 172 return values; 173 } else { 174 return null; 175 } 176 } 177 178 185 public String generateOutput(String startline) { 186 String str = startline + "\r\n"; Enumeration it = getHeaderFieldNames(); 188 189 while (it.hasMoreElements()) { 190 String fieldName = (String ) it.nextElement(); 191 int count = getHeaderFieldCount(fieldName); 192 for (int i = 0; i < count; i++) { 193 str += (fieldName + ": " + getHeaderField(fieldName, i) + "\r\n"); } 195 196 } 197 198 str += "\r\n"; return str; 200 } 201 202 class Field { 203 Vector headerValues; 204 String n; 205 206 Field(String n, Vector v) { 207 this.n = n; 208 this.headerValues = v; 209 } 210 } 211 } 212 | Popular Tags |