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