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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicLineFormatter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,330 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.message;
    1.32 +
    1.33 +import ch.boye.httpclientandroidlib.ProtocolVersion;
    1.34 +import ch.boye.httpclientandroidlib.RequestLine;
    1.35 +import ch.boye.httpclientandroidlib.StatusLine;
    1.36 +import ch.boye.httpclientandroidlib.Header;
    1.37 +import ch.boye.httpclientandroidlib.FormattedHeader;
    1.38 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.39 +
    1.40 +/**
    1.41 + * Interface for formatting elements of the HEAD section of an HTTP message.
    1.42 + * This is the complement to {@link LineParser}.
    1.43 + * There are individual methods for formatting a request line, a
    1.44 + * status line, or a header line. The formatting does <i>not</i> include the
    1.45 + * trailing line break sequence CR-LF.
    1.46 + * The formatted lines are returned in memory, the formatter does not depend
    1.47 + * on any specific IO mechanism.
    1.48 + * Instances of this interface are expected to be stateless and thread-safe.
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +public class BasicLineFormatter implements LineFormatter {
    1.53 +
    1.54 +    /**
    1.55 +     * A default instance of this class, for use as default or fallback.
    1.56 +     * Note that {@link BasicLineFormatter} is not a singleton, there can
    1.57 +     * be many instances of the class itself and of derived classes.
    1.58 +     * The instance here provides non-customized, default behavior.
    1.59 +     */
    1.60 +    public final static BasicLineFormatter DEFAULT = new BasicLineFormatter();
    1.61 +
    1.62 +
    1.63 +
    1.64 +    // public default constructor
    1.65 +
    1.66 +
    1.67 +    /**
    1.68 +     * Obtains a buffer for formatting.
    1.69 +     *
    1.70 +     * @param buffer    a buffer already available, or <code>null</code>
    1.71 +     *
    1.72 +     * @return  the cleared argument buffer if there is one, or
    1.73 +     *          a new empty buffer that can be used for formatting
    1.74 +     */
    1.75 +    protected CharArrayBuffer initBuffer(CharArrayBuffer buffer) {
    1.76 +        if (buffer != null) {
    1.77 +            buffer.clear();
    1.78 +        } else {
    1.79 +            buffer = new CharArrayBuffer(64);
    1.80 +        }
    1.81 +        return buffer;
    1.82 +    }
    1.83 +
    1.84 +
    1.85 +    /**
    1.86 +     * Formats a protocol version.
    1.87 +     *
    1.88 +     * @param version           the protocol version to format
    1.89 +     * @param formatter         the formatter to use, or
    1.90 +     *                          <code>null</code> for the
    1.91 +     *                          {@link #DEFAULT default}
    1.92 +     *
    1.93 +     * @return  the formatted protocol version
    1.94 +     */
    1.95 +    public final static
    1.96 +        String formatProtocolVersion(final ProtocolVersion version,
    1.97 +                                     LineFormatter formatter) {
    1.98 +        if (formatter == null)
    1.99 +            formatter = BasicLineFormatter.DEFAULT;
   1.100 +        return formatter.appendProtocolVersion(null, version).toString();
   1.101 +    }
   1.102 +
   1.103 +
   1.104 +    // non-javadoc, see interface LineFormatter
   1.105 +    public CharArrayBuffer appendProtocolVersion(final CharArrayBuffer buffer,
   1.106 +                                                 final ProtocolVersion version) {
   1.107 +        if (version == null) {
   1.108 +            throw new IllegalArgumentException
   1.109 +                ("Protocol version may not be null");
   1.110 +        }
   1.111 +
   1.112 +        // can't use initBuffer, that would clear the argument!
   1.113 +        CharArrayBuffer result = buffer;
   1.114 +        final int len = estimateProtocolVersionLen(version);
   1.115 +        if (result == null) {
   1.116 +            result = new CharArrayBuffer(len);
   1.117 +        } else {
   1.118 +            result.ensureCapacity(len);
   1.119 +        }
   1.120 +
   1.121 +        result.append(version.getProtocol());
   1.122 +        result.append('/');
   1.123 +        result.append(Integer.toString(version.getMajor()));
   1.124 +        result.append('.');
   1.125 +        result.append(Integer.toString(version.getMinor()));
   1.126 +
   1.127 +        return result;
   1.128 +    }
   1.129 +
   1.130 +
   1.131 +    /**
   1.132 +     * Guesses the length of a formatted protocol version.
   1.133 +     * Needed to guess the length of a formatted request or status line.
   1.134 +     *
   1.135 +     * @param version   the protocol version to format, or <code>null</code>
   1.136 +     *
   1.137 +     * @return  the estimated length of the formatted protocol version,
   1.138 +     *          in characters
   1.139 +     */
   1.140 +    protected int estimateProtocolVersionLen(final ProtocolVersion version) {
   1.141 +        return version.getProtocol().length() + 4; // room for "HTTP/1.1"
   1.142 +    }
   1.143 +
   1.144 +
   1.145 +    /**
   1.146 +     * Formats a request line.
   1.147 +     *
   1.148 +     * @param reqline           the request line to format
   1.149 +     * @param formatter         the formatter to use, or
   1.150 +     *                          <code>null</code> for the
   1.151 +     *                          {@link #DEFAULT default}
   1.152 +     *
   1.153 +     * @return  the formatted request line
   1.154 +     */
   1.155 +    public final static String formatRequestLine(final RequestLine reqline,
   1.156 +                                                 LineFormatter formatter) {
   1.157 +        if (formatter == null)
   1.158 +            formatter = BasicLineFormatter.DEFAULT;
   1.159 +        return formatter.formatRequestLine(null, reqline).toString();
   1.160 +    }
   1.161 +
   1.162 +
   1.163 +    // non-javadoc, see interface LineFormatter
   1.164 +    public CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
   1.165 +                                             RequestLine reqline) {
   1.166 +        if (reqline == null) {
   1.167 +            throw new IllegalArgumentException
   1.168 +                ("Request line may not be null");
   1.169 +        }
   1.170 +
   1.171 +        CharArrayBuffer result = initBuffer(buffer);
   1.172 +        doFormatRequestLine(result, reqline);
   1.173 +
   1.174 +        return result;
   1.175 +    }
   1.176 +
   1.177 +
   1.178 +    /**
   1.179 +     * Actually formats a request line.
   1.180 +     * Called from {@link #formatRequestLine}.
   1.181 +     *
   1.182 +     * @param buffer    the empty buffer into which to format,
   1.183 +     *                  never <code>null</code>
   1.184 +     * @param reqline   the request line to format, never <code>null</code>
   1.185 +     */
   1.186 +    protected void doFormatRequestLine(final CharArrayBuffer buffer,
   1.187 +                                       final RequestLine reqline) {
   1.188 +        final String method = reqline.getMethod();
   1.189 +        final String uri    = reqline.getUri();
   1.190 +
   1.191 +        // room for "GET /index.html HTTP/1.1"
   1.192 +        int len = method.length() + 1 + uri.length() + 1 +
   1.193 +            estimateProtocolVersionLen(reqline.getProtocolVersion());
   1.194 +        buffer.ensureCapacity(len);
   1.195 +
   1.196 +        buffer.append(method);
   1.197 +        buffer.append(' ');
   1.198 +        buffer.append(uri);
   1.199 +        buffer.append(' ');
   1.200 +        appendProtocolVersion(buffer, reqline.getProtocolVersion());
   1.201 +    }
   1.202 +
   1.203 +
   1.204 +
   1.205 +    /**
   1.206 +     * Formats a status line.
   1.207 +     *
   1.208 +     * @param statline          the status line to format
   1.209 +     * @param formatter         the formatter to use, or
   1.210 +     *                          <code>null</code> for the
   1.211 +     *                          {@link #DEFAULT default}
   1.212 +     *
   1.213 +     * @return  the formatted status line
   1.214 +     */
   1.215 +    public final static String formatStatusLine(final StatusLine statline,
   1.216 +                                                LineFormatter formatter) {
   1.217 +        if (formatter == null)
   1.218 +            formatter = BasicLineFormatter.DEFAULT;
   1.219 +        return formatter.formatStatusLine(null, statline).toString();
   1.220 +    }
   1.221 +
   1.222 +
   1.223 +    // non-javadoc, see interface LineFormatter
   1.224 +    public CharArrayBuffer formatStatusLine(final CharArrayBuffer buffer,
   1.225 +                                            final StatusLine statline) {
   1.226 +        if (statline == null) {
   1.227 +            throw new IllegalArgumentException
   1.228 +                ("Status line may not be null");
   1.229 +        }
   1.230 +
   1.231 +        CharArrayBuffer result = initBuffer(buffer);
   1.232 +        doFormatStatusLine(result, statline);
   1.233 +
   1.234 +        return result;
   1.235 +    }
   1.236 +
   1.237 +
   1.238 +    /**
   1.239 +     * Actually formats a status line.
   1.240 +     * Called from {@link #formatStatusLine}.
   1.241 +     *
   1.242 +     * @param buffer    the empty buffer into which to format,
   1.243 +     *                  never <code>null</code>
   1.244 +     * @param statline  the status line to format, never <code>null</code>
   1.245 +     */
   1.246 +    protected void doFormatStatusLine(final CharArrayBuffer buffer,
   1.247 +                                      final StatusLine statline) {
   1.248 +
   1.249 +        int len = estimateProtocolVersionLen(statline.getProtocolVersion())
   1.250 +            + 1 + 3 + 1; // room for "HTTP/1.1 200 "
   1.251 +        final String reason = statline.getReasonPhrase();
   1.252 +        if (reason != null) {
   1.253 +            len += reason.length();
   1.254 +        }
   1.255 +        buffer.ensureCapacity(len);
   1.256 +
   1.257 +        appendProtocolVersion(buffer, statline.getProtocolVersion());
   1.258 +        buffer.append(' ');
   1.259 +        buffer.append(Integer.toString(statline.getStatusCode()));
   1.260 +        buffer.append(' '); // keep whitespace even if reason phrase is empty
   1.261 +        if (reason != null) {
   1.262 +            buffer.append(reason);
   1.263 +        }
   1.264 +    }
   1.265 +
   1.266 +
   1.267 +    /**
   1.268 +     * Formats a header.
   1.269 +     *
   1.270 +     * @param header            the header to format
   1.271 +     * @param formatter         the formatter to use, or
   1.272 +     *                          <code>null</code> for the
   1.273 +     *                          {@link #DEFAULT default}
   1.274 +     *
   1.275 +     * @return  the formatted header
   1.276 +     */
   1.277 +    public final static String formatHeader(final Header header,
   1.278 +                                            LineFormatter formatter) {
   1.279 +        if (formatter == null)
   1.280 +            formatter = BasicLineFormatter.DEFAULT;
   1.281 +        return formatter.formatHeader(null, header).toString();
   1.282 +    }
   1.283 +
   1.284 +
   1.285 +    // non-javadoc, see interface LineFormatter
   1.286 +    public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
   1.287 +                                        Header header) {
   1.288 +        if (header == null) {
   1.289 +            throw new IllegalArgumentException
   1.290 +                ("Header may not be null");
   1.291 +        }
   1.292 +        CharArrayBuffer result = null;
   1.293 +
   1.294 +        if (header instanceof FormattedHeader) {
   1.295 +            // If the header is backed by a buffer, re-use the buffer
   1.296 +            result = ((FormattedHeader)header).getBuffer();
   1.297 +        } else {
   1.298 +            result = initBuffer(buffer);
   1.299 +            doFormatHeader(result, header);
   1.300 +        }
   1.301 +        return result;
   1.302 +
   1.303 +    } // formatHeader
   1.304 +
   1.305 +
   1.306 +    /**
   1.307 +     * Actually formats a header.
   1.308 +     * Called from {@link #formatHeader}.
   1.309 +     *
   1.310 +     * @param buffer    the empty buffer into which to format,
   1.311 +     *                  never <code>null</code>
   1.312 +     * @param header    the header to format, never <code>null</code>
   1.313 +     */
   1.314 +    protected void doFormatHeader(final CharArrayBuffer buffer,
   1.315 +                                  final Header header) {
   1.316 +        final String name = header.getName();
   1.317 +        final String value = header.getValue();
   1.318 +
   1.319 +        int len = name.length() + 2;
   1.320 +        if (value != null) {
   1.321 +            len += value.length();
   1.322 +        }
   1.323 +        buffer.ensureCapacity(len);
   1.324 +
   1.325 +        buffer.append(name);
   1.326 +        buffer.append(": ");
   1.327 +        if (value != null) {
   1.328 +            buffer.append(value);
   1.329 +        }
   1.330 +    }
   1.331 +
   1.332 +
   1.333 +} // class BasicLineFormatter

mercurial