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  
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);
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 headers when adding them
41       */
42      public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
43          super(version, validateHeaders);
44          if (status == null) {
45              throw new NullPointerException("status");
46          }
47          this.status = status;
48      }
49  
50      @Override
51      public HttpResponseStatus getStatus() {
52          return status;
53      }
54  
55      @Override
56      public HttpResponse setStatus(HttpResponseStatus status) {
57          if (status == null) {
58              throw new NullPointerException("status");
59          }
60          this.status = status;
61          return this;
62      }
63  
64      @Override
65      public HttpResponse setProtocolVersion(HttpVersion version) {
66          super.setProtocolVersion(version);
67          return this;
68      }
69  
70      @Override
71      public String toString() {
72          return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
73      }
74  }