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  import static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  /**
21   * The default [email protected] 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 [email protected] 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 [email protected] HttpHeaders}
54       * @param singleFieldHeaders [email protected] 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       * [email protected] 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 = checkNotNull(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 = checkNotNull(status, "status");
76      }
77  
78      @Override
79      @Deprecated
80      public HttpResponseStatus getStatus() {
81          return status();
82      }
83  
84      @Override
85      public HttpResponseStatus status() {
86          return status;
87      }
88  
89      @Override
90      public HttpResponse setStatus(HttpResponseStatus status) {
91          if (status == null) {
92              throw new NullPointerException("status");
93          }
94          this.status = status;
95          return this;
96      }
97  
98      @Override
99      public HttpResponse setProtocolVersion(HttpVersion version) {
100         super.setProtocolVersion(version);
101         return this;
102     }
103 
104     @Override
105     public String toString() {
106         return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
107     }
108 }