View Javadoc

1   /*
2    * $Id: RequestLine.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.http;
12  
13  import org.mule.transport.http.i18n.HttpMessages;
14  
15  import java.util.NoSuchElementException;
16  import java.util.StringTokenizer;
17  
18  import org.apache.commons.httpclient.HttpException;
19  import org.apache.commons.httpclient.HttpVersion;
20  import org.apache.commons.httpclient.ProtocolException;
21  
22  /**
23   * Defines a HTTP request-line, consisting of method name, URI and protocol.
24   */
25  public class RequestLine
26  {
27  
28      private HttpVersion httpversion = null;
29      private String method = null;
30      private String uri = null;
31  
32      public static RequestLine parseLine(final String l) throws HttpException
33      {
34          String method;
35          String uri;
36          String protocol;
37          try
38          {
39              if (l==null)
40              {
41                  throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage());
42              }
43              StringTokenizer st = new StringTokenizer(l, " ");
44              method = st.nextToken();
45              uri = st.nextToken();
46              protocol = st.nextToken();
47          }
48          catch (NoSuchElementException e)
49          {
50              throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage());
51          }
52          return new RequestLine(method, uri, protocol);
53      }
54  
55      public RequestLine(final String method, final String uri, final HttpVersion httpversion)
56      {
57          super();
58          if (method == null)
59          {
60              throw new IllegalArgumentException("Method may not be null");
61          }
62          if (uri == null)
63          {
64              throw new IllegalArgumentException("URI may not be null");
65          }
66          if (httpversion == null)
67          {
68              throw new IllegalArgumentException("HTTP version may not be null");
69          }
70          this.method = method;
71          this.uri = uri;
72          this.httpversion = httpversion;
73      }
74  
75      public RequestLine(final String method, final String uri, final String httpversion)
76          throws ProtocolException
77      {
78          this(method, uri, HttpVersion.parse(httpversion));
79      }
80  
81      public String getMethod()
82      {
83          return this.method;
84      }
85  
86      public HttpVersion getHttpVersion()
87      {
88          return this.httpversion;
89      }
90  
91      public String getUri()
92      {
93          return this.uri;
94      }
95  
96      public String toString()
97      {
98          StringBuffer sb = new StringBuffer(64);
99          sb.append(this.method);
100         sb.append(" ");
101         sb.append(this.uri);
102         sb.append(" ");
103         sb.append(this.httpversion);
104         return sb.toString();
105     }
106 }