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