1 /* Response.java --
2    Copyright (C) 2004, 2006 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package gnu.java.net.protocol.http;
40 
41 import java.io.InputStream;
42 import java.util.Date;
43 
44 /**
45  * An HTTP response.
46  *
47  * @author Chris Burdess (dog@gnu.org)
48  */
49 public class Response
50 {
51 
52   /**
53    * The HTTP major version of the server issuing the response.
54    */
55   protected final int majorVersion;
56 
57   /**
58    * The HTTP minor version of the server issuing the response.
59    */
60   protected final int minorVersion;
61 
62   /**
63    * The HTTP status code of the response.
64    */
65   protected final int code;
66 
67   /**
68    * Human-readable text of the response.
69    */
70   protected final String message;
71 
72   /**
73    * The response headers.
74    */
75   protected final Headers headers;
76 
77   /**
78    * An InputStream that returns the body of the response.
79    */
80   protected final InputStream body;
81 
82   /**
83    * Constructs a new response with the specified parameters.
84    */
Response(int majorVersion, int minorVersion, int code, String message, Headers headers, InputStream body)85   protected Response(int majorVersion, int minorVersion, int code,
86                      String message, Headers headers, InputStream body)
87   {
88     this.majorVersion = majorVersion;
89     this.minorVersion = minorVersion;
90     this.code = code;
91     this.message = message;
92     this.headers = headers;
93     this.body = body;
94   }
95 
96   /**
97    * Returns the HTTP major version of the server issuing the response.
98    * @see #majorVersion
99    */
getMajorVersion()100   public int getMajorVersion()
101   {
102     return majorVersion;
103   }
104 
105   /**
106    * Returns the HTTP minor version of the server issuing the response.
107    * @see #minorVersion
108    */
getMinorVersion()109   public int getMinorVersion()
110   {
111     return minorVersion;
112   }
113 
114   /**
115    * Returns the HTTP status code of the response.
116    * @see #code
117    */
getCode()118   public int getCode()
119   {
120     return code;
121   }
122 
123   /**
124    * Returns the class of the response.  This is the most significant
125    * digit of the status code.
126    * <dl>
127    * <dt><code>1xx</code></dt> <dd>Informational response</dd>
128    * <dt><code>2xx</code></dt> <dd>Success</dd>
129    * <dt><code>3xx</code></dt> <dd>Redirection</dd>
130    * <dt><code>4xx</code></dt> <dd>Client error</dd>
131    * <dt><code>5xx</code></dt> <dd>Server error</dd>
132    * </dl>
133    */
getCodeClass()134   public int getCodeClass()
135   {
136     return code / 100;
137   }
138 
139   /**
140    * Returns the human-readable text of the response.
141    * @see #message
142    */
getMessage()143   public String getMessage()
144   {
145     return message;
146   }
147 
148   /**
149    * Returns the headers in the response.
150    */
getHeaders()151   public Headers getHeaders()
152   {
153     return headers;
154   }
155 
156   /**
157    * Returns the header value for the specified name.
158    * @param name the header name
159    */
getHeader(String name)160   public String getHeader(String name)
161   {
162     return headers.getValue(name);
163   }
164 
165   /**
166    * Returns the header value for the specified name as an integer.
167    * @param name the header name
168    */
getIntHeader(String name)169   public int getIntHeader(String name)
170   {
171     return headers.getIntValue(name);
172   }
173 
174   /**
175    * Returns the header value for the specified name as a long.
176    * @param name the header name
177    */
getLongHeader(String name)178   public long getLongHeader(String name)
179   {
180     return headers.getLongValue(name);
181   }
182 
183   /**
184    * Returns the header value for the specified name as a date.
185    * @param name the header name
186    */
getDateHeader(String name)187   public Date getDateHeader(String name)
188   {
189     return headers.getDateValue(name);
190   }
191 
192   /**
193    * Tests whether this response indicates a redirection.
194    *
195    * @return <code>true</code> if, <code>false</code> otherwise.
196    */
isRedirect()197   public boolean isRedirect()
198   {
199     return (code != 304 && getCodeClass() == 3);
200   }
201 
202   /**
203    * Tests whether this response indicates an error.
204    * Errors are the response codes <code>4xx</code> - Client error and
205    * <code>5xx</code> - Server error.
206    *
207    * @return <code>true</code> if, <code>false</code> otherwise.
208    */
isError()209   public boolean isError()
210   {
211     return (getCodeClass() == 4 || getCodeClass() == 5);
212   }
213 
214   /**
215    * Returns an InputStream that returns the body of the response.
216    *
217    * @return the body of the response
218    */
getBody()219   public InputStream getBody()
220   {
221     return body;
222   }
223 }
224