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 */
27
28 package org.apache.http.message;
29
30 import java.util.Locale;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpVersion;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.ReasonPhraseCatalog;
37 import org.apache.http.StatusLine;
38 import org.apache.http.annotation.NotThreadSafe;
39 import org.apache.http.impl.DefaultHttpRequestFactory;
40 import org.apache.http.impl.DefaultHttpResponseFactory;
41 import org.apache.http.util.Args;
42
43 /**
44 * Basic implementation of {@link HttpResponse}.
45 *
46 * @see DefaultHttpResponseFactory
47 *
48 * @since 4.0
49 */
50 @NotThreadSafe
51 public class BasicHttpResponse extends AbstractHttpMessage implements HttpResponse {
52
53 private StatusLine statusline;
54 private ProtocolVersion ver;
55 private int code;
56 private String reasonPhrase;
57 private HttpEntity entity;
58 private final ReasonPhraseCatalog reasonCatalog;
59 private Locale locale;
60
61 /**
62 * Creates a new response.
63 * This is the constructor to which all others map.
64 *
65 * @param statusline the status line
66 * @param catalog the reason phrase catalog, or
67 * <code>null</code> to disable automatic
68 * reason phrase lookup
69 * @param locale the locale for looking up reason phrases, or
70 * <code>null</code> for the system locale
71 *
72 * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
73 */
74 @Deprecated
75 public BasicHttpResponse(final StatusLine statusline,
76 final ReasonPhraseCatalog catalog,
77 final Locale locale) {
78 super();
79 this.statusline = Args.notNull(statusline, "Status line");
80 this.ver = statusline.getProtocolVersion();
81 this.code = statusline.getStatusCode();
82 this.reasonPhrase = statusline.getReasonPhrase();
83 this.reasonCatalog = catalog;
84 this.locale = locale;
85 }
86
87 /**
88 * Creates a response from a status line.
89 * The response will not have a reason phrase catalog and
90 * use the system default locale.
91 *
92 * @param statusline the status line
93 */
94 public BasicHttpResponse(final StatusLine statusline) {
95 this(statusline, null, null);
96 }
97
98 /**
99 * Creates a response from elements of a status line.
100 * The response will not have a reason phrase catalog and
101 * use the system default locale.
102 *
103 * @param ver the protocol version of the response
104 * @param code the status code of the response
105 * @param reason the reason phrase to the status code, or
106 * <code>null</code>
107 */
108 public BasicHttpResponse(final ProtocolVersion ver,
109 final int code,
110 final String reason) {
111 super();
112 Args.notNegative(code, "Status code");
113 this.statusline = null;
114 this.ver = ver;
115 this.code = code;
116 this.reasonPhrase = reason;
117 this.reasonCatalog = null;
118 this.locale = null;
119 }
120
121
122 // non-javadoc, see interface HttpMessage
123 public ProtocolVersion getProtocolVersion() {
124 return this.ver;
125 }
126
127 // non-javadoc, see interface HttpResponse
128 public StatusLine getStatusLine() {
129 if (this.statusline == null) {
130 this.statusline = new BasicStatusLine(
131 this.ver != null ? this.ver : HttpVersion.HTTP_1_1,
132 this.code,
133 this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code));
134 }
135 return this.statusline;
136 }
137
138 // non-javadoc, see interface HttpResponse
139 public HttpEntity getEntity() {
140 return this.entity;
141 }
142
143 /**
144 * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
145 */
146 @Deprecated
147 public Locale getLocale() {
148 return this.locale;
149 }
150
151 // non-javadoc, see interface HttpResponse
152 public void setStatusLine(final StatusLine statusline) {
153 this.statusline = Args.notNull(statusline, "Status line");
154 this.ver = statusline.getProtocolVersion();
155 this.code = statusline.getStatusCode();
156 this.reasonPhrase = statusline.getReasonPhrase();
157 }
158
159 // non-javadoc, see interface HttpResponse
160 public void setStatusLine(final ProtocolVersion ver, final int code) {
161 Args.notNegative(code, "Status code");
162 this.statusline = null;
163 this.ver = ver;
164 this.code = code;
165 this.reasonPhrase = null;
166 }
167
168 // non-javadoc, see interface HttpResponse
169 public void setStatusLine(
170 final ProtocolVersion ver, final int code, final String reason) {
171 Args.notNegative(code, "Status code");
172 this.statusline = null;
173 this.ver = ver;
174 this.code = code;
175 this.reasonPhrase = reason;
176 }
177
178 // non-javadoc, see interface HttpResponse
179 public void setStatusCode(final int code) {
180 Args.notNegative(code, "Status code");
181 this.statusline = null;
182 this.code = code;
183 }
184
185 // non-javadoc, see interface HttpResponse
186 public void setReasonPhrase(final String reason) {
187 this.statusline = null;
188 this.reasonPhrase = reason;
189 }
190
191 // non-javadoc, see interface HttpResponse
192 public void setEntity(final HttpEntity entity) {
193 this.entity = entity;
194 }
195
196 /**
197 * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
198 */
199 @Deprecated
200 public void setLocale(final Locale locale) {
201 this.locale = Args.notNull(locale, "Locale");
202 this.statusline = null;
203 }
204
205 /**
206 * Looks up a reason phrase.
207 * This method evaluates the currently set catalog and locale.
208 * It also handles a missing catalog.
209 *
210 * @param code the status code for which to look up the reason
211 *
212 * @return the reason phrase, or <code>null</code> if there is none
213 *
214 * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
215 */
216 @Deprecated
217 protected String getReason(final int code) {
218 return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
219 this.locale != null ? this.locale : Locale.getDefault()) : null;
220 }
221
222 @Override
223 public String toString() {
224 final StatusLine statusline = getStatusLine();
225 return statusline + " " + this.headergroup;
226 }
227
228 }