Source code: com/sonalb/net/http/HeaderUtils.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.net.URLConnection;
33
34 import java.util.Iterator;
35
36
37 /**
38
39 * Utility class with methods relevant to HTTP Headers.
40
41 * @author Sonal Bansal
42
43 */
44 public final class HeaderUtils {
45 public static final String CVSID = "$Id: HeaderUtils.java,v 1.6 2003/02/09 23:38:10 lbruand Exp $";
46
47 private HeaderUtils() {
48 }
49
50 /**
51
52 * Extracts the headers from the input URLConnection, and populates them in a Header instance.
53
54 * @param uc a connected URLConnection
55
56 * @return the Header; always non-null
57
58 */
59 public static Header extractHeaders(URLConnection uc) {
60 if (uc == null) {
61 throw new IllegalArgumentException("Null URLConnection");
62 }
63
64 Header h = new Header();
65
66 h.setTopLine(uc.getHeaderField(0));
67
68 for (int i = 1; uc.getHeaderFieldKey(i) != null; i++) {
69 h.add(uc.getHeaderFieldKey(i), uc.getHeaderField(i));
70 }
71
72 return (h);
73 }
74
75 /**
76
77 * Converts the Header input, into properties in the URLConnection.
78
79 * @param uc an un-connected URLConnection
80
81 * @param h a Header
82
83 */
84 public static void setHeaders(URLConnection uc, Header h) {
85 if (uc == null) {
86 throw new IllegalArgumentException("Null URLConnection");
87 }
88
89 if ((h == null) || h.isEmpty()) {
90 return;
91 }
92
93 //System.out.ln("HeaderUtils.setHeaders(): I've been asked to set the following Headers = " + h);
94 HeaderEntry he;
95
96 Iterator iter = h.iterator();
97
98 while (iter.hasNext()) {
99 he = (HeaderEntry) iter.next();
100
101 //System.out.ln("HeaderUtils.setHeaders(): Setting Entry -:" + he);
102 uc.setRequestProperty(he.getKey(), he.getValue());
103 }
104 }
105 }