1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/StatusLine.java,v 1.9 2003/04/01 00:25:24 mbecke Exp $
3 * $Revision: 1.9 $
4 * $Date: 2003/04/01 00:25:24 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient;
65
66 /***
67 * Represents a Status-Line as returned from a HTTP server.
68 *
69 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a> states
70 * the following regarding the Status-Line:
71 * <pre>
72 * 6.1 Status-Line
73 *
74 * The first line of a Response message is the Status-Line, consisting
75 * of the protocol version followed by a numeric status code and its
76 * associated textual phrase, with each element separated by SP
77 * characters. No CR or LF is allowed except in the final CRLF sequence.
78 *
79 * Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
80 * </pre>
81 * <p>
82 * This class is immutable and is inherently thread safe.
83 *
84 * @see HttpStatus
85 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
86 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
87 * @version $Id: StatusLine.java,v 1.9 2003/04/01 00:25:24 mbecke Exp $
88 * @since 2.0
89 */
90 public class StatusLine {
91
92 // ----------------------------------------------------- Instance Variables
93
94 /*** The original Status-Line. */
95 private final String statusLine;
96
97 /*** The HTTP-Version. */
98 private final String httpVersion;
99
100 /*** The Status-Code. */
101 private final int statusCode;
102
103 /*** The Reason-Phrase. */
104 private final String reasonPhrase;
105
106
107 // ----------------------------------------------------------- Constructors
108
109 /***
110 * Default constructor.
111 *
112 * @param statusLine the status line returned from the HTTP server
113 * @throws HttpException if the status line is invalid
114 */
115 public StatusLine(String statusLine)
116 throws HttpException {
117
118 //save the original Status-Line
119 this.statusLine = new String(statusLine);
120 int length = statusLine.length();
121
122
123 //check validity of the Status-Line
124 if (!statusLine.startsWith("HTTP")) {
125 throw new HttpException("Status-Line '" + statusLine
126 + "' does not start with HTTP");
127 }
128
129 //handle the HTTP-Version
130 int at = statusLine.indexOf(" ");
131 if (at <= 0) {
132 throw new HttpException(
133 "Unable to parse HTTP-Version from the status line: '"
134 + statusLine + "'");
135 }
136 this.httpVersion = (statusLine.substring(0, at)).toUpperCase();
137
138 //advance through spaces
139 while (statusLine.charAt(at) == ' ') {
140 at++;
141 }
142
143 //handle the Status-Code
144 int to = statusLine.indexOf(" ", at);
145 if (to < 0) {
146 to = length;
147 }
148 try {
149 this.statusCode = Integer.parseInt(statusLine.substring(at, to));
150 } catch (NumberFormatException e) {
151 throw new HttpException(
152 "Unable to parse status code from status line: '"
153 + statusLine + "'");
154 }
155
156 //handle the Reason-Phrase
157 at = to + 1;
158 try {
159 if (at < length) {
160 this.reasonPhrase = statusLine.substring(at).trim();
161 } else {
162 this.reasonPhrase = "";
163 }
164 } catch (StringIndexOutOfBoundsException e) {
165 throw new HttpException("Status text not specified: '"
166 + statusLine + "'");
167 }
168 }
169
170
171 // --------------------------------------------------------- Public Methods
172
173 /***
174 * @return the Status-Code
175 */
176 public final int getStatusCode() {
177 return statusCode;
178 }
179
180 /***
181 * @return the HTTP-Version
182 */
183 public final String getHttpVersion() {
184 return httpVersion;
185 }
186
187 /***
188 * @return the Reason-Phrase
189 */
190 public final String getReasonPhrase() {
191 return reasonPhrase;
192 }
193
194 /***
195 * Return a string representation of this object.
196 * @return a string represenation of this object.
197 */
198 public final String toString() {
199 return statusLine;
200 }
201 }
This page was automatically generated by Maven