001: /*
002: * ====================================================================
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056: /* ====================================================================
057: The Jicarilla Software License
058:
059: Copyright (c) 2003 Leo Simons.
060: All rights reserved.
061:
062: Permission is hereby granted, free of charge, to any person obtaining
063: a copy of this software and associated documentation files (the
064: "Software"), to deal in the Software without restriction, including
065: without limitation the rights to use, copy, modify, merge, publish,
066: distribute, sublicense, and/or sell copies of the Software, and to
067: permit persons to whom the Software is furnished to do so, subject to
068: the following conditions:
069:
070: The above copyright notice and this permission notice shall be
071: included in all copies or substantial portions of the Software.
072:
073: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
074: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
075: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
076: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
077: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
078: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
079: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
080: ==================================================================== */
081:
082: package org.jicarilla.http;
083:
084: import org.jicarilla.lang.Recyclable;
085: import org.jicarilla.http.util.NioUtil;
086:
087: import java.io.Serializable;
088: import java.nio.ByteBuffer;
089:
090: /**
091: * <p>Simple JavaBean representing an HTTP header.</p>
092: *
093: * This class is originally from the
094: * <a href="http://jakarta.apache.org/">Jakarta Commons</a> httpclient
095: * libraries.
096: *
097: * @author <a href="mailto: lsimons at jicarilla dot org">Leo Simons</a>
098: * @author <a href="mailto:remm at apache dot org">Remy Maucherat</a>
099: * @author <a href="mailto:mbowler at GargoyleSoftware dot com">Mike Bowler</a>
100: * @version $Id: HTTPField.java,v 1.5 2004/03/23 13:37:48 lsimons Exp $
101: */
102: public class HTTPField implements Recyclable, Serializable {
103: // ----------------------------------------------------------------------
104: // Properties
105: // ----------------------------------------------------------------------
106: protected ByteBuffer m_name = null;
107: protected ByteBuffer m_value = null;
108:
109: // ----------------------------------------------------------------------
110: // Constructors
111: // ----------------------------------------------------------------------
112: /** Default constructor. */
113: public HTTPField() {
114: this (null, null);
115: }
116:
117: /**
118: * Constructor.
119: *
120: * @param name The name.
121: * @param value The value.
122: */
123: public HTTPField(final ByteBuffer name, final ByteBuffer value) {
124: m_name = name;
125: m_value = value;
126: }
127:
128: // ----------------------------------------------------------------------
129: // Getters/Setters
130: // ----------------------------------------------------------------------
131:
132: /**
133: * Set the name.
134: *
135: * @param name The new name
136: * @see #getName()
137: */
138: public void setName(final ByteBuffer name) {
139: m_name = name;
140: }
141:
142: /**
143: * Return the name.
144: *
145: * @return String name The name
146: * @see #setName(ByteBuffer)
147: */
148: public ByteBuffer getName() {
149: return m_name;
150: }
151:
152: public String getNameString() {
153: return NioUtil.toString(m_name);
154: }
155:
156: /**
157: * Set the value.
158: *
159: * @param value The new value.
160: */
161: public void setValue(final ByteBuffer value) {
162: m_value = value;
163: }
164:
165: /**
166: * Return the current value.
167: *
168: * @return String value The current value.
169: */
170: public ByteBuffer getValue() {
171: return m_value;
172: }
173:
174: public String getValueString() {
175: return NioUtil.toString(m_value);
176: }
177:
178: // ----------------------------------------------------------------------
179: // Utility methods
180: // ----------------------------------------------------------------------
181:
182: /**
183: * Test if the given <i>object</i> is equal to me. In this implementation,
184: * an <i>object</i> is equal to me iff it has the same runtime type and the
185: * <i>name</i> and <i>value</i> attributes are both <tt>equal</tt> (or
186: * <tt>==</tt>).
187: *
188: * @param object the {@link java.lang.Object} to compare to
189: * @return true if the objects are equal.
190: */
191: public boolean equals(final Object object) {
192: if (this == object) {
193: return true;
194: } else if (this .getClass().equals(object.getClass())) {
195: final HTTPField pair = (HTTPField) object;
196: return (null == m_name ? null == pair.m_name : m_name
197: .equals(pair.m_name))
198: && (null == m_value ? null == pair.m_value
199: : m_value.equals(pair.m_value));
200: } else {
201: return false;
202: }
203: }
204:
205: /**
206: * hashCode. Returns a hash code for this object such that if <tt>a.{@link
207: * #equals equals}(b)</tt> then <tt>a.hashCode() == b.hashCode()</tt>.
208: * @return The hash code.
209: */
210: public int hashCode() {
211: return this .getClass().hashCode()
212: ^ (null == m_name ? 0 : m_name.hashCode())
213: ^ (null == m_value ? 0 : m_value.hashCode());
214: }
215:
216: /**
217: * Get a {@link java.lang.String} representation of me, suitable
218: * for use in an HTTP head.
219: *
220: * @return string value for a HTTP HEAD
221: */
222: public String toExternalForm() {
223: final StringBuffer buffer = new StringBuffer();
224: NioUtil.append(buffer, m_name);
225: buffer.append(": ");
226: NioUtil.append(buffer, m_value);
227: buffer.append("\r\n");
228:
229: return buffer.toString();
230: }
231:
232: /**
233: * Returns a {@link java.lang.String} representation of me.
234: *
235: * @see #toExternalForm
236: * @return String representation
237: */
238: public String toString() {
239: return toExternalForm();
240: }
241:
242: // ----------------------------------------------------------------------
243: // Work Interface: Recyclable
244: // ----------------------------------------------------------------------
245: public void recycle() {
246: m_name = null;
247: m_value = null;
248: }
249: }
|