1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpResponse.java $
3 * $Revision: 573864 $
4 * $Date: 2007-09-08 08:53:25 -0700 (Sat, 08 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
32package org.apache.http.message;
33
34import java.util.Locale;
35
36import org.apache.http.HttpEntity;
37import org.apache.http.HttpResponse;
38import org.apache.http.ProtocolVersion;
39import org.apache.http.StatusLine;
40import org.apache.http.ReasonPhraseCatalog;
41
42
43/**
44 * Basic implementation of an HTTP response that can be modified.
45 * This implementation makes sure that there always is a status line.
46 *
47 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48 *
49 * @version $Revision: 573864 $
50 *
51 * @since 4.0
52 *
53 * @deprecated Please use {@link java.net.URL#openConnection} instead.
54 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
55 *     for further details.
56 */
57@Deprecated
58public class BasicHttpResponse extends AbstractHttpMessage
59    implements HttpResponse {
60
61    private StatusLine          statusline;
62    private HttpEntity          entity;
63    private ReasonPhraseCatalog reasonCatalog;
64    private Locale              locale;
65
66
67    /**
68     * Creates a new response.
69     * This is the constructor to which all others map.
70     *
71     * @param statusline        the status line
72     * @param catalog           the reason phrase catalog, or
73     *                          <code>null</code> to disable automatic
74     *                          reason phrase lookup
75     * @param locale            the locale for looking up reason phrases, or
76     *                          <code>null</code> for the system locale
77     */
78    public BasicHttpResponse(final StatusLine statusline,
79                             final ReasonPhraseCatalog catalog,
80                             final Locale locale) {
81        super();
82        if (statusline == null) {
83            throw new IllegalArgumentException("Status line may not be null.");
84        }
85        this.statusline    = statusline;
86        this.reasonCatalog = catalog;
87        this.locale        = (locale != null) ? locale : Locale.getDefault();
88    }
89
90    /**
91     * Creates a response from a status line.
92     * The response will not have a reason phrase catalog and
93     * use the system default locale.
94     *
95     * @param statusline        the status line
96     */
97    public BasicHttpResponse(final StatusLine statusline) {
98        this(statusline, null, null);
99    }
100
101    /**
102     * Creates a response from elements of a status line.
103     * The response will not have a reason phrase catalog and
104     * use the system default locale.
105     *
106     * @param ver       the protocol version of the response
107     * @param code      the status code of the response
108     * @param reason    the reason phrase to the status code, or
109     *                  <code>null</code>
110     */
111    public BasicHttpResponse(final ProtocolVersion ver,
112                             final int code,
113                             final String reason) {
114        this(new BasicStatusLine(ver, code, reason), null, null);
115    }
116
117
118    // non-javadoc, see interface HttpMessage
119    public ProtocolVersion getProtocolVersion() {
120        return this.statusline.getProtocolVersion();
121    }
122
123    // non-javadoc, see interface HttpResponse
124    public StatusLine getStatusLine() {
125        return this.statusline;
126    }
127
128    // non-javadoc, see interface HttpResponse
129    public HttpEntity getEntity() {
130        return this.entity;
131    }
132
133    // non-javadoc, see interface HttpResponse
134    public Locale getLocale() {
135        return this.locale;
136    }
137
138    // non-javadoc, see interface HttpResponse
139    public void setStatusLine(final StatusLine statusline) {
140        if (statusline == null) {
141            throw new IllegalArgumentException("Status line may not be null");
142        }
143        this.statusline = statusline;
144    }
145
146    // non-javadoc, see interface HttpResponse
147    public void setStatusLine(final ProtocolVersion ver, final int code) {
148        // arguments checked in BasicStatusLine constructor
149        this.statusline = new BasicStatusLine(ver, code, getReason(code));
150    }
151
152    // non-javadoc, see interface HttpResponse
153    public void setStatusLine(final ProtocolVersion ver, final int code,
154                              final String reason) {
155        // arguments checked in BasicStatusLine constructor
156        this.statusline = new BasicStatusLine(ver, code, reason);
157    }
158
159    // non-javadoc, see interface HttpResponse
160    public void setStatusCode(int code) {
161        // argument checked in BasicStatusLine constructor
162        ProtocolVersion ver = this.statusline.getProtocolVersion();
163        this.statusline = new BasicStatusLine(ver, code, getReason(code));
164    }
165
166    // non-javadoc, see interface HttpResponse
167    public void setReasonPhrase(String reason) {
168
169        if ((reason != null) && ((reason.indexOf('\n') >= 0) ||
170                                 (reason.indexOf('\r') >= 0))
171            ) {
172            throw new IllegalArgumentException("Line break in reason phrase.");
173        }
174        this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(),
175                                              this.statusline.getStatusCode(),
176                                              reason);
177    }
178
179    // non-javadoc, see interface HttpResponse
180    public void setEntity(final HttpEntity entity) {
181        this.entity = entity;
182    }
183
184    // non-javadoc, see interface HttpResponse
185    public void setLocale(Locale loc) {
186        if (loc == null) {
187            throw new IllegalArgumentException("Locale may not be null.");
188        }
189        this.locale = loc;
190        final int code = this.statusline.getStatusCode();
191        this.statusline = new BasicStatusLine
192            (this.statusline.getProtocolVersion(), code, getReason(code));
193    }
194
195    /**
196     * Looks up a reason phrase.
197     * This method evaluates the currently set catalog and locale.
198     * It also handles a missing catalog.
199     *
200     * @param code      the status code for which to look up the reason
201     *
202     * @return  the reason phrase, or <code>null</code> if there is none
203     */
204    protected String getReason(int code) {
205        return (this.reasonCatalog == null) ?
206            null : this.reasonCatalog.getReason(code, this.locale);
207    }
208
209}
210