1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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 }