KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > HttpHeader


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Encapsulates a list of <i>HTTP header</i>. Each request or response may consist
28  * of multiple headers. Each named header may contain 1 or more values.
29  *
30  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
31  */

32 public abstract class HttpHeader {
33
34     protected final static String JavaDoc WHITE_SPACE = " \t\r"; //$NON-NLS-1$
35

36     private Hashtable JavaDoc fields;
37     private Vector JavaDoc fieldNames;
38
39     // #ifdef DEBUG
40
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpHeader.class);
41
42     // #endif
43

44     protected HttpHeader() {
45         fields = new Hashtable JavaDoc();
46         fieldNames = new Vector JavaDoc();
47     }
48
49     /**
50      * Add a header field. <b>If headers with the same name already exist
51      * they will not be replaced</b>.
52      *
53      * @param headerName header name
54      * @param value value
55      */

56     public void addHeaderField(String JavaDoc headerName, String JavaDoc value) {
57         if(value == null) {
58             throw new IllegalArgumentException JavaDoc("Null value");
59         }
60
61         if (!fields.containsKey(headerName.toLowerCase())) {
62             fields.put(headerName.toLowerCase(), new Field(headerName, new Vector JavaDoc()));
63             fieldNames.addElement(headerName);
64         }
65
66         Vector JavaDoc v = ((Field) fields.get(headerName.toLowerCase())).headerValues;
67         v.addElement(value);
68     }
69
70     /**
71      * Get an enumeration of all unique header names. Each header may have
72      * multiple values.
73      *
74      * @return header field names
75      */

76     public Enumeration JavaDoc getHeaderFieldNames() {
77         return fieldNames.elements();
78     }
79
80     /**
81      * Each named header may have multiple values. This method allows you
82      * to retrieve a specific index in the list of values for the required
83      * header. If not such header exists <code>null</code> will be returned.
84      *
85      * @param headerName header name
86      * @param num index of header value
87      * @return header value
88      */

89     public String JavaDoc getHeaderField(String JavaDoc 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 JavaDoc) (f.headerValues.elementAt(num));
96             else
97                 return null;
98         }
99     }
100
101     /**
102      * Get the first header for a given name.
103      *
104      * @param headerName String
105      */

106     public String JavaDoc getHeaderField(String JavaDoc headerName) {
107         return getHeaderField(headerName, 0);
108     }
109
110     /**
111      * Set the value of a header field. <b>This will replace any current
112      * value for this header, regardless of how many times it occurs</b>.
113      *
114      * @param headerName header name
115      * @param value header value
116      */

117     public void setHeaderField(String JavaDoc headerName, String JavaDoc value) {
118         if(value == null) {
119             throw new IllegalArgumentException JavaDoc("Null value");
120         }
121         Vector JavaDoc v = new Vector JavaDoc();
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     /**
130      * Remove all values for the named header field.
131      *
132      * @param header header name
133      */

134     public void removeFields(String JavaDoc header) {
135         if (fields.containsKey(header.toLowerCase())) {
136             Field f = (Field)fields.remove(header.toLowerCase());
137             fieldNames.removeElement(f.n);
138         }
139     }
140     
141     /**
142      * Remove <b>ALL</b> header fields
143      */

144     public void clearHeaderFields() {
145         fieldNames.removeAllElements();
146         fields.clear();
147     }
148
149     /**
150      * Get the number of values the provider header name has.
151      *
152      * @param headerName header name
153      * @return number of values
154      */

155     public int getHeaderFieldCount(String JavaDoc headerName) {
156         Field f = (Field) fields.get(headerName.toLowerCase());
157         return f.headerValues == null ? 0 : f.headerValues.size();
158     }
159
160     /**
161      * Get an array of all the header values. <code>null</code> will be
162      * returned if no such header exists.
163      *
164      * @param headerName
165      * @return header fields
166      */

167     public String JavaDoc[] getHeaderFields(String JavaDoc headerName) {
168         Field f = (Field) fields.get(headerName.toLowerCase());
169         if (f != null) {
170             String JavaDoc[] values = new String JavaDoc[f.headerValues.size()];
171             f.headerValues.copyInto(values);
172             return values;
173         } else {
174             return null;
175         }
176     }
177
178     /**
179      * Generate the HTTP protocol text that represents the list of headers
180      * this object contains.
181      *
182      * @param startline start line (i.e. HTTP protocol header)
183      * @return http
184      */

185     public String JavaDoc generateOutput(String JavaDoc startline) {
186         String JavaDoc str = startline + "\r\n"; //$NON-NLS-1$
187
Enumeration JavaDoc it = getHeaderFieldNames();
188
189         while (it.hasMoreElements()) {
190             String JavaDoc fieldName = (String JavaDoc) it.nextElement();
191             int count = getHeaderFieldCount(fieldName);
192             for (int i = 0; i < count; i++) {
193                 str += (fieldName + ": " + getHeaderField(fieldName, i) + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
194
}
195
196         }
197
198         str += "\r\n"; //$NON-NLS-1$
199
return str;
200     }
201
202     class Field {
203         Vector JavaDoc headerValues;
204         String JavaDoc n;
205
206         Field(String JavaDoc n, Vector JavaDoc v) {
207             this.n = n;
208             this.headerValues = v;
209         }
210     }
211 }
212
Popular Tags