View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.http;
17  
18  import org.jboss.netty.util.internal.StringUtil;
19  
20  /**
21   * The default {@link HttpRequest} implementation.
22   */
23  public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest {
24  
25      private HttpMethod method;
26      private String uri;
27  
28      /**
29       * Creates a new instance.
30       *
31       * @param httpVersion the HTTP version of the request
32       * @param method      the HTTP method of the request
33       * @param uri         the URI or path of the request
34       */
35      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
36          super(httpVersion);
37          setMethod(method);
38          setUri(uri);
39      }
40  
41      public HttpMethod getMethod() {
42          return method;
43      }
44  
45      public void setMethod(HttpMethod method) {
46          if (method == null) {
47              throw new NullPointerException("method");
48          }
49          this.method = method;
50      }
51  
52      public String getUri() {
53          return uri;
54      }
55  
56      public void setUri(String uri) {
57          if (uri == null) {
58              throw new NullPointerException("uri");
59          }
60          this.uri = uri;
61      }
62  
63      @Override
64      public String toString() {
65          StringBuilder buf = new StringBuilder();
66          buf.append(getClass().getSimpleName());
67          buf.append("(chunked: ");
68          buf.append(isChunked());
69          buf.append(')');
70          buf.append(StringUtil.NEWLINE);
71          buf.append(getMethod().toString());
72          buf.append(' ');
73          buf.append(getUri());
74          buf.append(' ');
75          buf.append(getProtocolVersion().getText());
76          buf.append(StringUtil.NEWLINE);
77          appendHeaders(buf);
78  
79          // Remove the last newline.
80          buf.setLength(buf.length() - StringUtil.NEWLINE.length());
81          return buf.toString();
82      }
83  }