View Javadoc

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