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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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  */
    28 package ch.boye.httpclientandroidlib.message;
    30 import java.util.Locale;
    32 import ch.boye.httpclientandroidlib.HttpEntity;
    33 import ch.boye.httpclientandroidlib.HttpResponse;
    34 import ch.boye.httpclientandroidlib.ProtocolVersion;
    35 import ch.boye.httpclientandroidlib.StatusLine;
    36 import ch.boye.httpclientandroidlib.ReasonPhraseCatalog;
    38 /**
    39  * Basic implementation of {@link HttpResponse}.
    40  *
    41  * @since 4.0
    42  */
    43 public class BasicHttpResponse extends AbstractHttpMessage
    44     implements HttpResponse {
    46     private StatusLine          statusline;
    47     private HttpEntity          entity;
    48     private ReasonPhraseCatalog reasonCatalog;
    49     private Locale              locale;
    52     /**
    53      * Creates a new response.
    54      * This is the constructor to which all others map.
    55      *
    56      * @param statusline        the status line
    57      * @param catalog           the reason phrase catalog, or
    58      *                          <code>null</code> to disable automatic
    59      *                          reason phrase lookup
    60      * @param locale            the locale for looking up reason phrases, or
    61      *                          <code>null</code> for the system locale
    62      */
    63     public BasicHttpResponse(final StatusLine statusline,
    64                              final ReasonPhraseCatalog catalog,
    65                              final Locale locale) {
    66         super();
    67         if (statusline == null) {
    68             throw new IllegalArgumentException("Status line may not be null.");
    69         }
    70         this.statusline    = statusline;
    71         this.reasonCatalog = catalog;
    72         this.locale        = (locale != null) ? locale : Locale.getDefault();
    73     }
    75     /**
    76      * Creates a response from a status line.
    77      * The response will not have a reason phrase catalog and
    78      * use the system default locale.
    79      *
    80      * @param statusline        the status line
    81      */
    82     public BasicHttpResponse(final StatusLine statusline) {
    83         this(statusline, null, null);
    84     }
    86     /**
    87      * Creates a response from elements of a status line.
    88      * The response will not have a reason phrase catalog and
    89      * use the system default locale.
    90      *
    91      * @param ver       the protocol version of the response
    92      * @param code      the status code of the response
    93      * @param reason    the reason phrase to the status code, or
    94      *                  <code>null</code>
    95      */
    96     public BasicHttpResponse(final ProtocolVersion ver,
    97                              final int code,
    98                              final String reason) {
    99         this(new BasicStatusLine(ver, code, reason), null, null);
   100     }
   103     // non-javadoc, see interface HttpMessage
   104     public ProtocolVersion getProtocolVersion() {
   105         return this.statusline.getProtocolVersion();
   106     }
   108     // non-javadoc, see interface HttpResponse
   109     public StatusLine getStatusLine() {
   110         return this.statusline;
   111     }
   113     // non-javadoc, see interface HttpResponse
   114     public HttpEntity getEntity() {
   115         return this.entity;
   116     }
   118     // non-javadoc, see interface HttpResponse
   119     public Locale getLocale() {
   120         return this.locale;
   121     }
   123     // non-javadoc, see interface HttpResponse
   124     public void setStatusLine(final StatusLine statusline) {
   125         if (statusline == null) {
   126             throw new IllegalArgumentException("Status line may not be null");
   127         }
   128         this.statusline = statusline;
   129     }
   131     // non-javadoc, see interface HttpResponse
   132     public void setStatusLine(final ProtocolVersion ver, final int code) {
   133         // arguments checked in BasicStatusLine constructor
   134         this.statusline = new BasicStatusLine(ver, code, getReason(code));
   135     }
   137     // non-javadoc, see interface HttpResponse
   138     public void setStatusLine(final ProtocolVersion ver, final int code,
   139                               final String reason) {
   140         // arguments checked in BasicStatusLine constructor
   141         this.statusline = new BasicStatusLine(ver, code, reason);
   142     }
   144     // non-javadoc, see interface HttpResponse
   145     public void setStatusCode(int code) {
   146         // argument checked in BasicStatusLine constructor
   147         ProtocolVersion ver = this.statusline.getProtocolVersion();
   148         this.statusline = new BasicStatusLine(ver, code, getReason(code));
   149     }
   151     // non-javadoc, see interface HttpResponse
   152     public void setReasonPhrase(String reason) {
   154         if ((reason != null) && ((reason.indexOf('\n') >= 0) ||
   155                                  (reason.indexOf('\r') >= 0))
   156             ) {
   157             throw new IllegalArgumentException("Line break in reason phrase.");
   158         }
   159         this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(),
   160                                               this.statusline.getStatusCode(),
   161                                               reason);
   162     }
   164     // non-javadoc, see interface HttpResponse
   165     public void setEntity(final HttpEntity entity) {
   166         this.entity = entity;
   167     }
   169     // non-javadoc, see interface HttpResponse
   170     public void setLocale(Locale loc) {
   171         if (loc == null) {
   172             throw new IllegalArgumentException("Locale may not be null.");
   173         }
   174         this.locale = loc;
   175         final int code = this.statusline.getStatusCode();
   176         this.statusline = new BasicStatusLine
   177             (this.statusline.getProtocolVersion(), code, getReason(code));
   178     }
   180     /**
   181      * Looks up a reason phrase.
   182      * This method evaluates the currently set catalog and locale.
   183      * It also handles a missing catalog.
   184      *
   185      * @param code      the status code for which to look up the reason
   186      *
   187      * @return  the reason phrase, or <code>null</code> if there is none
   188      */
   189     protected String getReason(int code) {
   190         return (this.reasonCatalog == null) ?
   191             null : this.reasonCatalog.getReason(code, this.locale);
   192     }
   194     public String toString() {
   195         return this.statusline + " " + this.headergroup;
   196     }
   198 }

mercurial