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