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  import static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  /**
21   * The default {@link HttpRequest} implementation.
22   */
23  public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest {
24      private static final int HASH_CODE_PRIME = 31;
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          this(httpVersion, method, uri, true);
37      }
38  
39      /**
40       * Creates a new instance.
41       *
42       * @param httpVersion       the HTTP version of the request
43       * @param method            the HTTP method of the request
44       * @param uri               the URI or path of the request
45       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
46       */
47      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, boolean validateHeaders) {
48          super(httpVersion, validateHeaders, false);
49          this.method = checkNotNull(method, "method");
50          this.uri = checkNotNull(uri, "uri");
51      }
52  
53      /**
54       * Creates a new instance.
55       *
56       * @param httpVersion       the HTTP version of the request
57       * @param method            the HTTP method of the request
58       * @param uri               the URI or path of the request
59       * @param headers           the Headers for this Request
60       */
61      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, HttpHeaders headers) {
62          super(httpVersion, headers);
63          this.method = checkNotNull(method, "method");
64          this.uri = checkNotNull(uri, "uri");
65      }
66  
67      @Override
68      @Deprecated
69      public HttpMethod getMethod() {
70          return method();
71      }
72  
73      @Override
74      public HttpMethod method() {
75          return method;
76      }
77  
78      @Override
79      @Deprecated
80      public String getUri() {
81          return uri();
82      }
83  
84      @Override
85      public String uri() {
86          return uri;
87      }
88  
89      @Override
90      public HttpRequest setMethod(HttpMethod method) {
91          if (method == null) {
92              throw new NullPointerException("method");
93          }
94          this.method = method;
95          return this;
96      }
97  
98      @Override
99      public HttpRequest setUri(String uri) {
100         if (uri == null) {
101             throw new NullPointerException("uri");
102         }
103         this.uri = uri;
104         return this;
105     }
106 
107     @Override
108     public HttpRequest setProtocolVersion(HttpVersion version) {
109         super.setProtocolVersion(version);
110         return this;
111     }
112 
113     @Override
114     public int hashCode() {
115         int result = 1;
116         result = HASH_CODE_PRIME * result + method.hashCode();
117         result = HASH_CODE_PRIME * result + uri.hashCode();
118         result = HASH_CODE_PRIME * result + super.hashCode();
119         return result;
120     }
121 
122     @Override
123     public boolean equals(Object o) {
124         if (!(o instanceof DefaultHttpRequest)) {
125             return false;
126         }
127 
128         DefaultHttpRequest other = (DefaultHttpRequest) o;
129 
130         return method().equals(other.method()) &&
131                uri().equalsIgnoreCase(other.uri()) &&
132                super.equals(o);
133     }
134 
135     @Override
136     public String toString() {
137         return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
138     }
139 }