2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java $
4
* $Date: 2009-02-14 18:06:25 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.message;
34
import org.apache.http.ProtocolVersion;
35
import org.apache.http.RequestLine;
36
import org.apache.http.StatusLine;
37
import org.apache.http.Header;
38
import org.apache.http.FormattedHeader;
39
import org.apache.http.util.CharArrayBuffer;
42
* Interface for formatting elements of the HEAD section of an HTTP message.
43
* This is the complement to {@link LineParser}.
44
* There are individual methods for formatting a request line, a
45
* status line, or a header line. The formatting does <i>not</i> include the
46
* trailing line break sequence CR-LF.
47
* The formatted lines are returned in memory, the formatter does not depend
48
* on any specific IO mechanism.
49
* Instances of this interface are expected to be stateless and thread-safe.
53
* <!-- empty lines above to avoid 'svn diff' context problems -->
54
* @version $Revision: 744527 $
58
public class BasicLineFormatter implements LineFormatter {
61
* A default instance of this class, for use as default or fallback.
62
* Note that {@link BasicLineFormatter} is not a singleton, there can
63
* be many instances of the class itself and of derived classes.
64
* The instance here provides non-customized, default behavior.
66
public final static BasicLineFormatter DEFAULT = new BasicLineFormatter();
70
// public default constructor
74
* Obtains a buffer for formatting.
76
* @param buffer a buffer already available, or <code>null</code>
78
* @return the cleared argument buffer if there is one, or
79
* a new empty buffer that can be used for formatting
81
protected CharArrayBuffer initBuffer(CharArrayBuffer buffer) {
85
buffer = new CharArrayBuffer(64);
92
* Formats a protocol version.
94
* @param version the protocol version to format
95
* @param formatter the formatter to use, or
96
* <code>null</code> for the
97
* {@link #DEFAULT default}
99
* @return the formatted protocol version
102
String formatProtocolVersion(final ProtocolVersion version,
103
LineFormatter formatter) {
104
if (formatter == null)
105
formatter = BasicLineFormatter.DEFAULT;
106
return formatter.appendProtocolVersion(null, version).toString();
110
// non-javadoc, see interface LineFormatter
111
public CharArrayBuffer appendProtocolVersion(final CharArrayBuffer buffer,
112
final ProtocolVersion version) {
113
if (version == null) {
114
throw new IllegalArgumentException
115
("Protocol version may not be null");
118
// can't use initBuffer, that would clear the argument!
119
CharArrayBuffer result = buffer;
120
final int len = estimateProtocolVersionLen(version);
121
if (result == null) {
122
result = new CharArrayBuffer(len);
124
result.ensureCapacity(len);
127
result.append(version.getProtocol());
129
result.append(Integer.toString(version.getMajor()));
131
result.append(Integer.toString(version.getMinor()));
138
* Guesses the length of a formatted protocol version.
139
* Needed to guess the length of a formatted request or status line.
141
* @param version the protocol version to format, or <code>null</code>
143
* @return the estimated length of the formatted protocol version,
146
protected int estimateProtocolVersionLen(final ProtocolVersion version) {
147
return version.getProtocol().length() + 4; // room for "HTTP/1.1"
152
* Formats a request line.
154
* @param reqline the request line to format
155
* @param formatter the formatter to use, or
156
* <code>null</code> for the
157
* {@link #DEFAULT default}
159
* @return the formatted request line
161
public final static String formatRequestLine(final RequestLine reqline,
162
LineFormatter formatter) {
163
if (formatter == null)
164
formatter = BasicLineFormatter.DEFAULT;
165
return formatter.formatRequestLine(null, reqline).toString();
169
// non-javadoc, see interface LineFormatter
170
public CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
171
RequestLine reqline) {
172
if (reqline == null) {
173
throw new IllegalArgumentException
174
("Request line may not be null");
177
CharArrayBuffer result = initBuffer(buffer);
178
doFormatRequestLine(result, reqline);
185
* Actually formats a request line.
186
* Called from {@link #formatRequestLine}.
188
* @param buffer the empty buffer into which to format,
189
* never <code>null</code>
190
* @param reqline the request line to format, never <code>null</code>
192
protected void doFormatRequestLine(final CharArrayBuffer buffer,
193
final RequestLine reqline) {
194
final String method = reqline.getMethod();
195
final String uri = reqline.getUri();
197
// room for "GET /index.html HTTP/1.1"
198
int len = method.length() + 1 + uri.length() + 1 +
199
estimateProtocolVersionLen(reqline.getProtocolVersion());
200
buffer.ensureCapacity(len);
202
buffer.append(method);
206
appendProtocolVersion(buffer, reqline.getProtocolVersion());
212
* Formats a status line.
214
* @param statline the status line to format
215
* @param formatter the formatter to use, or
216
* <code>null</code> for the
217
* {@link #DEFAULT default}
219
* @return the formatted status line
221
public final static String formatStatusLine(final StatusLine statline,
222
LineFormatter formatter) {
223
if (formatter == null)
224
formatter = BasicLineFormatter.DEFAULT;
225
return formatter.formatStatusLine(null, statline).toString();
229
// non-javadoc, see interface LineFormatter
230
public CharArrayBuffer formatStatusLine(final CharArrayBuffer buffer,
231
final StatusLine statline) {
232
if (statline == null) {
233
throw new IllegalArgumentException
234
("Status line may not be null");
237
CharArrayBuffer result = initBuffer(buffer);
238
doFormatStatusLine(result, statline);
245
* Actually formats a status line.
246
* Called from {@link #formatStatusLine}.
248
* @param buffer the empty buffer into which to format,
249
* never <code>null</code>
250
* @param statline the status line to format, never <code>null</code>
252
protected void doFormatStatusLine(final CharArrayBuffer buffer,
253
final StatusLine statline) {
255
int len = estimateProtocolVersionLen(statline.getProtocolVersion())
256
+ 1 + 3 + 1; // room for "HTTP/1.1 200 "
257
final String reason = statline.getReasonPhrase();
258
if (reason != null) {
259
len += reason.length();
261
buffer.ensureCapacity(len);
263
appendProtocolVersion(buffer, statline.getProtocolVersion());
265
buffer.append(Integer.toString(statline.getStatusCode()));
266
buffer.append(' '); // keep whitespace even if reason phrase is empty
267
if (reason != null) {
268
buffer.append(reason);
276
* @param header the header to format
277
* @param formatter the formatter to use, or
278
* <code>null</code> for the
279
* {@link #DEFAULT default}
281
* @return the formatted header
283
public final static String formatHeader(final Header header,
284
LineFormatter formatter) {
285
if (formatter == null)
286
formatter = BasicLineFormatter.DEFAULT;
287
return formatter.formatHeader(null, header).toString();
291
// non-javadoc, see interface LineFormatter
292
public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
294
if (header == null) {
295
throw new IllegalArgumentException
296
("Header may not be null");
298
CharArrayBuffer result = null;
300
if (header instanceof FormattedHeader) {
301
// If the header is backed by a buffer, re-use the buffer
302
result = ((FormattedHeader)header).getBuffer();
304
result = initBuffer(buffer);
305
doFormatHeader(result, header);
313
* Actually formats a header.
314
* Called from {@link #formatHeader}.
316
* @param buffer the empty buffer into which to format,
317
* never <code>null</code>
318
* @param header the header to format, never <code>null</code>
320
protected void doFormatHeader(final CharArrayBuffer buffer,
321
final Header header) {
322
final String name = header.getName();
323
final String value = header.getValue();
325
int len = name.length() + 2;
327
len += value.length();
329
buffer.ensureCapacity(len);
334
buffer.append(value);
339
} // class BasicLineFormatter