001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import org.apache.http.HeaderElement;
035: import org.apache.http.NameValuePair;
036: import org.apache.http.util.CharArrayBuffer;
037: import org.apache.http.util.LangUtils;
038:
039: /**
040: * One element of an HTTP header's value.
041: * <p>
042: * Some HTTP headers (such as the set-cookie header) have values that
043: * can be decomposed into multiple elements. Such headers must be in the
044: * following form:
045: * </p>
046: * <pre>
047: * header = [ element ] *( "," [ element ] )
048: * element = name [ "=" [ value ] ] *( ";" [ param ] )
049: * param = name [ "=" [ value ] ]
050: *
051: * name = token
052: * value = ( token | quoted-string )
053: *
054: * token = 1*<any char except "=", ",", ";", <"> and
055: * white space>
056: * quoted-string = <"> *( text | quoted-char ) <">
057: * text = any char except <">
058: * quoted-char = "\" char
059: * </pre>
060: * <p>
061: * Any amount of white space is allowed between any part of the
062: * header, element or param and is ignored. A missing value in any
063: * element or param will be stored as the empty {@link String};
064: * if the "=" is also missing <var>null</var> will be stored instead.
065: * </p>
066: * <p>
067: * This class represents an individual header element, containing
068: * both a name/value pair (value may be <tt>null</tt>) and optionally
069: * a set of additional parameters.
070: * </p>
071: *
072: * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
073: * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
074: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
075: * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
076: *
077: *
078: * <!-- empty lines above to avoid 'svn diff' context problems -->
079: * @version $Revision: 604625 $ $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
080: *
081: * @since 4.0
082: */
083: public class BasicHeaderElement implements HeaderElement, Cloneable {
084:
085: private final String name;
086: private final String value;
087: private final NameValuePair[] parameters;
088:
089: /**
090: * Constructor with name, value and parameters.
091: *
092: * @param name header element name
093: * @param value header element value. May be <tt>null</tt>
094: * @param parameters header element parameters. May be <tt>null</tt>.
095: * Parameters are copied by reference, not by value
096: */
097: public BasicHeaderElement(final String name, final String value,
098: final NameValuePair[] parameters) {
099: super ();
100: if (name == null) {
101: throw new IllegalArgumentException("Name may not be null");
102: }
103: this .name = name;
104: this .value = value;
105: if (parameters != null) {
106: this .parameters = parameters;
107: } else {
108: this .parameters = new NameValuePair[] {};
109: }
110: }
111:
112: /**
113: * Constructor with name and value.
114: *
115: * @param name header element name
116: * @param value header element value. May be <tt>null</tt>
117: */
118: public BasicHeaderElement(final String name, final String value) {
119: this (name, value, null);
120: }
121:
122: /**
123: * Returns the name.
124: *
125: * @return String name The name
126: */
127: public String getName() {
128: return this .name;
129: }
130:
131: /**
132: * Returns the value.
133: *
134: * @return String value The current value.
135: */
136: public String getValue() {
137: return this .value;
138: }
139:
140: /**
141: * Get parameters, if any.
142: * The returned array is created for each invocation and can
143: * be modified by the caller without affecting this header element.
144: *
145: * @return parameters as an array of {@link NameValuePair}s
146: */
147: public NameValuePair[] getParameters() {
148: return (NameValuePair[]) this .parameters.clone();
149: }
150:
151: /**
152: * Obtains the number of parameters.
153: *
154: * @return the number of parameters
155: */
156: public int getParameterCount() {
157: return this .parameters.length;
158: }
159:
160: /**
161: * Obtains the parameter with the given index.
162: *
163: * @param index the index of the parameter, 0-based
164: *
165: * @return the parameter with the given index
166: */
167: public NameValuePair getParameter(int index) {
168: // ArrayIndexOutOfBoundsException is appropriate
169: return this .parameters[index];
170: }
171:
172: /**
173: * Returns parameter with the given name, if found. Otherwise null
174: * is returned
175: *
176: * @param name The name to search by.
177: * @return NameValuePair parameter with the given name
178: */
179: public NameValuePair getParameterByName(final String name) {
180: if (name == null) {
181: throw new IllegalArgumentException("Name may not be null");
182: }
183: NameValuePair found = null;
184: for (int i = 0; i < this .parameters.length; i++) {
185: NameValuePair current = this .parameters[i];
186: if (current.getName().equalsIgnoreCase(name)) {
187: found = current;
188: break;
189: }
190: }
191: return found;
192: }
193:
194: public boolean equals(final Object object) {
195: if (object == null)
196: return false;
197: if (this == object)
198: return true;
199: if (object instanceof HeaderElement) {
200: BasicHeaderElement that = (BasicHeaderElement) object;
201: return this .name.equals(that.name)
202: && LangUtils.equals(this .value, that.value)
203: && LangUtils.equals(this .parameters,
204: that.parameters);
205: } else {
206: return false;
207: }
208: }
209:
210: public int hashCode() {
211: int hash = LangUtils.HASH_SEED;
212: hash = LangUtils.hashCode(hash, this .name);
213: hash = LangUtils.hashCode(hash, this .value);
214: for (int i = 0; i < this .parameters.length; i++) {
215: hash = LangUtils.hashCode(hash, this .parameters[i]);
216: }
217: return hash;
218: }
219:
220: public String toString() {
221: CharArrayBuffer buffer = new CharArrayBuffer(64);
222: buffer.append(this .name);
223: if (this .value != null) {
224: buffer.append("=");
225: buffer.append(this .value);
226: }
227: for (int i = 0; i < this .parameters.length; i++) {
228: buffer.append("; ");
229: buffer.append(this .parameters[i]);
230: }
231: return buffer.toString();
232: }
233:
234: public Object clone() throws CloneNotSupportedException {
235: // parameters array is considered immutable
236: // no need to make a copy of it
237: return super.clone();
238: }
239:
240: }
|