001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/RequestLine.java,v 1.4 2004/09/14 15:50:41 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: package org.apache.commons.httpclient.server;
034:
035: import java.util.NoSuchElementException;
036: import java.util.StringTokenizer;
037:
038: import org.apache.commons.httpclient.HttpException;
039: import org.apache.commons.httpclient.HttpVersion;
040: import org.apache.commons.httpclient.ProtocolException;
041:
042: /**
043: * Defines a HTTP request-line, consisting of method name, URI and protocol.
044: * Instances of this class are immutable.
045: *
046: * @author Christian Kohlschuetter
047: * @author Oleg Kalnichevski
048: */
049: public class RequestLine {
050:
051: private HttpVersion httpversion = null;
052: private String method = null;
053: private String uri = null;
054:
055: public static RequestLine parseLine(final String l)
056: throws HttpException {
057: String method = null;
058: String uri = null;
059: String protocol = null;
060: try {
061: StringTokenizer st = new StringTokenizer(l, " ");
062: method = st.nextToken();
063: uri = st.nextToken();
064: protocol = st.nextToken();
065: } catch (NoSuchElementException e) {
066: throw new ProtocolException("Invalid request line: " + l);
067: }
068: return new RequestLine(method, uri, protocol);
069: }
070:
071: public RequestLine(final String method, final String uri,
072: final HttpVersion httpversion) {
073: super ();
074: if (method == null) {
075: throw new IllegalArgumentException("Method may not be null");
076: }
077: if (uri == null) {
078: throw new IllegalArgumentException("URI may not be null");
079: }
080: if (httpversion == null) {
081: throw new IllegalArgumentException(
082: "HTTP version may not be null");
083: }
084: this .method = method;
085: this .uri = uri;
086: this .httpversion = httpversion;
087: }
088:
089: public RequestLine(final String method, final String uri,
090: final String httpversion) throws ProtocolException {
091: this (method, uri, HttpVersion.parse(httpversion));
092: }
093:
094: public String getMethod() {
095: return this .method;
096: }
097:
098: public HttpVersion getHttpVersion() {
099: return this .httpversion;
100: }
101:
102: public String getUri() {
103: return this .uri;
104: }
105:
106: public String toString() {
107: StringBuffer sb = new StringBuffer();
108: sb.append(this .method);
109: sb.append(" ");
110: sb.append(this .uri);
111: sb.append(" ");
112: sb.append(this.httpversion);
113: return sb.toString();
114: }
115: }
|