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