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