KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > RequestLine


1 /*
2  * $Id: RequestLine.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import org.apache.commons.httpclient.HttpException;
14 import org.apache.commons.httpclient.HttpVersion;
15 import org.apache.commons.httpclient.ProtocolException;
16
17 import java.util.NoSuchElementException JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 /**
21  * Defines a HTTP request-line, consisting of method name, URI and protocol.
22  */

23 public class RequestLine
24 {
25
26     private HttpVersion httpversion = null;
27     private String JavaDoc method = null;
28     private String JavaDoc uri = null;
29
30     public static RequestLine parseLine(final String JavaDoc l) throws HttpException
31     {
32         String JavaDoc method = null;
33         String JavaDoc uri = null;
34         String JavaDoc protocol = null;
35         try
36         {
37             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(l, " ");
38             method = st.nextToken();
39             uri = st.nextToken();
40             protocol = st.nextToken();
41         }
42         catch (NoSuchElementException JavaDoc e)
43         {
44             throw new ProtocolException("Invalid request line: " + l);
45         }
46         return new RequestLine(method, uri, protocol);
47     }
48
49     public RequestLine(final String JavaDoc method, final String JavaDoc uri, final HttpVersion httpversion)
50     {
51         super();
52         if (method == null)
53         {
54             throw new IllegalArgumentException JavaDoc("Method may not be null");
55         }
56         if (uri == null)
57         {
58             throw new IllegalArgumentException JavaDoc("URI may not be null");
59         }
60         if (httpversion == null)
61         {
62             throw new IllegalArgumentException JavaDoc("HTTP version may not be null");
63         }
64         this.method = method;
65         this.uri = uri;
66         this.httpversion = httpversion;
67     }
68
69     public RequestLine(final String JavaDoc method, final String JavaDoc uri, final String JavaDoc httpversion)
70         throws ProtocolException
71     {
72         this(method, uri, HttpVersion.parse(httpversion));
73     }
74
75     public String JavaDoc getMethod()
76     {
77         return this.method;
78     }
79
80     public HttpVersion getHttpVersion()
81     {
82         return this.httpversion;
83     }
84
85     public String JavaDoc getUri()
86     {
87         return this.uri;
88     }
89
90     public String JavaDoc toString()
91     {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
93         sb.append(this.method);
94         sb.append(" ");
95         sb.append(this.uri);
96         sb.append(" ");
97         sb.append(this.httpversion);
98         return sb.toString();
99     }
100 }
101
Popular Tags