Home | History | Annotate | Download | only in message
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java $
      3  * $Revision: 652956 $
      4  * $Date: 2008-05-02 17:13:05 -0700 (Fri, 02 May 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.message;
     33 
     34 import org.apache.http.Header;
     35 import org.apache.http.HeaderElement;
     36 import org.apache.http.ParseException;
     37 
     38 /**
     39  * Represents an HTTP header field.
     40  *
     41  * <p>The HTTP header fields follow the same generic format as
     42  * that given in Section 3.1 of RFC 822. Each header field consists
     43  * of a name followed by a colon (":") and the field value. Field names
     44  * are case-insensitive. The field value MAY be preceded by any amount
     45  * of LWS, though a single SP is preferred.
     46  *
     47  *<pre>
     48  *     message-header = field-name ":" [ field-value ]
     49  *     field-name     = token
     50  *     field-value    = *( field-content | LWS )
     51  *     field-content  = &lt;the OCTETs making up the field-value
     52  *                      and consisting of either *TEXT or combinations
     53  *                      of token, separators, and quoted-string&gt;
     54  *</pre>
     55  *
     56  * @author <a href="mailto:remm (at) apache.org">Remy Maucherat</a>
     57  * @author <a href="mailto:mbowler (at) GargoyleSoftware.com">Mike Bowler</a>
     58  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     59  *
     60  *
     61  * <!-- empty lines above to avoid 'svn diff' context problems -->
     62  * @version $Revision: 652956 $ $Date: 2008-05-02 17:13:05 -0700 (Fri, 02 May 2008) $
     63  *
     64  * @since 4.0
     65  */
     66 public class BasicHeader implements Header, Cloneable {
     67 
     68     /**
     69      * Header name.
     70      */
     71     private final String name;
     72 
     73     /**
     74      * Header value.
     75      */
     76     private final String value;
     77 
     78     /**
     79      * Constructor with name and value
     80      *
     81      * @param name the header name
     82      * @param value the header value
     83      */
     84     public BasicHeader(final String name, final String value) {
     85         super();
     86         if (name == null) {
     87             throw new IllegalArgumentException("Name may not be null");
     88         }
     89         this.name = name;
     90         this.value = value;
     91     }
     92 
     93     /**
     94      * Returns the header name.
     95      *
     96      * @return String name The name
     97      */
     98     public String getName() {
     99         return this.name;
    100     }
    101 
    102     /**
    103      * Returns the header value.
    104      *
    105      * @return String value The current value.
    106      */
    107     public String getValue() {
    108         return this.value;
    109     }
    110 
    111     /**
    112      * Returns a {@link String} representation of the header.
    113      *
    114      * @return a string
    115      */
    116     public String toString() {
    117         // no need for non-default formatting in toString()
    118         return BasicLineFormatter.DEFAULT.formatHeader(null, this).toString();
    119     }
    120 
    121     /**
    122      * Returns an array of {@link HeaderElement}s constructed from my value.
    123      *
    124      * @see BasicHeaderValueParser#parseElements(String, HeaderValueParser)
    125      *
    126      * @return an array of header elements
    127      *
    128      * @throws ParseException   in case of a parse error
    129      */
    130     public HeaderElement[] getElements() throws ParseException {
    131         if (this.value != null) {
    132             // result intentionally not cached, it's probably not used again
    133             return BasicHeaderValueParser.parseElements(this.value, null);
    134         } else {
    135             return new HeaderElement[] {};
    136         }
    137     }
    138 
    139     public Object clone() throws CloneNotSupportedException {
    140         return super.clone();
    141     }
    142 
    143 }
    144