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