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.netty5.handler.codec.http;
17  
18  import static java.util.Objects.requireNonNull;
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 = requireNonNull(method, "method");
50          this.uri = requireNonNull(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 = requireNonNull(method, "method");
64          this.uri = requireNonNull(uri, "uri");
65      }
66  
67      @Override
68      public HttpMethod method() {
69          return method;
70      }
71  
72      @Override
73      public String uri() {
74          return uri;
75      }
76  
77      @Override
78      public HttpRequest setMethod(HttpMethod method) {
79          requireNonNull(method, "method");
80          this.method = method;
81          return this;
82      }
83  
84      @Override
85      public HttpRequest setUri(String uri) {
86          requireNonNull(uri, "uri");
87          this.uri = uri;
88          return this;
89      }
90  
91      @Override
92      public HttpRequest setProtocolVersion(HttpVersion version) {
93          super.setProtocolVersion(version);
94          return this;
95      }
96  
97      @Override
98      public int hashCode() {
99          int result = 1;
100         result = HASH_CODE_PRIME * result + method.hashCode();
101         result = HASH_CODE_PRIME * result + uri.hashCode();
102         result = HASH_CODE_PRIME * result + super.hashCode();
103         return result;
104     }
105 
106     @Override
107     public boolean equals(Object o) {
108         if (!(o instanceof DefaultHttpRequest)) {
109             return false;
110         }
111 
112         DefaultHttpRequest other = (DefaultHttpRequest) o;
113 
114         return method().equals(other.method()) &&
115                uri().equalsIgnoreCase(other.uri()) &&
116                super.equals(o);
117     }
118 
119     @Override
120     public String toString() {
121         return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
122     }
123 }