View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/StatusLine.java,v 1.9.2.2 2003/12/05 20:18:22 oglueck Exp $ 3 * $Revision: 1.9.2.2 $ 4 * $Date: 2003/12/05 20:18:22 $ 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.2.2 2003/12/05 20:18:22 oglueck 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(final String statusLine) 116 throws HttpException { 117 118 int length = statusLine.length(); 119 120 int at = 0; 121 int start = 0; 122 try { 123 while (Character.isWhitespace(statusLine.charAt(at))) { 124 ++at; 125 ++start; 126 } 127 if (!"HTTP".equals(statusLine.substring(at, at += 4))) { 128 throw new HttpException("Status-Line '" + statusLine 129 + "' does not start with HTTP"); 130 } 131 } catch (StringIndexOutOfBoundsException e) { 132 throw new HttpException("Status-Line '" + statusLine + "' is not valid"); 133 } 134 //handle the HTTP-Version 135 at = statusLine.indexOf(" ", at); 136 if (at <= 0) { 137 throw new HttpException( 138 "Unable to parse HTTP-Version from the status line: '" 139 + statusLine + "'"); 140 } 141 this.httpVersion = (statusLine.substring(start, at)).toUpperCase(); 142 143 //advance through spaces 144 while (statusLine.charAt(at) == ' ') { 145 at++; 146 } 147 148 //handle the Status-Code 149 int to = statusLine.indexOf(" ", at); 150 if (to < 0) { 151 to = length; 152 } 153 try { 154 this.statusCode = Integer.parseInt(statusLine.substring(at, to)); 155 } catch (NumberFormatException e) { 156 throw new HttpException( 157 "Unable to parse status code from status line: '" 158 + statusLine + "'"); 159 } 160 161 //handle the Reason-Phrase 162 at = to + 1; 163 try { 164 if (at < length) { 165 this.reasonPhrase = statusLine.substring(at).trim(); 166 } else { 167 this.reasonPhrase = ""; 168 } 169 } catch (StringIndexOutOfBoundsException e) { 170 throw new HttpException("Status text not specified: '" 171 + statusLine + "'"); 172 } 173 //save the original Status-Line if everything is OK 174 this.statusLine = new String(statusLine); 175 } 176 177 178 // --------------------------------------------------------- Public Methods 179 180 /*** 181 * @return the Status-Code 182 */ 183 public final int getStatusCode() { 184 return statusCode; 185 } 186 187 /*** 188 * @return the HTTP-Version 189 */ 190 public final String getHttpVersion() { 191 return httpVersion; 192 } 193 194 /*** 195 * @return the Reason-Phrase 196 */ 197 public final String getReasonPhrase() { 198 return reasonPhrase; 199 } 200 201 /*** 202 * Return the status line as it was set by the constructor. 203 * @return a string represenation of this object. 204 */ 205 public final String toString() { 206 return statusLine; 207 } 208 209 /*** 210 * Tests if the string starts with 'HTTP' signature. 211 * @param s string to test 212 * @return <tt>true</tt> if the line starts with 'HTTP' 213 * signature, <tt>false</tt> otherwise. 214 */ 215 public static boolean startsWithHTTP(final String s) { 216 try { 217 int at = 0; 218 while (Character.isWhitespace(s.charAt(at))) { 219 ++at; 220 } 221 return ("HTTP".equals(s.substring(at, at + 4))); 222 } catch (StringIndexOutOfBoundsException e) { 223 return false; 224 } 225 } 226 }

This page was automatically generated by Maven