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