View Javadoc

1   /*
2    * Copyright 2013 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.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  
21  
22  /**
23   * Default implementation of a {@link FullHttpResponse}.
24   */
25  public class DefaultFullHttpResponse extends DefaultHttpResponse implements FullHttpResponse {
26  
27      private final ByteBuf content;
28      private final HttpHeaders trailingHeaders;
29      private final boolean validateHeaders;
30  
31      public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status) {
32          this(version, status, Unpooled.buffer(0));
33      }
34  
35      public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content) {
36          this(version, status, content, true);
37      }
38  
39      public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
40                                     ByteBuf content, boolean validateHeaders) {
41          super(version, status, validateHeaders);
42          if (content == null) {
43              throw new NullPointerException("content");
44          }
45          this.content = content;
46          trailingHeaders = new DefaultHttpHeaders(validateHeaders);
47          this.validateHeaders = validateHeaders;
48      }
49  
50      @Override
51      public HttpHeaders trailingHeaders() {
52          return trailingHeaders;
53      }
54  
55      @Override
56      public ByteBuf content() {
57          return content;
58      }
59  
60      @Override
61      public int refCnt() {
62          return content.refCnt();
63      }
64  
65      @Override
66      public FullHttpResponse retain() {
67          content.retain();
68          return this;
69      }
70  
71      @Override
72      public FullHttpResponse retain(int increment) {
73          content.retain(increment);
74          return this;
75      }
76  
77      @Override
78      public boolean release() {
79          return content.release();
80      }
81  
82      @Override
83      public boolean release(int decrement) {
84          return content.release(decrement);
85      }
86  
87      @Override
88      public FullHttpResponse setProtocolVersion(HttpVersion version) {
89          super.setProtocolVersion(version);
90          return this;
91      }
92  
93      @Override
94      public FullHttpResponse setStatus(HttpResponseStatus status) {
95          super.setStatus(status);
96          return this;
97      }
98  
99      @Override
100     public FullHttpResponse copy() {
101         DefaultFullHttpResponse copy = new DefaultFullHttpResponse(
102                 getProtocolVersion(), getStatus(), content().copy(), validateHeaders);
103         copy.headers().set(headers());
104         copy.trailingHeaders().set(trailingHeaders());
105         return copy;
106     }
107 
108     @Override
109     public FullHttpResponse duplicate() {
110         DefaultFullHttpResponse duplicate = new DefaultFullHttpResponse(getProtocolVersion(), getStatus(),
111                 content().duplicate(), validateHeaders);
112         duplicate.headers().set(headers());
113         duplicate.trailingHeaders().set(trailingHeaders());
114         return duplicate;
115     }
116 
117     @Override
118     public String toString() {
119         return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
120     }
121 }