View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.message;
29  
30  import java.io.Serializable;
31  
32  import org.apache.http.FormattedHeader;
33  import org.apache.http.HeaderElement;
34  import org.apache.http.ParseException;
35  import org.apache.http.annotation.NotThreadSafe;
36  import org.apache.http.util.CharArrayBuffer;
37  
38  /**
39   * This class represents a raw HTTP header whose content is parsed 'on demand'
40   * only when the header value needs to be consumed.
41   *
42   * @since 4.0
43   */
44  @NotThreadSafe
45  public class BufferedHeader implements FormattedHeader, Cloneable, Serializable {
46  
47      private static final long serialVersionUID = -2768352615787625448L;
48  
49      /**
50       * Header name.
51       */
52      private final String name;
53  
54      /**
55       * The buffer containing the entire header line.
56       */
57      private final CharArrayBuffer buffer;
58  
59      /**
60       * The beginning of the header value in the buffer
61       */
62      private final int valuePos;
63  
64  
65      /**
66       * Creates a new header from a buffer.
67       * The name of the header will be parsed immediately,
68       * the value only if it is accessed.
69       *
70       * @param buffer    the buffer containing the header to represent
71       *
72       * @throws ParseException   in case of a parse error
73       */
74      public BufferedHeader(final CharArrayBuffer buffer)
75          throws ParseException {
76  
77          super();
78          if (buffer == null) {
79              throw new IllegalArgumentException
80                  ("Char array buffer may not be null");
81          }
82          int colon = buffer.indexOf(':');
83          if (colon == -1) {
84              throw new ParseException
85                  ("Invalid header: " + buffer.toString());
86          }
87          String s = buffer.substringTrimmed(0, colon);
88          if (s.length() == 0) {
89              throw new ParseException
90                  ("Invalid header: " + buffer.toString());
91          }
92          this.buffer = buffer;
93          this.name = s;
94          this.valuePos = colon + 1;
95      }
96  
97  
98      public String getName() {
99          return this.name;
100     }
101 
102     public String getValue() {
103         return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
104     }
105 
106     public HeaderElement[] getElements() throws ParseException {
107         ParserCursor cursor = new ParserCursor(0, this.buffer.length());
108         cursor.updatePos(this.valuePos);
109         return BasicHeaderValueParser.DEFAULT
110             .parseElements(this.buffer, cursor);
111     }
112 
113     public int getValuePos() {
114         return this.valuePos;
115     }
116 
117     public CharArrayBuffer getBuffer() {
118         return this.buffer;
119     }
120 
121     @Override
122     public String toString() {
123         return this.buffer.toString();
124     }
125 
126     @Override
127     public Object clone() throws CloneNotSupportedException {
128         // buffer is considered immutable
129         // no need to make a copy of it
130         return super.clone();
131     }
132 
133 }