KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > http > HttpHeader


1 package net.matuschek.http;
2
3 /*********************************************
4     Copyright (c) 2001 by Daniel Matuschek
5 *********************************************/

6
7 /**
8  * This object represents a HTTP header. A header simply consist
9  * of a name and a value separated by ":"
10  *
11  * @author Daniel Matuschek
12  * @version $Id: HttpHeader.java,v 1.4 2003/02/27 11:36:53 oliver_schmidt Exp $
13 */

14 public class HttpHeader {
15
16   /** standard headers */
17   public final static String JavaDoc CACHE_CONTROL = "Cache-Control";
18   public final static String JavaDoc CONTENT_LENGTH = "Content-Length";
19   public final static String JavaDoc CONTENT_TYPE = "Content-Type";
20   public final static String JavaDoc DATE = "Date";
21   public final static String JavaDoc LAST_MODIFIED = "Last-Modified";
22   public final static String JavaDoc LOCATION = "location";
23   public final static String JavaDoc SERVER = "Server";
24   public final static String JavaDoc SET_COOKIE = "Set-Cookie";
25   public final static String JavaDoc TRANSFER_ENCODING = "Transfer-Encoding";
26     
27   /** application header used to store MD5 key of content */
28   public final static String JavaDoc CONTENT_MD5 = "Content-MD5";
29
30   /** the name (e.g. Content-Length, Set-Cookie, ...) */
31   private String JavaDoc name="";
32
33   /** the value (everything behind the first colon */
34   private String JavaDoc value="";
35
36   /**
37    * initializes the HttpHeader from a given name/value pair
38    */

39   public HttpHeader(String JavaDoc name, String JavaDoc value) {
40     this.name=name;
41     this.value=value;
42   }
43   
44   /**
45    * initializes the HttpHeader from a line (request or response)
46    * @param line a HTTP header line in the format name: value
47    */

48   public HttpHeader(String JavaDoc httpLine) {
49     int pos=0;
50     pos=httpLine.indexOf(":");
51     if (pos == -1) { return; }
52
53     name=httpLine.substring(0,pos);
54     value=httpLine.substring(pos+1).trim();
55   }
56   
57   public String JavaDoc getName() {
58     return name;
59   }
60   
61   public void setName(String JavaDoc name) {
62     this.name = name;
63   }
64   
65   public String JavaDoc getValue() {
66     return value;
67   }
68   
69   public void setValue(String JavaDoc value) {
70     this.value = value;
71   }
72
73   public String JavaDoc toString() {
74     return toLine();
75   }
76
77   /**
78    * Converts the object to a String
79    * @return a name: value String
80    */

81   public String JavaDoc toLine() {
82     return name+": "+value;
83   }
84
85   /**
86    * Is this a Set-Cookie header ?
87    * @return true if this header sets a cookie.
88    */

89   public boolean isSetCookie() {
90     return name.equalsIgnoreCase(SET_COOKIE);
91   }
92   
93 }
94
Popular Tags