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 io.netty.util.internal.ObjectUtil;
19
20 import static io.netty.util.internal.ObjectUtil.checkNotNull;
21
22 /**
23  * The default {@link HttpRequest} implementation.
24  */

25 public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest {
26     private static final int HASH_CODE_PRIME = 31;
27     private HttpMethod method;
28     private String uri;
29
30     /**
31      * Creates a new instance.
32      *
33      * @param httpVersion the HTTP version of the request
34      * @param method      the HTTP method of the request
35      * @param uri         the URI or path of the request
36      */

37     public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
38         this(httpVersion, method, uri, true);
39     }
40
41     /**
42      * Creates a new instance.
43      *
44      * @param httpVersion       the HTTP version of the request
45      * @param method            the HTTP method of the request
46      * @param uri               the URI or path of the request
47      * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
48      */

49     public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, boolean validateHeaders) {
50         super(httpVersion, validateHeaders, false);
51         this.method = checkNotNull(method, "method");
52         this.uri = checkNotNull(uri, "uri");
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 headers           the Headers for this Request
62      */

63     public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, HttpHeaders headers) {
64         super(httpVersion, headers);
65         this.method = checkNotNull(method, "method");
66         this.uri = checkNotNull(uri, "uri");
67     }
68
69     @Override
70     @Deprecated
71     public HttpMethod getMethod() {
72         return method();
73     }
74
75     @Override
76     public HttpMethod method() {
77         return method;
78     }
79
80     @Override
81     @Deprecated
82     public String getUri() {
83         return uri();
84     }
85
86     @Override
87     public String uri() {
88         return uri;
89     }
90
91     @Override
92     public HttpRequest setMethod(HttpMethod method) {
93         this.method = ObjectUtil.checkNotNull(method, "method");
94         return this;
95     }
96
97     @Override
98     public HttpRequest setUri(String uri) {
99         this.uri = ObjectUtil.checkNotNull(uri, "uri");
100         return this;
101     }
102
103     @Override
104     public HttpRequest setProtocolVersion(HttpVersion version) {
105         super.setProtocolVersion(version);
106         return this;
107     }
108
109     @Override
110     public int hashCode() {
111         int result = 1;
112         result = HASH_CODE_PRIME * result + method.hashCode();
113         result = HASH_CODE_PRIME * result + uri.hashCode();
114         result = HASH_CODE_PRIME * result + super.hashCode();
115         return result;
116     }
117
118     @Override
119     public boolean equals(Object o) {
120         if (!(o instanceof DefaultHttpRequest)) {
121             return false;
122         }
123
124         DefaultHttpRequest other = (DefaultHttpRequest) o;
125
126         return method().equals(other.method()) &&
127                uri().equalsIgnoreCase(other.uri()) &&
128                super.equals(o);
129     }
130
131     @Override
132     public String toString() {
133         return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
134     }
135 }
136