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 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 }