View Javadoc

1   /*
2    * $Id: RequestLine.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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 CPAL v1.0
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 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   * 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 method = null;
28      private String uri = null;
29  
30      public static RequestLine parseLine(final String l) throws HttpException
31      {
32          String method = null;
33          String uri = null;
34          String protocol = null;
35          try
36          {
37              if(l==null)
38              {
39                  System.out.println("");
40              }
41              StringTokenizer st = new StringTokenizer(l, " ");
42              method = st.nextToken();
43              uri = st.nextToken();
44              protocol = st.nextToken();
45          }
46          catch (NoSuchElementException e)
47          {
48              throw new ProtocolException("Invalid request line: " + l);
49          }
50          return new RequestLine(method, uri, protocol);
51      }
52  
53      public RequestLine(final String method, final String uri, final HttpVersion httpversion)
54      {
55          super();
56          if (method == null)
57          {
58              throw new IllegalArgumentException("Method may not be null");
59          }
60          if (uri == null)
61          {
62              throw new IllegalArgumentException("URI may not be null");
63          }
64          if (httpversion == null)
65          {
66              throw new IllegalArgumentException("HTTP version may not be null");
67          }
68          this.method = method;
69          this.uri = uri;
70          this.httpversion = httpversion;
71      }
72  
73      public RequestLine(final String method, final String uri, final String httpversion)
74          throws ProtocolException
75      {
76          this(method, uri, HttpVersion.parse(httpversion));
77      }
78  
79      public String getMethod()
80      {
81          return this.method;
82      }
83  
84      public HttpVersion getHttpVersion()
85      {
86          return this.httpversion;
87      }
88  
89      public String getUri()
90      {
91          return this.uri;
92      }
93  
94      public String toString()
95      {
96          StringBuffer sb = new StringBuffer(64);
97          sb.append(this.method);
98          sb.append(" ");
99          sb.append(this.uri);
100         sb.append(" ");
101         sb.append(this.httpversion);
102         return sb.toString();
103     }
104 }