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/BasicHeaderElement.java $
      3  * $Revision: 604625 $
      4  * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
      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.HeaderElement;
     35 import org.apache.http.NameValuePair;
     36 import org.apache.http.util.CharArrayBuffer;
     37 import org.apache.http.util.LangUtils;
     38 
     39 /**
     40  * One element of an HTTP header's value.
     41  * <p>
     42  * Some HTTP headers (such as the set-cookie header) have values that
     43  * can be decomposed into multiple elements.  Such headers must be in the
     44  * following form:
     45  * </p>
     46  * <pre>
     47  * header  = [ element ] *( "," [ element ] )
     48  * element = name [ "=" [ value ] ] *( ";" [ param ] )
     49  * param   = name [ "=" [ value ] ]
     50  *
     51  * name    = token
     52  * value   = ( token | quoted-string )
     53  *
     54  * token         = 1*&lt;any char except "=", ",", ";", &lt;"&gt; and
     55  *                       white space&gt;
     56  * quoted-string = &lt;"&gt; *( text | quoted-char ) &lt;"&gt;
     57  * text          = any char except &lt;"&gt;
     58  * quoted-char   = "\" char
     59  * </pre>
     60  * <p>
     61  * Any amount of white space is allowed between any part of the
     62  * header, element or param and is ignored. A missing value in any
     63  * element or param will be stored as the empty {@link String};
     64  * if the "=" is also missing <var>null</var> will be stored instead.
     65  * </p>
     66  * <p>
     67  * This class represents an individual header element, containing
     68  * both a name/value pair (value may be <tt>null</tt>) and optionally
     69  * a set of additional parameters.
     70  * </p>
     71  *
     72  * @author <a href="mailto:bcholmes (at) interlog.com">B.C. Holmes</a>
     73  * @author <a href="mailto:jericho (at) thinkfree.com">Park, Sung-Gu</a>
     74  * @author <a href="mailto:mbowler (at) GargoyleSoftware.com">Mike Bowler</a>
     75  * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
     76  *
     77  *
     78  * <!-- empty lines above to avoid 'svn diff' context problems -->
     79  * @version $Revision: 604625 $ $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
     80  *
     81  * @since 4.0
     82  *
     83  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     84  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     85  *     for further details.
     86  */
     87 @Deprecated
     88 public class BasicHeaderElement implements HeaderElement, Cloneable {
     89 
     90     private final String name;
     91     private final String value;
     92     private final NameValuePair[] parameters;
     93 
     94     /**
     95      * Constructor with name, value and parameters.
     96      *
     97      * @param name header element name
     98      * @param value header element value. May be <tt>null</tt>
     99      * @param parameters header element parameters. May be <tt>null</tt>.
    100      *   Parameters are copied by reference, not by value
    101      */
    102     public BasicHeaderElement(
    103             final String name,
    104             final String value,
    105             final NameValuePair[] parameters) {
    106         super();
    107         if (name == null) {
    108             throw new IllegalArgumentException("Name may not be null");
    109         }
    110         this.name = name;
    111         this.value = value;
    112         if (parameters != null) {
    113             this.parameters = parameters;
    114         } else {
    115             this.parameters = new NameValuePair[] {};
    116         }
    117     }
    118 
    119     /**
    120      * Constructor with name and value.
    121      *
    122      * @param name header element name
    123      * @param value header element value. May be <tt>null</tt>
    124      */
    125     public BasicHeaderElement(final String name, final String value) {
    126        this(name, value, null);
    127     }
    128 
    129     /**
    130      * Returns the name.
    131      *
    132      * @return String name The name
    133      */
    134     public String getName() {
    135         return this.name;
    136     }
    137 
    138     /**
    139      * Returns the value.
    140      *
    141      * @return String value The current value.
    142      */
    143     public String getValue() {
    144         return this.value;
    145     }
    146 
    147     /**
    148      * Get parameters, if any.
    149      * The returned array is created for each invocation and can
    150      * be modified by the caller without affecting this header element.
    151      *
    152      * @return parameters as an array of {@link NameValuePair}s
    153      */
    154     public NameValuePair[] getParameters() {
    155         return (NameValuePair[])this.parameters.clone();
    156     }
    157 
    158 
    159     /**
    160      * Obtains the number of parameters.
    161      *
    162      * @return  the number of parameters
    163      */
    164     public int getParameterCount() {
    165         return this.parameters.length;
    166     }
    167 
    168 
    169     /**
    170      * Obtains the parameter with the given index.
    171      *
    172      * @param index     the index of the parameter, 0-based
    173      *
    174      * @return  the parameter with the given index
    175      */
    176     public NameValuePair getParameter(int index) {
    177         // ArrayIndexOutOfBoundsException is appropriate
    178         return this.parameters[index];
    179     }
    180 
    181 
    182     /**
    183      * Returns parameter with the given name, if found. Otherwise null
    184      * is returned
    185      *
    186      * @param name The name to search by.
    187      * @return NameValuePair parameter with the given name
    188      */
    189     public NameValuePair getParameterByName(final String name) {
    190         if (name == null) {
    191             throw new IllegalArgumentException("Name may not be null");
    192         }
    193         NameValuePair found = null;
    194         for (int i = 0; i < this.parameters.length; i++) {
    195             NameValuePair current = this.parameters[ i ];
    196             if (current.getName().equalsIgnoreCase(name)) {
    197                 found = current;
    198                 break;
    199             }
    200         }
    201         return found;
    202     }
    203 
    204     public boolean equals(final Object object) {
    205         if (object == null) return false;
    206         if (this == object) return true;
    207         if (object instanceof HeaderElement) {
    208             BasicHeaderElement that = (BasicHeaderElement) object;
    209             return this.name.equals(that.name)
    210                 && LangUtils.equals(this.value, that.value)
    211                 && LangUtils.equals(this.parameters, that.parameters);
    212         } else {
    213             return false;
    214         }
    215     }
    216 
    217     public int hashCode() {
    218         int hash = LangUtils.HASH_SEED;
    219         hash = LangUtils.hashCode(hash, this.name);
    220         hash = LangUtils.hashCode(hash, this.value);
    221         for (int i = 0; i < this.parameters.length; i++) {
    222             hash = LangUtils.hashCode(hash, this.parameters[i]);
    223         }
    224         return hash;
    225     }
    226 
    227     public String toString() {
    228         CharArrayBuffer buffer = new CharArrayBuffer(64);
    229         buffer.append(this.name);
    230         if (this.value != null) {
    231             buffer.append("=");
    232             buffer.append(this.value);
    233         }
    234         for (int i = 0; i < this.parameters.length; i++) {
    235             buffer.append("; ");
    236             buffer.append(this.parameters[i]);
    237         }
    238         return buffer.toString();
    239     }
    240 
    241     public Object clone() throws CloneNotSupportedException {
    242         // parameters array is considered immutable
    243         // no need to make a copy of it
    244         return super.clone();
    245     }
    246 
    247 }
    248 
    249