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