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