001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.io.dav.http;
013:
014: import java.text.ParseException;
015:
016: import org.tmatesoft.svn.core.SVNErrorMessage;
017:
018: /**
019: * @version 1.1.1
020: * @author TMate Software Ltd.
021: */
022: public class HTTPStatus {
023:
024: private String myStatusLine;
025: private String myVersion;
026: private int myCode;
027: private String myReason;
028: private HTTPHeader myHeader;
029: private SVNErrorMessage myError;
030:
031: public static boolean isHTTPStatusLine(String statusLine) {
032: try {
033: HTTPStatus status = createHTTPStatus(statusLine);
034: return status != null;
035: } catch (ParseException pe) {
036: }
037: return false;
038: }
039:
040: public static HTTPStatus createHTTPStatus(String statusLine)
041: throws ParseException {
042: int length = statusLine.length();
043: int at = 0;
044: int start = 0;
045:
046: String reason = null;
047: String version = null;
048: int code = -1;
049:
050: try {
051: while (Character.isWhitespace(statusLine.charAt(at))) {
052: ++at;
053: ++start;
054: }
055: if (!"HTTP".equals(statusLine.substring(at, at += 4))) {
056: throw new ParseException("Status-Line '" + statusLine
057: + "' does not start with HTTP", 0);
058: }
059: //handle the HTTP-Version
060: at = statusLine.indexOf(" ", at);
061: if (at <= 0) {
062: throw new ParseException(
063: "Unable to parse HTTP-Version from the status line: '"
064: + statusLine + "'", 0);
065: }
066: version = (statusLine.substring(start, at)).toUpperCase();
067:
068: //advance through spaces
069: while (statusLine.charAt(at) == ' ') {
070: at++;
071: }
072:
073: //handle the Status-Code
074: int to = statusLine.indexOf(" ", at);
075: if (to < 0) {
076: to = length;
077: }
078: try {
079: code = Integer.parseInt(statusLine.substring(at, to));
080: } catch (NumberFormatException e) {
081: throw new ParseException(
082: "Unable to parse status code from status line: '"
083: + statusLine + "'", 0);
084: }
085: //handle the Reason-Phrase
086: at = to + 1;
087: if (at < length) {
088: reason = statusLine.substring(at).trim();
089: } else {
090: reason = "";
091: }
092: } catch (StringIndexOutOfBoundsException e) {
093: throw new ParseException("Status-Line '" + statusLine
094: + "' is not valid", 0);
095: }
096: return new HTTPStatus(version, reason, code, statusLine);
097: }
098:
099: private HTTPStatus(String version, String reason, int code,
100: String statusLine) {
101: myVersion = version;
102: myStatusLine = statusLine;
103: myReason = reason;
104: myCode = code;
105: }
106:
107: public String getReason() {
108: return myReason;
109: }
110:
111: public int getCode() {
112: return myCode;
113: }
114:
115: public String getStatusLine() {
116: return myStatusLine;
117: }
118:
119: public String getVersion() {
120: return myVersion;
121: }
122:
123: public void setHeader(HTTPHeader header) {
124: myHeader = header;
125: }
126:
127: public HTTPHeader getHeader() {
128: return myHeader;
129: }
130:
131: public void setError(SVNErrorMessage error) {
132: myError = error;
133: }
134:
135: public SVNErrorMessage getError() {
136: return myError;
137: }
138:
139: }
|