View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/Header.java $
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  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, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  /***
34   * <p>An HTTP header.</p>
35   *
36   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
37   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
38   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
39   * @version $Revision$ $Date$
40   */
41  public class Header extends NameValuePair {
42  
43      // ----------------------------------------------------------- Constructors
44  
45      /***
46       * Autogenerated header flag.
47       */
48      private boolean isAutogenerated = false;
49      
50      /***
51       * Default constructor.
52       */
53      public Header() {
54          this(null, null);
55      }
56  
57      /***
58       * Constructor with name and value
59       *
60       * @param name the header name
61       * @param value the header value
62       */
63      public Header(String name, String value) {
64          super(name, value);
65      }
66  
67      /***
68       * Constructor with name and value
69       *
70       * @param name the header name
71       * @param value the header value
72       * @param isAutogenerated <tt>true</tt> if the header is autogenerated,
73       *  <tt>false</tt> otherwise.
74       * 
75       * @since 3.0
76       */
77      public Header(String name, String value, boolean isAutogenerated) {
78          super(name, value);
79          this.isAutogenerated = isAutogenerated;
80      }
81  
82      // --------------------------------------------------------- Public Methods
83  
84      /***
85       * Returns a {@link String} representation of the header.
86       *
87       * @return stringHEAD
88       */
89      public String toExternalForm() {
90          return ((null == getName() ? "" : getName()) 
91              + ": " 
92              + (null == getValue() ? "" : getValue()) 
93              + "\r\n");
94      }
95  
96      /***
97       * Returns a {@link String} representation of the header.
98       *
99       * @return stringHEAD
100      */
101     public String toString() {
102         return toExternalForm();
103     }
104 
105     /***
106      * Returns an array of {@link HeaderElement}s
107      * constructed from my value.
108      *
109      * @see HeaderElement#parse
110      * @throws HttpException if the header cannot be parsed
111      * @return an array of header elements
112      * 
113      * @deprecated Use #getElements
114      */
115     public HeaderElement[] getValues() throws HttpException {
116         return HeaderElement.parse(getValue());
117     }
118 
119     /***
120      * Returns an array of {@link HeaderElement}s
121      * constructed from my value.
122      *
123      * @see HeaderElement#parseElements(String)
124      * 
125      * @return an array of header elements
126      * 
127      * @since 3.0
128      */
129     public HeaderElement[] getElements() {
130         return HeaderElement.parseElements(getValue());
131     }
132 
133     /***
134      * Returns the value of the auto-generated header flag.
135      * 
136      * @return <tt>true</tt> if the header is autogenerated,
137      *  <tt>false</tt> otherwise.
138      * 
139      * @since 3.0
140      */
141     public boolean isAutogenerated() {
142         return isAutogenerated;
143     }
144 
145 }