mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHttpResponse.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/BasicHttpResponse.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,198 @@
     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 java.util.Locale;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.HttpEntity;
    1.36 +import ch.boye.httpclientandroidlib.HttpResponse;
    1.37 +import ch.boye.httpclientandroidlib.ProtocolVersion;
    1.38 +import ch.boye.httpclientandroidlib.StatusLine;
    1.39 +import ch.boye.httpclientandroidlib.ReasonPhraseCatalog;
    1.40 +
    1.41 +/**
    1.42 + * Basic implementation of {@link HttpResponse}.
    1.43 + *
    1.44 + * @since 4.0
    1.45 + */
    1.46 +public class BasicHttpResponse extends AbstractHttpMessage
    1.47 +    implements HttpResponse {
    1.48 +
    1.49 +    private StatusLine          statusline;
    1.50 +    private HttpEntity          entity;
    1.51 +    private ReasonPhraseCatalog reasonCatalog;
    1.52 +    private Locale              locale;
    1.53 +
    1.54 +
    1.55 +    /**
    1.56 +     * Creates a new response.
    1.57 +     * This is the constructor to which all others map.
    1.58 +     *
    1.59 +     * @param statusline        the status line
    1.60 +     * @param catalog           the reason phrase catalog, or
    1.61 +     *                          <code>null</code> to disable automatic
    1.62 +     *                          reason phrase lookup
    1.63 +     * @param locale            the locale for looking up reason phrases, or
    1.64 +     *                          <code>null</code> for the system locale
    1.65 +     */
    1.66 +    public BasicHttpResponse(final StatusLine statusline,
    1.67 +                             final ReasonPhraseCatalog catalog,
    1.68 +                             final Locale locale) {
    1.69 +        super();
    1.70 +        if (statusline == null) {
    1.71 +            throw new IllegalArgumentException("Status line may not be null.");
    1.72 +        }
    1.73 +        this.statusline    = statusline;
    1.74 +        this.reasonCatalog = catalog;
    1.75 +        this.locale        = (locale != null) ? locale : Locale.getDefault();
    1.76 +    }
    1.77 +
    1.78 +    /**
    1.79 +     * Creates a response from a status line.
    1.80 +     * The response will not have a reason phrase catalog and
    1.81 +     * use the system default locale.
    1.82 +     *
    1.83 +     * @param statusline        the status line
    1.84 +     */
    1.85 +    public BasicHttpResponse(final StatusLine statusline) {
    1.86 +        this(statusline, null, null);
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * Creates a response from elements of a status line.
    1.91 +     * The response will not have a reason phrase catalog and
    1.92 +     * use the system default locale.
    1.93 +     *
    1.94 +     * @param ver       the protocol version of the response
    1.95 +     * @param code      the status code of the response
    1.96 +     * @param reason    the reason phrase to the status code, or
    1.97 +     *                  <code>null</code>
    1.98 +     */
    1.99 +    public BasicHttpResponse(final ProtocolVersion ver,
   1.100 +                             final int code,
   1.101 +                             final String reason) {
   1.102 +        this(new BasicStatusLine(ver, code, reason), null, null);
   1.103 +    }
   1.104 +
   1.105 +
   1.106 +    // non-javadoc, see interface HttpMessage
   1.107 +    public ProtocolVersion getProtocolVersion() {
   1.108 +        return this.statusline.getProtocolVersion();
   1.109 +    }
   1.110 +
   1.111 +    // non-javadoc, see interface HttpResponse
   1.112 +    public StatusLine getStatusLine() {
   1.113 +        return this.statusline;
   1.114 +    }
   1.115 +
   1.116 +    // non-javadoc, see interface HttpResponse
   1.117 +    public HttpEntity getEntity() {
   1.118 +        return this.entity;
   1.119 +    }
   1.120 +
   1.121 +    // non-javadoc, see interface HttpResponse
   1.122 +    public Locale getLocale() {
   1.123 +        return this.locale;
   1.124 +    }
   1.125 +
   1.126 +    // non-javadoc, see interface HttpResponse
   1.127 +    public void setStatusLine(final StatusLine statusline) {
   1.128 +        if (statusline == null) {
   1.129 +            throw new IllegalArgumentException("Status line may not be null");
   1.130 +        }
   1.131 +        this.statusline = statusline;
   1.132 +    }
   1.133 +
   1.134 +    // non-javadoc, see interface HttpResponse
   1.135 +    public void setStatusLine(final ProtocolVersion ver, final int code) {
   1.136 +        // arguments checked in BasicStatusLine constructor
   1.137 +        this.statusline = new BasicStatusLine(ver, code, getReason(code));
   1.138 +    }
   1.139 +
   1.140 +    // non-javadoc, see interface HttpResponse
   1.141 +    public void setStatusLine(final ProtocolVersion ver, final int code,
   1.142 +                              final String reason) {
   1.143 +        // arguments checked in BasicStatusLine constructor
   1.144 +        this.statusline = new BasicStatusLine(ver, code, reason);
   1.145 +    }
   1.146 +
   1.147 +    // non-javadoc, see interface HttpResponse
   1.148 +    public void setStatusCode(int code) {
   1.149 +        // argument checked in BasicStatusLine constructor
   1.150 +        ProtocolVersion ver = this.statusline.getProtocolVersion();
   1.151 +        this.statusline = new BasicStatusLine(ver, code, getReason(code));
   1.152 +    }
   1.153 +
   1.154 +    // non-javadoc, see interface HttpResponse
   1.155 +    public void setReasonPhrase(String reason) {
   1.156 +
   1.157 +        if ((reason != null) && ((reason.indexOf('\n') >= 0) ||
   1.158 +                                 (reason.indexOf('\r') >= 0))
   1.159 +            ) {
   1.160 +            throw new IllegalArgumentException("Line break in reason phrase.");
   1.161 +        }
   1.162 +        this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(),
   1.163 +                                              this.statusline.getStatusCode(),
   1.164 +                                              reason);
   1.165 +    }
   1.166 +
   1.167 +    // non-javadoc, see interface HttpResponse
   1.168 +    public void setEntity(final HttpEntity entity) {
   1.169 +        this.entity = entity;
   1.170 +    }
   1.171 +
   1.172 +    // non-javadoc, see interface HttpResponse
   1.173 +    public void setLocale(Locale loc) {
   1.174 +        if (loc == null) {
   1.175 +            throw new IllegalArgumentException("Locale may not be null.");
   1.176 +        }
   1.177 +        this.locale = loc;
   1.178 +        final int code = this.statusline.getStatusCode();
   1.179 +        this.statusline = new BasicStatusLine
   1.180 +            (this.statusline.getProtocolVersion(), code, getReason(code));
   1.181 +    }
   1.182 +
   1.183 +    /**
   1.184 +     * Looks up a reason phrase.
   1.185 +     * This method evaluates the currently set catalog and locale.
   1.186 +     * It also handles a missing catalog.
   1.187 +     *
   1.188 +     * @param code      the status code for which to look up the reason
   1.189 +     *
   1.190 +     * @return  the reason phrase, or <code>null</code> if there is none
   1.191 +     */
   1.192 +    protected String getReason(int code) {
   1.193 +        return (this.reasonCatalog == null) ?
   1.194 +            null : this.reasonCatalog.getReason(code, this.locale);
   1.195 +    }
   1.196 +
   1.197 +    public String toString() {
   1.198 +        return this.statusline + " " + this.headergroup;
   1.199 +    }
   1.200 +
   1.201 +}

mercurial