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.util.internal.ObjectUtil.checkNotNull;
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, DefaultHttpHeadersFactory.headersFactory());
33      }
34  
35      /**
36       * Creates a new instance.
37       * <p>
38       * @deprecated Use the {@link #DefaultHttpMessage(HttpVersion, HttpHeadersFactory)} constructor instead,
39       * ideally using the {@link DefaultHttpHeadersFactory#headersFactory()},
40       * or a factory that otherwise has validation enabled.
41       */
42      @Deprecated
43      protected DefaultHttpMessage(final HttpVersion version, boolean validateHeaders, boolean singleFieldHeaders) {
44          this(version, DefaultHttpHeadersFactory.headersFactory()
45                  .withValidation(validateHeaders)
46                  .withCombiningHeaders(singleFieldHeaders));
47      }
48  
49      /**
50       * Creates a new instance.
51       */
52      protected DefaultHttpMessage(HttpVersion version, HttpHeadersFactory headersFactory) {
53          this(version, headersFactory.newHeaders());
54      }
55  
56      /**
57       * Creates a new instance.
58       */
59      protected DefaultHttpMessage(final HttpVersion version, HttpHeaders headers) {
60          this.version = checkNotNull(version, "version");
61          this.headers = checkNotNull(headers, "headers");
62      }
63  
64      @Override
65      public HttpHeaders headers() {
66          return headers;
67      }
68  
69      @Override
70      @Deprecated
71      public HttpVersion getProtocolVersion() {
72          return protocolVersion();
73      }
74  
75      @Override
76      public HttpVersion protocolVersion() {
77          return version;
78      }
79  
80      @Override
81      public int hashCode() {
82          int result = 1;
83          result = HASH_CODE_PRIME * result + headers.hashCode();
84          result = HASH_CODE_PRIME * result + version.hashCode();
85          result = HASH_CODE_PRIME * result + super.hashCode();
86          return result;
87      }
88  
89      @Override
90      public boolean equals(Object o) {
91          if (!(o instanceof DefaultHttpMessage)) {
92              return false;
93          }
94  
95          DefaultHttpMessage other = (DefaultHttpMessage) o;
96  
97          return headers().equals(other.headers()) &&
98                 protocolVersion().equals(other.protocolVersion()) &&
99                 super.equals(o);
100     }
101 
102     @Override
103     public HttpMessage setProtocolVersion(HttpVersion version) {
104         this.version = checkNotNull(version, "version");
105         return this;
106     }
107 }