1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.http; |
8 | |
|
9 | |
import org.mule.transport.http.i18n.HttpMessages; |
10 | |
|
11 | |
import java.io.UnsupportedEncodingException; |
12 | |
import java.net.URLEncoder; |
13 | |
import java.util.NoSuchElementException; |
14 | |
import java.util.StringTokenizer; |
15 | |
|
16 | |
import org.apache.commons.httpclient.HttpException; |
17 | |
import org.apache.commons.httpclient.HttpVersion; |
18 | |
import org.apache.commons.httpclient.ProtocolException; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class RequestLine |
24 | |
{ |
25 | |
|
26 | 0 | private HttpVersion httpversion = null; |
27 | 0 | private String method = null; |
28 | 0 | private String uri = null; |
29 | |
|
30 | |
public static RequestLine parseLine(final String l) throws HttpException |
31 | |
{ |
32 | |
String method; |
33 | |
String uri; |
34 | |
String protocol; |
35 | |
try |
36 | |
{ |
37 | 0 | if (l==null) |
38 | |
{ |
39 | 0 | throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage()); |
40 | |
} |
41 | 0 | StringTokenizer st = new StringTokenizer(l, " "); |
42 | 0 | method = st.nextToken(); |
43 | 0 | uri = st.nextToken(); |
44 | 0 | protocol = st.nextToken(); |
45 | |
} |
46 | 0 | catch (NoSuchElementException e) |
47 | |
{ |
48 | 0 | throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage()); |
49 | 0 | } |
50 | 0 | return new RequestLine(method, uri, protocol); |
51 | |
} |
52 | |
|
53 | |
public RequestLine(final String method, final String uri, final HttpVersion httpversion) |
54 | |
{ |
55 | 0 | super(); |
56 | 0 | if (method == null) |
57 | |
{ |
58 | 0 | throw new IllegalArgumentException("Method may not be null"); |
59 | |
} |
60 | 0 | if (uri == null) |
61 | |
{ |
62 | 0 | throw new IllegalArgumentException("URI may not be null"); |
63 | |
} |
64 | 0 | if (httpversion == null) |
65 | |
{ |
66 | 0 | throw new IllegalArgumentException("HTTP version may not be null"); |
67 | |
} |
68 | 0 | this.method = method; |
69 | 0 | this.uri = encodeIfNeeded(uri); |
70 | 0 | this.httpversion = httpversion; |
71 | 0 | } |
72 | |
|
73 | |
public RequestLine(final String method, final String uri, final String httpversion) |
74 | |
throws ProtocolException |
75 | |
{ |
76 | 0 | this(method, uri, HttpVersion.parse(httpversion)); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private String encodeIfNeeded(String uri) |
83 | |
{ |
84 | 0 | if (uri.contains("<") || uri.contains(">")) |
85 | |
{ |
86 | |
try |
87 | |
{ |
88 | 0 | return URLEncoder.encode(uri, "UTF-8"); |
89 | |
} |
90 | 0 | catch (UnsupportedEncodingException e) |
91 | |
{ |
92 | |
|
93 | 0 | throw new RuntimeException(e); |
94 | |
} |
95 | |
} |
96 | 0 | return uri; |
97 | |
} |
98 | |
|
99 | |
public String getMethod() |
100 | |
{ |
101 | 0 | return this.method; |
102 | |
} |
103 | |
|
104 | |
public HttpVersion getHttpVersion() |
105 | |
{ |
106 | 0 | return this.httpversion; |
107 | |
} |
108 | |
|
109 | |
public String getUri() |
110 | |
{ |
111 | 0 | return this.uri; |
112 | |
} |
113 | |
|
114 | |
public String toString() |
115 | |
{ |
116 | 0 | StringBuffer sb = new StringBuffer(64); |
117 | 0 | sb.append(this.method); |
118 | 0 | sb.append(" "); |
119 | 0 | sb.append(this.uri); |
120 | 0 | sb.append(" "); |
121 | 0 | sb.append(this.httpversion); |
122 | 0 | return sb.toString(); |
123 | |
} |
124 | |
} |