1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.server;
33  
34  import java.util.NoSuchElementException;
35  import java.util.StringTokenizer;
36  
37  import org.apache.commons.httpclient.HttpException;
38  import org.apache.commons.httpclient.HttpVersion;
39  import org.apache.commons.httpclient.ProtocolException;
40  
41  /***
42   * Defines a HTTP request-line, consisting of method name, URI and protocol.
43   * Instances of this class are immutable.
44   * 
45   * @author Christian Kohlschuetter
46   * @author Oleg Kalnichevski
47   */
48  public class RequestLine {
49  
50      private HttpVersion httpversion = null;
51      private String method = null;
52      private String uri= null;
53  
54      public static RequestLine parseLine(final String l) 
55      throws HttpException {
56          String method = null;
57          String uri = null;
58          String protocol = null;
59          try {
60              StringTokenizer st = new StringTokenizer(l, " ");
61              method = st.nextToken();
62              uri = st.nextToken();
63              protocol = st.nextToken();
64          } catch (NoSuchElementException e) {
65          	throw new ProtocolException("Invalid request line: " + l);
66          }
67          return new RequestLine(method, uri, protocol);
68      }
69      
70      public RequestLine(final String method, final String uri, final HttpVersion httpversion) {
71      	super();
72      	if (method == null) {
73      		throw new IllegalArgumentException("Method may not be null");
74      	}
75      	if (uri == null) {
76      		throw new IllegalArgumentException("URI may not be null");
77      	}
78      	if (httpversion == null) {
79      		throw new IllegalArgumentException("HTTP version may not be null");
80      	}
81      	this.method = method;
82          this.uri = uri;
83          this.httpversion = httpversion;
84      }
85  
86      public RequestLine(final String method, final String uri, final String httpversion)
87      throws ProtocolException {
88      	this(method, uri, HttpVersion.parse(httpversion));
89      }
90  
91      public String getMethod() {
92          return this.method;
93      }
94  
95      public HttpVersion getHttpVersion() {
96          return this.httpversion;
97      }
98  
99      public String getUri() {
100         return this.uri;
101     }
102 
103     public String toString() {
104         StringBuffer sb = new StringBuffer();
105         sb.append(this.method);
106         sb.append(" ");
107         sb.append(this.uri);
108         sb.append(" ");
109         sb.append(this.httpversion);
110         return sb.toString();
111     }
112 }