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