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    *   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  /**
19   * The default {@link HttpResponse} implementation.
20   */
21  public class DefaultHttpResponse extends DefaultHttpMessage implements HttpResponse {
22      private static final int HASH_CODE_PRIME = 31;
23      private HttpResponseStatus status;
24  
25      /**
26       * Creates a new instance.
27       *
28       * @param version the HTTP version of this response
29       * @param status  the getStatus of this response
30       */
31      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status) {
32          this(version, status, true, false);
33      }
34  
35      /**
36       * Creates a new instance.
37       *
38       * @param version           the HTTP version of this response
39       * @param status            the getStatus of this response
40       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
41       */
42      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
43          this(version, status, validateHeaders, false);
44      }
45  
46      /**
47       * Creates a new instance.
48       *
49       * @param version           the HTTP version of this response
50       * @param status            the getStatus of this response
51       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
52       * @param singleHeaderFields  determines if HTTP headers with multiple values should be added as a single
53       *                            field or as multiple header fields.
54       */
55      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
56                                 boolean singleHeaderFields) {
57          super(version, validateHeaders, singleHeaderFields);
58          if (status == null) {
59              throw new NullPointerException("status");
60          }
61          this.status = status;
62      }
63  
64      @Override
65      public HttpResponseStatus status() {
66          return status;
67      }
68  
69      @Override
70      public HttpResponse setStatus(HttpResponseStatus status) {
71          if (status == null) {
72              throw new NullPointerException("status");
73          }
74          this.status = status;
75          return this;
76      }
77  
78      @Override
79      public HttpResponse setProtocolVersion(HttpVersion version) {
80          super.setProtocolVersion(version);
81          return this;
82      }
83  
84      @Override
85      public int hashCode() {
86          int result = 1;
87          result = HASH_CODE_PRIME * result + status.hashCode();
88          result = HASH_CODE_PRIME * result + super.hashCode();
89          return result;
90      }
91  
92      @Override
93      public boolean equals(Object o) {
94          if (!(o instanceof DefaultHttpResponse)) {
95              return false;
96          }
97  
98          DefaultHttpResponse other = (DefaultHttpResponse) o;
99  
100         return status().equals(other.status()) && super.equals(o);
101     }
102 
103     @Override
104     public String toString() {
105         return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
106     }
107 }