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.Args;
37 import org.apache.http.util.CharArrayBuffer;
38
39 /**
40 * This class represents a raw HTTP header whose content is parsed 'on demand'
41 * only when the header value needs to be consumed.
42 *
43 * @since 4.0
44 */
45 @NotThreadSafe
46 public class BufferedHeader implements FormattedHeader, Cloneable, Serializable {
47
48 private static final long serialVersionUID = -2768352615787625448L;
49
50 /**
51 * Header name.
52 */
53 private final String name;
54
55 /**
56 * The buffer containing the entire header line.
57 */
58 private final CharArrayBuffer buffer;
59
60 /**
61 * The beginning of the header value in the buffer
62 */
63 private final int valuePos;
64
65
66 /**
67 * Creates a new header from a buffer.
68 * The name of the header will be parsed immediately,
69 * the value only if it is accessed.
70 *
71 * @param buffer the buffer containing the header to represent
72 *
73 * @throws ParseException in case of a parse error
74 */
75 public BufferedHeader(final CharArrayBuffer buffer)
76 throws ParseException {
77
78 super();
79 Args.notNull(buffer, "Char array buffer");
80 final int colon = buffer.indexOf(':');
81 if (colon == -1) {
82 throw new ParseException
83 ("Invalid header: " + buffer.toString());
84 }
85 final String s = buffer.substringTrimmed(0, colon);
86 if (s.length() == 0) {
87 throw new ParseException
88 ("Invalid header: " + buffer.toString());
89 }
90 this.buffer = buffer;
91 this.name = s;
92 this.valuePos = colon + 1;
93 }
94
95
96 public String getName() {
97 return this.name;
98 }
99
100 public String getValue() {
101 return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
102 }
103
104 public HeaderElement[] getElements() throws ParseException {
105 final ParserCursor cursor = new ParserCursor(0, this.buffer.length());
106 cursor.updatePos(this.valuePos);
107 return BasicHeaderValueParser.INSTANCE.parseElements(this.buffer, cursor);
108 }
109
110 public int getValuePos() {
111 return this.valuePos;
112 }
113
114 public CharArrayBuffer getBuffer() {
115 return this.buffer;
116 }
117
118 @Override
119 public String toString() {
120 return this.buffer.toString();
121 }
122
123 @Override
124 public Object clone() throws CloneNotSupportedException {
125 // buffer is considered immutable
126 // no need to make a copy of it
127 return super.clone();
128 }
129
130 }