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 HttpResponse} implementation.
22   */
23  public class DefaultHttpResponse extends DefaultHttpMessage implements HttpResponse {
24  
25      private HttpResponseStatus status;
26  
27      /**
28       * Creates a new instance.
29       *
30       * @param version the HTTP version of this response
31       * @param status  the status of this response
32       */
33      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status) {
34          this(version, status, true, false);
35      }
36  
37      /**
38       * Creates a new instance.
39       *
40       * @param version           the HTTP version of this response
41       * @param status            the status of this response
42       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
43       */
44      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
45          this(version, status, validateHeaders, false);
46      }
47  
48      /**
49       * Creates a new instance.
50       *
51       * @param version           the HTTP version of this response
52       * @param status            the status of this response
53       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
54       * @param singleFieldHeaders {@code true} to check and enforce that headers with the same name are appended
55       * to the same entry and comma separated.
56       * See <a href="https://tools.ietf.org/html/rfc7230#section-3.2.2">RFC 7230, 3.2.2</a>.
57       * {@code false} to allow multiple header entries with the same name to
58       * coexist.
59       */
60      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
61                                 boolean singleFieldHeaders) {
62          super(version, validateHeaders, singleFieldHeaders);
63          this.status = requireNonNull(status, "status");
64      }
65  
66      /**
67       * Creates a new instance.
68       *
69       * @param version           the HTTP version of this response
70       * @param status            the status of this response
71       * @param headers           the headers for this HTTP Response
72       */
73      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, HttpHeaders headers) {
74          super(version, headers);
75          this.status = requireNonNull(status, "status");
76      }
77  
78      @Override
79      public HttpResponseStatus status() {
80          return status;
81      }
82  
83      @Override
84      public HttpResponse setStatus(HttpResponseStatus status) {
85          requireNonNull(status, "status");
86          this.status = status;
87          return this;
88      }
89  
90      @Override
91      public HttpResponse setProtocolVersion(HttpVersion version) {
92          super.setProtocolVersion(version);
93          return this;
94      }
95  
96      @Override
97      public String toString() {
98          return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
99      }
100 
101     @Override
102     public int hashCode() {
103         int result = 1;
104         result = 31 * result + status.hashCode();
105         result = 31 * result + super.hashCode();
106         return result;
107     }
108 
109     @Override
110     public boolean equals(Object o) {
111         if (!(o instanceof DefaultHttpResponse)) {
112             return false;
113         }
114 
115         DefaultHttpResponse other = (DefaultHttpResponse) o;
116 
117         return status.equals(other.status()) && super.equals(o);
118     }
119 }