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     public ProtocolVersion getProtocolVersion() {
125         return this.ver;
126     }
127 
128     // non-javadoc, see interface HttpResponse
129     public StatusLine getStatusLine() {
130         if (this.statusline == null) {
131             this.statusline = new BasicStatusLine(
132                     this.ver != null ? this.ver : HttpVersion.HTTP_1_1,
133                     this.code,
134                     this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code));
135         }
136         return this.statusline;
137     }
138 
139     // non-javadoc, see interface HttpResponse
140     public HttpEntity getEntity() {
141         return this.entity;
142     }
143 
144     public Locale getLocale() {
145         return this.locale;
146     }
147 
148     // non-javadoc, see interface HttpResponse
149     public void setStatusLine(final StatusLine statusline) {
150         this.statusline = Args.notNull(statusline, "Status line");
151         this.ver = statusline.getProtocolVersion();
152         this.code = statusline.getStatusCode();
153         this.reasonPhrase = statusline.getReasonPhrase();
154     }
155 
156     // non-javadoc, see interface HttpResponse
157     public void setStatusLine(final ProtocolVersion ver, final int code) {
158         Args.notNegative(code, "Status code");
159         this.statusline = null;
160         this.ver = ver;
161         this.code = code;
162         this.reasonPhrase = null;
163     }
164 
165     // non-javadoc, see interface HttpResponse
166     public void setStatusLine(
167             final ProtocolVersion ver, final int code, final String reason) {
168         Args.notNegative(code, "Status code");
169         this.statusline = null;
170         this.ver = ver;
171         this.code = code;
172         this.reasonPhrase = reason;
173     }
174 
175     // non-javadoc, see interface HttpResponse
176     public void setStatusCode(final int code) {
177         Args.notNegative(code, "Status code");
178         this.statusline = null;
179         this.code = code;
180         this.reasonPhrase = null;
181     }
182 
183     // non-javadoc, see interface HttpResponse
184     public void setReasonPhrase(final String reason) {
185         this.statusline = null;
186         this.reasonPhrase = reason;
187     }
188 
189     // non-javadoc, see interface HttpResponse
190     public void setEntity(final HttpEntity entity) {
191         this.entity = entity;
192     }
193 
194     public void setLocale(final Locale locale) {
195         this.locale = Args.notNull(locale, "Locale");
196         this.statusline = null;
197     }
198 
199     /**
200      * Looks up a reason phrase.
201      * This method evaluates the currently set catalog and locale.
202      * It also handles a missing catalog.
203      *
204      * @param code      the status code for which to look up the reason
205      *
206      * @return  the reason phrase, or <code>null</code> if there is none
207      */
208     protected String getReason(final int code) {
209         return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
210                 this.locale != null ? this.locale : Locale.getDefault()) : null;
211     }
212 
213     @Override
214     public String toString() {
215         final StringBuilder sb = new StringBuilder();
216         sb.append(getStatusLine());
217         sb.append(' ');
218         sb.append(this.headergroup);
219         if (this.entity != null) {
220             sb.append(' ');
221             sb.append(this.entity);
222         }
223         return sb.toString();
224     }
225 
226 }