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