001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BasicHttpResponse.java $
003: * $Revision: 573864 $
004: * $Date: 2007-09-08 17:53:25 +0200 (Sat, 08 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import java.util.Locale;
035:
036: import org.apache.http.HttpEntity;
037: import org.apache.http.HttpResponse;
038: import org.apache.http.ProtocolVersion;
039: import org.apache.http.StatusLine;
040: import org.apache.http.ReasonPhraseCatalog;
041:
042: /**
043: * Basic implementation of an HTTP response that can be modified.
044: * This implementation makes sure that there always is a status line.
045: *
046: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
047: *
048: * @version $Revision: 573864 $
049: *
050: * @since 4.0
051: */
052: public class BasicHttpResponse extends AbstractHttpMessage implements
053: HttpResponse {
054:
055: private StatusLine statusline;
056: private HttpEntity entity;
057: private ReasonPhraseCatalog reasonCatalog;
058: private Locale locale;
059:
060: /**
061: * Creates a new response.
062: * This is the constructor to which all others map.
063: *
064: * @param statusline the status line
065: * @param catalog the reason phrase catalog, or
066: * <code>null</code> to disable automatic
067: * reason phrase lookup
068: * @param locale the locale for looking up reason phrases, or
069: * <code>null</code> for the system locale
070: */
071: public BasicHttpResponse(final StatusLine statusline,
072: final ReasonPhraseCatalog catalog, final Locale locale) {
073: super ();
074: if (statusline == null) {
075: throw new IllegalArgumentException(
076: "Status line may not be null.");
077: }
078: this .statusline = statusline;
079: this .reasonCatalog = catalog;
080: this .locale = (locale != null) ? locale : Locale.getDefault();
081: }
082:
083: /**
084: * Creates a response from a status line.
085: * The response will not have a reason phrase catalog and
086: * use the system default locale.
087: *
088: * @param statusline the status line
089: */
090: public BasicHttpResponse(final StatusLine statusline) {
091: this (statusline, null, null);
092: }
093:
094: /**
095: * Creates a response from elements of a status line.
096: * The response will not have a reason phrase catalog and
097: * use the system default locale.
098: *
099: * @param ver the protocol version of the response
100: * @param code the status code of the response
101: * @param reason the reason phrase to the status code, or
102: * <code>null</code>
103: */
104: public BasicHttpResponse(final ProtocolVersion ver, final int code,
105: final String reason) {
106: this (new BasicStatusLine(ver, code, reason), null, null);
107: }
108:
109: // non-javadoc, see interface HttpMessage
110: public ProtocolVersion getProtocolVersion() {
111: return this .statusline.getProtocolVersion();
112: }
113:
114: // non-javadoc, see interface HttpResponse
115: public StatusLine getStatusLine() {
116: return this .statusline;
117: }
118:
119: // non-javadoc, see interface HttpResponse
120: public HttpEntity getEntity() {
121: return this .entity;
122: }
123:
124: // non-javadoc, see interface HttpResponse
125: public Locale getLocale() {
126: return this .locale;
127: }
128:
129: // non-javadoc, see interface HttpResponse
130: public void setStatusLine(final StatusLine statusline) {
131: if (statusline == null) {
132: throw new IllegalArgumentException(
133: "Status line may not be null");
134: }
135: this .statusline = statusline;
136: }
137:
138: // non-javadoc, see interface HttpResponse
139: public void setStatusLine(final ProtocolVersion ver, final int code) {
140: // arguments checked in BasicStatusLine constructor
141: this .statusline = new BasicStatusLine(ver, code,
142: getReason(code));
143: }
144:
145: // non-javadoc, see interface HttpResponse
146: public void setStatusLine(final ProtocolVersion ver,
147: final int code, final String reason) {
148: // arguments checked in BasicStatusLine constructor
149: this .statusline = new BasicStatusLine(ver, code, reason);
150: }
151:
152: // non-javadoc, see interface HttpResponse
153: public void setStatusCode(int code) {
154: // argument checked in BasicStatusLine constructor
155: ProtocolVersion ver = this .statusline.getProtocolVersion();
156: this .statusline = new BasicStatusLine(ver, code,
157: getReason(code));
158: }
159:
160: // non-javadoc, see interface HttpResponse
161: public void setReasonPhrase(String reason) {
162:
163: if ((reason != null)
164: && ((reason.indexOf('\n') >= 0) || (reason
165: .indexOf('\r') >= 0))) {
166: throw new IllegalArgumentException(
167: "Line break in reason phrase.");
168: }
169: this .statusline = new BasicStatusLine(this .statusline
170: .getProtocolVersion(), this .statusline.getStatusCode(),
171: reason);
172: }
173:
174: // non-javadoc, see interface HttpResponse
175: public void setEntity(final HttpEntity entity) {
176: this .entity = entity;
177: }
178:
179: // non-javadoc, see interface HttpResponse
180: public void setLocale(Locale loc) {
181: if (loc == null) {
182: throw new IllegalArgumentException(
183: "Locale may not be null.");
184: }
185: this .locale = loc;
186: final int code = this .statusline.getStatusCode();
187: this .statusline = new BasicStatusLine(this .statusline
188: .getProtocolVersion(), code, getReason(code));
189: }
190:
191: /**
192: * Looks up a reason phrase.
193: * This method evaluates the currently set catalog and locale.
194: * It also handles a missing catalog.
195: *
196: * @param code the status code for which to look up the reason
197: *
198: * @return the reason phrase, or <code>null</code> if there is none
199: */
200: protected String getReason(int code) {
201: return (this.reasonCatalog == null) ? null : this.reasonCatalog
202: .getReason(code, this.locale);
203: }
204:
205: }
|