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 io.netty.util.internal.ObjectUtil;
19  
20  import static io.netty.util.internal.ObjectUtil.checkNotNull;
21  
22  /**
23   * The default {@link HttpMessage} implementation.
24   */
25  public abstract class DefaultHttpMessage extends DefaultHttpObject implements HttpMessage {
26      private static final int HASH_CODE_PRIME = 31;
27      private HttpVersion version;
28      private final HttpHeaders headers;
29  
30      /**
31       * Creates a new instance.
32       */
33      protected DefaultHttpMessage(final HttpVersion version) {
34          this(version, true, false);
35      }
36  
37      /**
38       * Creates a new instance.
39       */
40      protected DefaultHttpMessage(final HttpVersion version, boolean validateHeaders, boolean singleFieldHeaders) {
41          this(version,
42                  singleFieldHeaders ? new CombinedHttpHeaders(validateHeaders)
43                                     : new DefaultHttpHeaders(validateHeaders));
44      }
45  
46      /**
47       * Creates a new instance.
48       */
49      protected DefaultHttpMessage(final HttpVersion version, HttpHeaders headers) {
50          this.version = checkNotNull(version, "version");
51          this.headers = checkNotNull(headers, "headers");
52      }
53  
54      @Override
55      public HttpHeaders headers() {
56          return headers;
57      }
58  
59      @Override
60      @Deprecated
61      public HttpVersion getProtocolVersion() {
62          return protocolVersion();
63      }
64  
65      @Override
66      public HttpVersion protocolVersion() {
67          return version;
68      }
69  
70      @Override
71      public int hashCode() {
72          int result = 1;
73          result = HASH_CODE_PRIME * result + headers.hashCode();
74          result = HASH_CODE_PRIME * result + version.hashCode();
75          result = HASH_CODE_PRIME * result + super.hashCode();
76          return result;
77      }
78  
79      @Override
80      public boolean equals(Object o) {
81          if (!(o instanceof DefaultHttpMessage)) {
82              return false;
83          }
84  
85          DefaultHttpMessage other = (DefaultHttpMessage) o;
86  
87          return headers().equals(other.headers()) &&
88                 protocolVersion().equals(other.protocolVersion()) &&
89                 super.equals(o);
90      }
91  
92      @Override
93      public HttpMessage setProtocolVersion(HttpVersion version) {
94          this.version = ObjectUtil.checkNotNull(version, "version");
95          return this;
96      }
97  }