Source code: com/sonalb/net/http/Header.java
1 /*
2 * -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
3 * :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
4 * ex: set tabstop=4 expandtab:
5 *
6 * MrPostman - webmail <-> email gateway
7 * Copyright (C) 2002-2003 MrPostman Development Group
8 * Projectpage: http://mrbook.org/mrpostman/
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * In particular, this implies that users are responsible for
21 * using MrPostman after reading the terms and conditions given
22 * by their web-mail provider.
23 *
24 * You should have received a copy of the GNU General Public License
25 * Named LICENSE in the base directory of this distribution,
26 * if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 package com.sonalb.net.http;
31
32 import java.util.AbstractCollection;
33 import java.util.Collection;
34 import java.util.Iterator;
35 import java.util.Vector;
36
37
38 /**
39
40 * Represents the Header of an HTTP Message. An HTTP header usually consists of two
41
42 * major components:
43
44 * <ul>
45
46 * <li>The first line containing HTTP-Command, HTTP-Version etc.</li>
47
48 * <li>A number of name-value pairs</li>
49
50 * </ul>
51
52 * This Header class does not consider the first line as a name-value pair as the HttpURLConnection
53
54 * class does.<br>
55
56 * NULL name fields in a pair are not permitted.
57
58 * @author Sonal Bansal
59
60 */
61 public class Header extends AbstractCollection implements java.io.Serializable, java.lang.Cloneable {
62 public static final String CVSID = "$Id: Header.java,v 1.5 2003/02/09 23:38:10 lbruand Exp $";
63 private String topLine;
64 private Vector theHeader;
65
66 /**
67
68 * Creates an empty Header.
69
70 */
71 public Header() {
72 topLine = null;
73
74 theHeader = new Vector();
75 }
76
77 /**
78
79 * Creates a Header, and populates it with HeaderEntries from input Collection.
80
81 * @param c the Collection containing HeaderEntry objects
82
83 */
84 public Header(Collection c) {
85 topLine = null;
86
87 theHeader = new Vector();
88
89 addAll(c);
90 }
91
92 /**
93
94 * Creates a Header with given top-line, and populates it with HeaderEntries from input Collection.
95
96 * @param c the Collection containing HeaderEntry objects
97
98 * @param topLine the top-most line in an HTTP Header
99
100 */
101 public Header(String topLine, Collection c) {
102 this.topLine = topLine;
103
104 theHeader = new Vector();
105
106 addAll(c);
107 }
108
109 /**
110
111 * Sets the top-line of this Header.
112
113 * @param topLine the top-most line in an HTTP Header
114
115 */
116 public void setTopLine(String topLine) {
117 this.topLine = topLine;
118 }
119
120 /**
121
122 * Gets the top-line of this Header.
123
124 * @return the top-most line in an HTTP Header
125
126 */
127 public String getTopLine() {
128 return (topLine);
129 }
130
131 /**
132
133 * Adds the specified key-value pair to this Header.
134
135 */
136 public boolean add(String key, String value) {
137 return (add(new HeaderEntry(key, value)));
138 }
139
140 /**
141
142 * Returns the entry at specified index.
143
144 */
145 public HeaderEntry getEntryAt(int index) {
146 return ((HeaderEntry) theHeader.get(index));
147 }
148
149 /**
150
151 * Returns the header key for entry at specified index.
152
153 */
154 public String getHeaderFieldKey(int i) {
155 return (getEntryAt(i).getKey());
156 }
157
158 /**
159
160 * Returns the header value for entry at specified index.
161
162 */
163 public String getHeaderField(int i) {
164 return (getEntryAt(i).getValue());
165 }
166
167 /**
168
169 * Checks whether any header entry exists with given key.
170
171 */
172 public boolean containsKey(String s) {
173 if (s == null) {
174 throw new IllegalArgumentException("Key can't be null");
175 }
176
177 HeaderEntry he;
178
179 Iterator iter = iterator();
180
181 while (iter.hasNext()) {
182 he = (HeaderEntry) iter.next();
183
184 if (s.equalsIgnoreCase(he.getKey())) {
185 return (true);
186 }
187 }
188
189 return (false);
190 }
191
192 /**
193
194 * Checks whether any header entry exists with given value.
195
196 */
197 public boolean containsValue(String s) {
198 HeaderEntry he;
199
200 Iterator iter = iterator();
201
202 while (iter.hasNext()) {
203 he = (HeaderEntry) iter.next();
204
205 if (s == null) {
206 if (he.getValue() == null) {
207 return (true);
208 }
209 } else if (s.equals(he.getValue())) {
210 return (true);
211 }
212 }
213
214 return (false);
215 }
216
217 /**
218
219 * Returns the HeaderEntry corresponding to the first occurrence of the given key.
220
221 */
222 public HeaderEntry getFirstEntryForKey(String s) {
223 return (getEntryForKey(s, -1));
224 }
225
226 /**
227
228 * Returns the HeaderEntry corresponding to the first occurrence of the given value.
229
230 */
231 public HeaderEntry getFirstEntryForValue(String s) {
232 return (getEntryForValue(s, -1));
233 }
234
235 /**
236
237 * Returns the HeaderEntry corresponding to the first occurrence of the given key, after specified index (non-inclusive).
238
239 */
240 public HeaderEntry getEntryForKey(String s, int j) {
241 if (s == null) {
242 throw new IllegalArgumentException("Key can't be null");
243 }
244
245 if (j < 0) {
246 j = 0;
247 } else if (j > (theHeader.size() - 1)) {
248 j = theHeader.size() - 1;
249 }
250
251 HeaderEntry he;
252
253 for (int i = j + 1; i < theHeader.size(); i++) {
254 try {
255 he = getEntryAt(i);
256 } catch (IndexOutOfBoundsException ioobe) {
257 break;
258 }
259
260 if (s.equalsIgnoreCase(he.getKey())) {
261 return (he);
262 }
263 }
264
265 return (null);
266 }
267
268 /**
269
270 * Returns the HeaderEntry corresponding to the first occurrence of the given value, after specified index (non-inclusive).
271
272 */
273 public HeaderEntry getEntryForValue(String s, int j) {
274 HeaderEntry he;
275
276 if (j < 0) {
277 j = 0;
278 } else if (j > (theHeader.size() - 1)) {
279 j = theHeader.size() - 1;
280 }
281
282 for (int i = j + 1; i < theHeader.size(); i++) {
283 try {
284 he = getEntryAt(i);
285 } catch (IndexOutOfBoundsException ioobe) {
286 break;
287 }
288
289 if (s == null) {
290 if (he.getValue() == null) {
291 return (he);
292 }
293 } else if (s.equalsIgnoreCase(he.getValue())) {
294 return (he);
295 }
296 }
297
298 return (null);
299 }
300
301 /**
302
303 * Returns a Header consisting of all HeaderEntries having given key.
304
305 */
306 public Header getEntriesForKey(String s) {
307 if (s == null) {
308 throw new IllegalArgumentException("Key can't be null.");
309 }
310
311 HeaderEntry he;
312
313 Header h = new Header();
314
315 Iterator iter = iterator();
316
317 while (iter.hasNext()) {
318 he = (HeaderEntry) iter.next();
319
320 if (s.equalsIgnoreCase(he.getKey())) {
321 h.add(he);
322 }
323 }
324
325 return (h);
326 }
327
328 /**
329
330 * Returns a Header consisting of all HeaderEntries having given value.
331
332 */
333 public Header getEntriesForValue(String s) {
334 HeaderEntry he;
335
336 Header h = new Header();
337
338 Iterator iter = iterator();
339
340 while (iter.hasNext()) {
341 he = (HeaderEntry) iter.next();
342
343 if (s == null) {
344 if (he.getValue() == null) {
345 h.add(he);
346 }
347 } else if (s.equals(he.getValue())) {
348 h.add(he);
349 }
350 }
351
352 return (h);
353 }
354
355 public boolean add(Object entry) {
356 if (entry == null) {
357 throw new IllegalArgumentException("Null entry.");
358 } else if (!(entry instanceof HeaderEntry)) {
359 throw new ClassCastException("Not a HeaderEntry");
360 }
361
362 if (contains(entry)) {
363 return (false);
364 }
365
366 theHeader.add(entry);
367
368 return (true);
369 }
370
371 public Iterator iterator() {
372 return (theHeader.iterator());
373 }
374
375 public int size() {
376 return (theHeader.size());
377 }
378
379 public String toString() {
380 if (isEmpty()) {
381 return ("{}");
382 }
383
384 StringBuffer sb = new StringBuffer();
385
386 sb.append("{");
387
388 if (topLine != null) {
389 sb.append(topLine);
390
391 sb.append("\n[");
392 }
393
394 for (int i = 0; i < theHeader.size(); i++) {
395 sb.append(theHeader.get(i).toString());
396
397 sb.append(",");
398 }
399
400 sb.deleteCharAt(sb.length() - 1);
401
402 sb.append("]}");
403
404 return (sb.toString());
405 }
406
407 public boolean isEmpty() {
408 return ((theHeader.size() == 0) && (topLine == null));
409 }
410
411 public Object clone() throws CloneNotSupportedException {
412 return (super.clone());
413 }
414 }