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.hc.core5.http.message;
29  
30  import java.io.Serializable;
31  
32  import org.apache.hc.core5.http.FormattedHeader;
33  import org.apache.hc.core5.http.ParseException;
34  import org.apache.hc.core5.util.Args;
35  import org.apache.hc.core5.util.CharArrayBuffer;
36  import org.apache.hc.core5.util.Tokenizer;
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  public class BufferedHeader implements FormattedHeader, Serializable {
45  
46      private static final long serialVersionUID = -2768352615787625448L;
47  
48      /**
49       * Header name.
50       */
51      private final String name;
52  
53      /**
54       * The buffer containing the entire header line.
55       */
56      private final CharArrayBuffer buffer;
57  
58      /**
59       * The beginning of the header value in the buffer
60       */
61      private final int valuePos;
62  
63      /**
64       * @since 5.0
65       */
66      public static BufferedHeader create(final CharArrayBuffer buffer) {
67          try {
68              return new BufferedHeader(buffer);
69          } catch (final ParseException ex) {
70              throw new IllegalArgumentException(ex.getMessage());
71          }
72      }
73  
74      /**
75       * Creates a new header from a buffer.
76       * The name of the header will be parsed immediately,
77       * the value only if it is accessed.
78       *
79       * @param buffer    the buffer containing the header to represent
80       *
81       * @throws ParseException   in case of a parse error
82       */
83      public BufferedHeader(final CharArrayBuffer buffer) throws ParseException {
84          this(buffer, true);
85      }
86  
87      BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException {
88          super();
89          Args.notNull(buffer, "Char array buffer");
90          final int colon = buffer.indexOf(':');
91          if (colon <= 0) {
92              throw new ParseException("Invalid header", buffer, 0, buffer.length());
93          }
94          if (strict && Tokenizer.isWhitespace(buffer.charAt(colon - 1))) {
95              throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1);
96          }
97          final String s = buffer.substringTrimmed(0, colon);
98          if (s.isEmpty()) {
99              throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon);
100         }
101         this.buffer = buffer;
102         this.name = s;
103         this.valuePos = colon + 1;
104     }
105 
106     @Override
107     public String getName() {
108         return this.name;
109     }
110 
111     @Override
112     public String getValue() {
113         return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
114     }
115 
116     @Override
117     public boolean isSensitive() {
118         return false;
119     }
120 
121     @Override
122     public int getValuePos() {
123         return this.valuePos;
124     }
125 
126     @Override
127     public CharArrayBuffer getBuffer() {
128         return this.buffer;
129     }
130 
131     @Override
132     public String toString() {
133         return this.buffer.toString();
134     }
135 
136 }