~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java $
 
3
 * $Revision: 744527 $
 
4
 * $Date: 2009-02-14 18:06:25 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.message;
 
33
 
 
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;
 
40
 
 
41
/**
 
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.
 
50
 *
 
51
 *
 
52
 *
 
53
 * <!-- empty lines above to avoid 'svn diff' context problems -->
 
54
 * @version $Revision: 744527 $
 
55
 *
 
56
 * @since 4.0
 
57
 */
 
58
public class BasicLineFormatter implements LineFormatter {
 
59
 
 
60
    /**
 
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.
 
65
     */
 
66
    public final static BasicLineFormatter DEFAULT = new BasicLineFormatter();
 
67
 
 
68
 
 
69
 
 
70
    // public default constructor
 
71
 
 
72
 
 
73
    /**
 
74
     * Obtains a buffer for formatting.
 
75
     *
 
76
     * @param buffer    a buffer already available, or <code>null</code>
 
77
     *
 
78
     * @return  the cleared argument buffer if there is one, or
 
79
     *          a new empty buffer that can be used for formatting
 
80
     */
 
81
    protected CharArrayBuffer initBuffer(CharArrayBuffer buffer) {
 
82
        if (buffer != null) {
 
83
            buffer.clear();
 
84
        } else {
 
85
            buffer = new CharArrayBuffer(64);
 
86
        }
 
87
        return buffer;
 
88
    }
 
89
 
 
90
 
 
91
    /**
 
92
     * Formats a protocol version.
 
93
     *
 
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}
 
98
     *
 
99
     * @return  the formatted protocol version
 
100
     */
 
101
    public final static
 
102
        String formatProtocolVersion(final ProtocolVersion version,
 
103
                                     LineFormatter formatter) {
 
104
        if (formatter == null)
 
105
            formatter = BasicLineFormatter.DEFAULT;
 
106
        return formatter.appendProtocolVersion(null, version).toString();
 
107
    }
 
108
 
 
109
 
 
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");
 
116
        }
 
117
 
 
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);
 
123
        } else {
 
124
            result.ensureCapacity(len);
 
125
        }
 
126
 
 
127
        result.append(version.getProtocol());
 
128
        result.append('/'); 
 
129
        result.append(Integer.toString(version.getMajor())); 
 
130
        result.append('.'); 
 
131
        result.append(Integer.toString(version.getMinor())); 
 
132
 
 
133
        return result;
 
134
    }
 
135
 
 
136
 
 
137
    /**
 
138
     * Guesses the length of a formatted protocol version.
 
139
     * Needed to guess the length of a formatted request or status line.
 
140
     *
 
141
     * @param version   the protocol version to format, or <code>null</code>
 
142
     *
 
143
     * @return  the estimated length of the formatted protocol version,
 
144
     *          in characters
 
145
     */
 
146
    protected int estimateProtocolVersionLen(final ProtocolVersion version) {
 
147
        return version.getProtocol().length() + 4; // room for "HTTP/1.1"
 
148
    }
 
149
 
 
150
 
 
151
    /**
 
152
     * Formats a request line.
 
153
     *
 
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}
 
158
     *
 
159
     * @return  the formatted request line
 
160
     */
 
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();
 
166
    }
 
167
 
 
168
 
 
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");
 
175
        }
 
176
 
 
177
        CharArrayBuffer result = initBuffer(buffer);
 
178
        doFormatRequestLine(result, reqline);
 
179
 
 
180
        return result;
 
181
    }
 
182
 
 
183
 
 
184
    /**
 
185
     * Actually formats a request line.
 
186
     * Called from {@link #formatRequestLine}.
 
187
     *
 
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>
 
191
     */
 
192
    protected void doFormatRequestLine(final CharArrayBuffer buffer,
 
193
                                       final RequestLine reqline) {
 
194
        final String method = reqline.getMethod();
 
195
        final String uri    = reqline.getUri();
 
196
 
 
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);
 
201
 
 
202
        buffer.append(method);
 
203
        buffer.append(' ');
 
204
        buffer.append(uri);
 
205
        buffer.append(' ');
 
206
        appendProtocolVersion(buffer, reqline.getProtocolVersion());
 
207
    }
 
208
 
 
209
 
 
210
 
 
211
    /**
 
212
     * Formats a status line.
 
213
     *
 
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}
 
218
     *
 
219
     * @return  the formatted status line
 
220
     */
 
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();
 
226
    }
 
227
 
 
228
 
 
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");
 
235
        }
 
236
 
 
237
        CharArrayBuffer result = initBuffer(buffer);
 
238
        doFormatStatusLine(result, statline);
 
239
 
 
240
        return result;
 
241
    }
 
242
 
 
243
 
 
244
    /**
 
245
     * Actually formats a status line.
 
246
     * Called from {@link #formatStatusLine}.
 
247
     *
 
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>
 
251
     */
 
252
    protected void doFormatStatusLine(final CharArrayBuffer buffer,
 
253
                                      final StatusLine statline) {
 
254
 
 
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();
 
260
        }
 
261
        buffer.ensureCapacity(len);
 
262
 
 
263
        appendProtocolVersion(buffer, statline.getProtocolVersion());
 
264
        buffer.append(' ');
 
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);
 
269
        }
 
270
    }
 
271
 
 
272
 
 
273
    /**
 
274
     * Formats a header.
 
275
     *
 
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}
 
280
     *
 
281
     * @return  the formatted header
 
282
     */
 
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();
 
288
    }
 
289
 
 
290
 
 
291
    // non-javadoc, see interface LineFormatter
 
292
    public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
 
293
                                        Header header) {
 
294
        if (header == null) {
 
295
            throw new IllegalArgumentException
 
296
                ("Header may not be null");
 
297
        }
 
298
        CharArrayBuffer result = null;
 
299
 
 
300
        if (header instanceof FormattedHeader) {
 
301
            // If the header is backed by a buffer, re-use the buffer
 
302
            result = ((FormattedHeader)header).getBuffer();
 
303
        } else {
 
304
            result = initBuffer(buffer);
 
305
            doFormatHeader(result, header);
 
306
        }
 
307
        return result;
 
308
 
 
309
    } // formatHeader
 
310
 
 
311
 
 
312
    /**
 
313
     * Actually formats a header.
 
314
     * Called from {@link #formatHeader}.
 
315
     *
 
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>
 
319
     */
 
320
    protected void doFormatHeader(final CharArrayBuffer buffer,
 
321
                                  final Header header) {
 
322
        final String name = header.getName();
 
323
        final String value = header.getValue();
 
324
 
 
325
        int len = name.length() + 2;
 
326
        if (value != null) {
 
327
            len += value.length();
 
328
        }
 
329
        buffer.ensureCapacity(len);
 
330
 
 
331
        buffer.append(name);
 
332
        buffer.append(": ");
 
333
        if (value != null) {
 
334
            buffer.append(value);
 
335
        }
 
336
    }
 
337
 
 
338
 
 
339
} // class BasicLineFormatter