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 io.netty.handler.codec.http;
17  
18  /**
19   * The default {@link HttpRequest} implementation.
20   */
21  public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest {
22  
23      private HttpMethod method;
24      private String uri;
25  
26      /**
27       * Creates a new instance.
28       *
29       * @param httpVersion the HTTP version of the request
30       * @param method      the HTTP getMethod of the request
31       * @param uri         the URI or path of the request
32       */
33      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
34          this(httpVersion, method, uri, true);
35      }
36  
37      /**
38       * Creates a new instance.
39       *
40       * @param httpVersion       the HTTP version of the request
41       * @param method            the HTTP getMethod of the request
42       * @param uri               the URI or path of the request
43       * @param validateHeaders   validate the headers when adding them
44       */
45      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, boolean validateHeaders) {
46          super(httpVersion, validateHeaders);
47          if (method == null) {
48              throw new NullPointerException("method");
49          }
50          if (uri == null) {
51              throw new NullPointerException("uri");
52          }
53          this.method = method;
54          this.uri = uri;
55      }
56  
57      @Override
58      public HttpMethod getMethod() {
59          return method;
60      }
61  
62      @Override
63      public String getUri() {
64          return uri;
65      }
66  
67      @Override
68      public HttpRequest setMethod(HttpMethod method) {
69          if (method == null) {
70              throw new NullPointerException("method");
71          }
72          this.method = method;
73          return this;
74      }
75  
76      @Override
77      public HttpRequest setUri(String uri) {
78          if (uri == null) {
79              throw new NullPointerException("uri");
80          }
81          this.uri = uri;
82          return this;
83      }
84  
85      @Override
86      public HttpRequest setProtocolVersion(HttpVersion version) {
87          super.setProtocolVersion(version);
88          return this;
89      }
90  
91      @Override
92      public String toString() {
93          return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
94      }
95  }