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