Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/RequestLine.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/RequestLine.java $
3    * $Revision: 390865 $
4    * $Date: 2006-04-02 19:23:01 +0200 (Sun, 02 Apr 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http;
31  
32  import org.apache.http.io.CharArrayBuffer;
33  import org.apache.http.protocol.HTTP;
34  
35  /**
36   * The first line of an {@link HttpRequest HttpRequest}.
37   * It contains the method, URI, and HTTP version of the request.
38   * For details, see RFC 2616.
39   *
40   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
41   *
42   * @version $Revision: 390865 $
43   * 
44   * @since 4.0
45   */
46  public class RequestLine {
47  
48      private final HttpVersion httpversion;
49      private final String method;
50      private final String uri;
51  
52      public RequestLine(final String method, final String uri, final HttpVersion httpversion) {
53            super();
54            if (method == null) {
55                throw new IllegalArgumentException("Method may not be null");
56            }
57            if (uri == null) {
58              throw new IllegalArgumentException("URI may not be null");
59            }
60            if (httpversion == null) {
61              throw new IllegalArgumentException("HTTP version may not be null");
62            }
63            this.method = method;
64          this.uri = uri;
65          this.httpversion = httpversion;
66      }
67  
68      public String getMethod() {
69          return this.method;
70      }
71  
72      public HttpVersion getHttpVersion() {
73          return this.httpversion;
74      }
75  
76      public String getUri() {
77          return this.uri;
78      }
79  
80      public String toString() {
81          CharArrayBuffer buffer = new CharArrayBuffer(64);
82          buffer.append(this.method);
83          buffer.append(' ');
84          buffer.append(this.uri);
85          buffer.append(' ');
86          buffer.append(this.httpversion);
87          return buffer.toString();
88      }
89      
90      public static RequestLine parse(
91              final CharArrayBuffer buffer, final int indexFrom, final int indexTo) 
92              throws ProtocolException {
93          if (buffer == null) {
94              throw new IllegalArgumentException("Char array buffer may not be null");
95          }
96          if (indexFrom < 0) {
97              throw new IndexOutOfBoundsException();
98          }
99          if (indexTo > buffer.length()) {
100             throw new IndexOutOfBoundsException();
101         }
102         if (indexFrom > indexTo) {
103             throw new IndexOutOfBoundsException();
104         }
105         try {
106             int i = indexFrom;
107             while (HTTP.isWhitespace(buffer.charAt(i))) {
108                 i++;
109             }
110             int blank = buffer.indexOf(' ', i, indexTo);
111             if (blank < 0) {
112                 throw new ProtocolException("Invalid request line: " + 
113                         buffer.substring(indexFrom, indexTo));
114             }
115             String method = buffer.substringTrimmed(i, blank);
116             i = blank;
117             while (HTTP.isWhitespace(buffer.charAt(i))) {
118                 i++;
119             }
120             blank = buffer.indexOf(' ', i, indexTo);
121             if (blank < 0) {
122                 throw new ProtocolException("Invalid request line: " + 
123                         buffer.substring(indexFrom, indexTo));
124             }
125             String uri = buffer.substringTrimmed(i, blank);
126             HttpVersion ver = HttpVersion.parse(buffer, blank, indexTo);
127             return new RequestLine(method, uri, ver);
128         } catch (IndexOutOfBoundsException e) {
129             throw new ProtocolException("Invalid request line: " + 
130                     buffer.substring(indexFrom, indexTo)); 
131         }
132     }
133 
134     public static final RequestLine parse(final String s)
135             throws ProtocolException {
136         if (s == null) {
137             throw new IllegalArgumentException("String may not be null");
138         }
139         CharArrayBuffer buffer = new CharArrayBuffer(s.length()); 
140         buffer.append(s);
141         return parse(buffer, 0, buffer.length());
142     }
143     
144     public static void format(final CharArrayBuffer buffer, final RequestLine requestline) {
145         if (buffer == null) {
146             throw new IllegalArgumentException("String buffer may not be null");
147         }
148         if (requestline == null) {
149             throw new IllegalArgumentException("Request line may not be null");
150         }
151         buffer.append(requestline.getMethod());
152         buffer.append(' ');
153         buffer.append(requestline.getUri());
154         buffer.append(' ');
155         HttpVersion.format(buffer, requestline.getHttpVersion());
156     }
157  
158     public static String format(final RequestLine requestline) {
159         CharArrayBuffer buffer = new CharArrayBuffer(64);
160         format(buffer, requestline);
161         return buffer.toString();
162     }
163     
164 }