mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicLineFormatter.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:dbc343c34775
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 ch.boye.httpclientandroidlib.message;
29
30 import ch.boye.httpclientandroidlib.ProtocolVersion;
31 import ch.boye.httpclientandroidlib.RequestLine;
32 import ch.boye.httpclientandroidlib.StatusLine;
33 import ch.boye.httpclientandroidlib.Header;
34 import ch.boye.httpclientandroidlib.FormattedHeader;
35 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
36
37 /**
38 * Interface for formatting elements of the HEAD section of an HTTP message.
39 * This is the complement to {@link LineParser}.
40 * There are individual methods for formatting a request line, a
41 * status line, or a header line. The formatting does <i>not</i> include the
42 * trailing line break sequence CR-LF.
43 * The formatted lines are returned in memory, the formatter does not depend
44 * on any specific IO mechanism.
45 * Instances of this interface are expected to be stateless and thread-safe.
46 *
47 * @since 4.0
48 */
49 public class BasicLineFormatter implements LineFormatter {
50
51 /**
52 * A default instance of this class, for use as default or fallback.
53 * Note that {@link BasicLineFormatter} is not a singleton, there can
54 * be many instances of the class itself and of derived classes.
55 * The instance here provides non-customized, default behavior.
56 */
57 public final static BasicLineFormatter DEFAULT = new BasicLineFormatter();
58
59
60
61 // public default constructor
62
63
64 /**
65 * Obtains a buffer for formatting.
66 *
67 * @param buffer a buffer already available, or <code>null</code>
68 *
69 * @return the cleared argument buffer if there is one, or
70 * a new empty buffer that can be used for formatting
71 */
72 protected CharArrayBuffer initBuffer(CharArrayBuffer buffer) {
73 if (buffer != null) {
74 buffer.clear();
75 } else {
76 buffer = new CharArrayBuffer(64);
77 }
78 return buffer;
79 }
80
81
82 /**
83 * Formats a protocol version.
84 *
85 * @param version the protocol version to format
86 * @param formatter the formatter to use, or
87 * <code>null</code> for the
88 * {@link #DEFAULT default}
89 *
90 * @return the formatted protocol version
91 */
92 public final static
93 String formatProtocolVersion(final ProtocolVersion version,
94 LineFormatter formatter) {
95 if (formatter == null)
96 formatter = BasicLineFormatter.DEFAULT;
97 return formatter.appendProtocolVersion(null, version).toString();
98 }
99
100
101 // non-javadoc, see interface LineFormatter
102 public CharArrayBuffer appendProtocolVersion(final CharArrayBuffer buffer,
103 final ProtocolVersion version) {
104 if (version == null) {
105 throw new IllegalArgumentException
106 ("Protocol version may not be null");
107 }
108
109 // can't use initBuffer, that would clear the argument!
110 CharArrayBuffer result = buffer;
111 final int len = estimateProtocolVersionLen(version);
112 if (result == null) {
113 result = new CharArrayBuffer(len);
114 } else {
115 result.ensureCapacity(len);
116 }
117
118 result.append(version.getProtocol());
119 result.append('/');
120 result.append(Integer.toString(version.getMajor()));
121 result.append('.');
122 result.append(Integer.toString(version.getMinor()));
123
124 return result;
125 }
126
127
128 /**
129 * Guesses the length of a formatted protocol version.
130 * Needed to guess the length of a formatted request or status line.
131 *
132 * @param version the protocol version to format, or <code>null</code>
133 *
134 * @return the estimated length of the formatted protocol version,
135 * in characters
136 */
137 protected int estimateProtocolVersionLen(final ProtocolVersion version) {
138 return version.getProtocol().length() + 4; // room for "HTTP/1.1"
139 }
140
141
142 /**
143 * Formats a request line.
144 *
145 * @param reqline the request line to format
146 * @param formatter the formatter to use, or
147 * <code>null</code> for the
148 * {@link #DEFAULT default}
149 *
150 * @return the formatted request line
151 */
152 public final static String formatRequestLine(final RequestLine reqline,
153 LineFormatter formatter) {
154 if (formatter == null)
155 formatter = BasicLineFormatter.DEFAULT;
156 return formatter.formatRequestLine(null, reqline).toString();
157 }
158
159
160 // non-javadoc, see interface LineFormatter
161 public CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
162 RequestLine reqline) {
163 if (reqline == null) {
164 throw new IllegalArgumentException
165 ("Request line may not be null");
166 }
167
168 CharArrayBuffer result = initBuffer(buffer);
169 doFormatRequestLine(result, reqline);
170
171 return result;
172 }
173
174
175 /**
176 * Actually formats a request line.
177 * Called from {@link #formatRequestLine}.
178 *
179 * @param buffer the empty buffer into which to format,
180 * never <code>null</code>
181 * @param reqline the request line to format, never <code>null</code>
182 */
183 protected void doFormatRequestLine(final CharArrayBuffer buffer,
184 final RequestLine reqline) {
185 final String method = reqline.getMethod();
186 final String uri = reqline.getUri();
187
188 // room for "GET /index.html HTTP/1.1"
189 int len = method.length() + 1 + uri.length() + 1 +
190 estimateProtocolVersionLen(reqline.getProtocolVersion());
191 buffer.ensureCapacity(len);
192
193 buffer.append(method);
194 buffer.append(' ');
195 buffer.append(uri);
196 buffer.append(' ');
197 appendProtocolVersion(buffer, reqline.getProtocolVersion());
198 }
199
200
201
202 /**
203 * Formats a status line.
204 *
205 * @param statline the status line to format
206 * @param formatter the formatter to use, or
207 * <code>null</code> for the
208 * {@link #DEFAULT default}
209 *
210 * @return the formatted status line
211 */
212 public final static String formatStatusLine(final StatusLine statline,
213 LineFormatter formatter) {
214 if (formatter == null)
215 formatter = BasicLineFormatter.DEFAULT;
216 return formatter.formatStatusLine(null, statline).toString();
217 }
218
219
220 // non-javadoc, see interface LineFormatter
221 public CharArrayBuffer formatStatusLine(final CharArrayBuffer buffer,
222 final StatusLine statline) {
223 if (statline == null) {
224 throw new IllegalArgumentException
225 ("Status line may not be null");
226 }
227
228 CharArrayBuffer result = initBuffer(buffer);
229 doFormatStatusLine(result, statline);
230
231 return result;
232 }
233
234
235 /**
236 * Actually formats a status line.
237 * Called from {@link #formatStatusLine}.
238 *
239 * @param buffer the empty buffer into which to format,
240 * never <code>null</code>
241 * @param statline the status line to format, never <code>null</code>
242 */
243 protected void doFormatStatusLine(final CharArrayBuffer buffer,
244 final StatusLine statline) {
245
246 int len = estimateProtocolVersionLen(statline.getProtocolVersion())
247 + 1 + 3 + 1; // room for "HTTP/1.1 200 "
248 final String reason = statline.getReasonPhrase();
249 if (reason != null) {
250 len += reason.length();
251 }
252 buffer.ensureCapacity(len);
253
254 appendProtocolVersion(buffer, statline.getProtocolVersion());
255 buffer.append(' ');
256 buffer.append(Integer.toString(statline.getStatusCode()));
257 buffer.append(' '); // keep whitespace even if reason phrase is empty
258 if (reason != null) {
259 buffer.append(reason);
260 }
261 }
262
263
264 /**
265 * Formats a header.
266 *
267 * @param header the header to format
268 * @param formatter the formatter to use, or
269 * <code>null</code> for the
270 * {@link #DEFAULT default}
271 *
272 * @return the formatted header
273 */
274 public final static String formatHeader(final Header header,
275 LineFormatter formatter) {
276 if (formatter == null)
277 formatter = BasicLineFormatter.DEFAULT;
278 return formatter.formatHeader(null, header).toString();
279 }
280
281
282 // non-javadoc, see interface LineFormatter
283 public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
284 Header header) {
285 if (header == null) {
286 throw new IllegalArgumentException
287 ("Header may not be null");
288 }
289 CharArrayBuffer result = null;
290
291 if (header instanceof FormattedHeader) {
292 // If the header is backed by a buffer, re-use the buffer
293 result = ((FormattedHeader)header).getBuffer();
294 } else {
295 result = initBuffer(buffer);
296 doFormatHeader(result, header);
297 }
298 return result;
299
300 } // formatHeader
301
302
303 /**
304 * Actually formats a header.
305 * Called from {@link #formatHeader}.
306 *
307 * @param buffer the empty buffer into which to format,
308 * never <code>null</code>
309 * @param header the header to format, never <code>null</code>
310 */
311 protected void doFormatHeader(final CharArrayBuffer buffer,
312 final Header header) {
313 final String name = header.getName();
314 final String value = header.getValue();
315
316 int len = name.length() + 2;
317 if (value != null) {
318 len += value.length();
319 }
320 buffer.ensureCapacity(len);
321
322 buffer.append(name);
323 buffer.append(": ");
324 if (value != null) {
325 buffer.append(value);
326 }
327 }
328
329
330 } // class BasicLineFormatter

mercurial