Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/Header.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/Header.java $
3    * $Revision: 411090 $
4    * $Date: 2006-06-02 10:39:44 +0200 (Fri, 02 Jun 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http;
31  
32  import org.apache.http.io.CharArrayBuffer;
33  
34  /**
35   * Represents an HTTP header field.
36   * 
37   * <p>The HTTP header fields follow the same generic format as
38   * that given in Section 3.1 of RFC 822. Each header field consists
39   * of a name followed by a colon (":") and the field value. Field names
40   * are case-insensitive. The field value MAY be preceded by any amount
41   * of LWS, though a single SP is preferred. 
42   *
43   *<pre>
44   *     message-header = field-name ":" [ field-value ]
45   *     field-name     = token
46   *     field-value    = *( field-content | LWS )
47   *     field-content  = &lt;the OCTETs making up the field-value
48   *                      and consisting of either *TEXT or combinations
49   *                      of token, separators, and quoted-string&gt;
50   *</pre>
51   * 
52   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
53   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
55   * @version $Revision: 411090 $ $Date: 2006-06-02 10:39:44 +0200 (Fri, 02 Jun 2006) $
56   */
57  public class Header {
58  
59      /**
60       * Header name.
61       */
62      private final String name;
63      
64      /**
65       * Header value.
66       */
67      private final String value;
68      
69      /**
70       * Constructor with name and value
71       *
72       * @param name the header name
73       * @param value the header value
74       */
75      public Header(final String name, final String value) {
76          super();
77          if (name == null) {
78              throw new IllegalArgumentException("Name may not be null");
79          }
80          this.name = name;
81          this.value = value;
82      }
83  
84      /**
85       * Returns the header name.
86       *
87       * @return String name The name
88       */
89      public String getName() {
90          return this.name;
91      }
92  
93      /**
94       * Returns the header value.
95       *
96       * @return String value The current value.
97       */
98      public String getValue() {
99          return this.value;
100     }
101 
102     /**
103      * Returns a {@link String} representation of the header.
104      *
105      * @return a string
106      */
107     public String toString() {
108         CharArrayBuffer buffer = new CharArrayBuffer(32);
109         buffer.append(this.name);
110         buffer.append(": ");
111         if (this.value != null) {
112             buffer.append(this.value);
113         }
114         return buffer.toString();
115     }
116 
117     /**
118      * Returns an array of {@link HeaderElement}s constructed from my value.
119      *
120      * @see HeaderElement#parseAll
121      * 
122      * @return an array of header elements
123      * 
124      * @since 3.0
125      */
126     public HeaderElement[] getElements() {
127         if (this.value != null) {
128             return HeaderElement.parseAll(this.value);
129         } else {
130             return new HeaderElement[] {}; 
131         }
132     }
133 
134     /**
135      * Formats a Header into a header line. The <code>header</code> is
136      * directly appended to <code>buffer</code>; no newline characters are
137      * inserted (folding).
138      * 
139      * @param buffer the buffer to append to
140      * @param header the header to format
141      */
142     public static void format(final CharArrayBuffer buffer, final Header header) {
143         if (buffer == null) {
144             throw new IllegalArgumentException("String buffer may not be null");
145         }
146         if (header == null) {
147             throw new IllegalArgumentException("Header may not be null");
148         }
149         buffer.append(header.getName());
150         buffer.append(": ");
151         if (header.getValue() != null) {
152             buffer.append(header.getValue());
153         }
154     }
155  
156     /**
157      * @see #format(CharArrayBuffer, Header)
158      */
159     public static String format(final Header header) {
160         CharArrayBuffer buffer = new CharArrayBuffer(32);
161         format(buffer, header);
162         return buffer.toString();
163     }
164 
165 }