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 static final int HASH_CODE_PRIME = 31;
26      private final ByteBuf content;
27      private final HttpHeaders trailingHeader;
28      private final boolean validateHeaders;
29  
30      public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
31          this(httpVersion, method, uri, Unpooled.buffer(0));
32      }
33  
34      public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, ByteBuf content) {
35          this(httpVersion, method, uri, content, true);
36      }
37  
38      public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, boolean validateHeaders) {
39          this(httpVersion, method, uri, Unpooled.buffer(0), true);
40      }
41  
42      public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri,
43                                    ByteBuf content, boolean validateHeaders) {
44          super(httpVersion, method, uri, validateHeaders);
45          if (content == null) {
46              throw new NullPointerException("content");
47          }
48          this.content = content;
49          trailingHeader = new DefaultHttpHeaders(validateHeaders);
50          this.validateHeaders = validateHeaders;
51      }
52  
53      @Override
54      public HttpHeaders trailingHeaders() {
55          return trailingHeader;
56      }
57  
58      @Override
59      public ByteBuf content() {
60          return content;
61      }
62  
63      @Override
64      public int refCnt() {
65          return content.refCnt();
66      }
67  
68      @Override
69      public FullHttpRequest retain() {
70          content.retain();
71          return this;
72      }
73  
74      @Override
75      public FullHttpRequest retain(int increment) {
76          content.retain(increment);
77          return this;
78      }
79  
80      @Override
81      public FullHttpRequest touch() {
82          content.touch();
83          return this;
84      }
85  
86      @Override
87      public FullHttpRequest touch(Object hint) {
88          content.touch(hint);
89          return this;
90      }
91  
92      @Override
93      public boolean release() {
94          return content.release();
95      }
96  
97      @Override
98      public boolean release(int decrement) {
99          return content.release(decrement);
100     }
101 
102     @Override
103     public FullHttpRequest setProtocolVersion(HttpVersion version) {
104         super.setProtocolVersion(version);
105         return this;
106     }
107 
108     @Override
109     public FullHttpRequest setMethod(HttpMethod method) {
110         super.setMethod(method);
111         return this;
112     }
113 
114     @Override
115     public FullHttpRequest setUri(String uri) {
116         super.setUri(uri);
117         return this;
118     }
119 
120     /**
121      * Copy this object
122      *
123      * @param copyContent
124      * <ul>
125      * <li>{@code true} if this object's {@link #content()} should be used to copy.</li>
126      * <li>{@code false} if {@code newContent} should be used instead.</li>
127      * </ul>
128      * @param newContent
129      * <ul>
130      * <li>if {@code copyContent} is false then this will be used in the copy's content.</li>
131      * <li>if {@code null} then a default buffer of 0 size will be selected</li>
132      * </ul>
133      * @return A copy of this object
134      */
135     private FullHttpRequest copy(boolean copyContent, ByteBuf newContent) {
136         DefaultFullHttpRequest copy = new DefaultFullHttpRequest(
137                 protocolVersion(), method(), uri(),
138                 copyContent ? content().copy() :
139                     newContent == null ? Unpooled.buffer(0) : newContent);
140         copy.headers().set(headers());
141         copy.trailingHeaders().set(trailingHeaders());
142         return copy;
143     }
144 
145     @Override
146     public FullHttpRequest copy(ByteBuf newContent) {
147         return copy(false, newContent);
148     }
149 
150     @Override
151     public FullHttpRequest copy() {
152         return copy(true, null);
153     }
154 
155     @Override
156     public FullHttpRequest duplicate() {
157         DefaultFullHttpRequest duplicate = new DefaultFullHttpRequest(
158                 protocolVersion(), method(), uri(), content().duplicate(), validateHeaders);
159         duplicate.headers().set(headers());
160         duplicate.trailingHeaders().set(trailingHeaders());
161         return duplicate;
162     }
163 
164     @Override
165     public int hashCode() {
166         int result = 1;
167         result = HASH_CODE_PRIME * result + content().hashCode();
168         result = HASH_CODE_PRIME * result + trailingHeaders().hashCode();
169         result = HASH_CODE_PRIME * result + super.hashCode();
170         return result;
171     }
172 
173     @Override
174     public boolean equals(Object o) {
175         if (!(o instanceof DefaultFullHttpRequest)) {
176             return false;
177         }
178 
179         DefaultFullHttpRequest other = (DefaultFullHttpRequest) o;
180 
181         return super.equals(other) &&
182                content().equals(other.content()) &&
183                trailingHeaders().equals(other.trailingHeaders());
184     }
185 
186     @Override
187     public String toString() {
188         return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
189     }
190 }