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