View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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 org.jboss.netty.handler.codec.http2;
17  
18  import java.util.Queue;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.buffer.ChannelBuffers;
22  import org.jboss.netty.channel.ChannelHandlerContext;
23  import org.jboss.netty.channel.Channels;
24  import org.jboss.netty.channel.MessageEvent;
25  import org.jboss.netty.channel.SimpleChannelHandler;
26  import org.jboss.netty.handler.codec.embedder.EncoderEmbedder;
27  import org.jboss.netty.util.internal.QueueFactory;
28  
29  /**
30   * Encodes the content of the outbound {@link HttpResponse} and {@link HttpChunk}.
31   * The original content is replaced with the new content encoded by the
32   * {@link EncoderEmbedder}, which is created by {@link #newContentEncoder(String)}.
33   * Once encoding is finished, the value of the <tt>'Content-Encoding'</tt> header
34   * is set to the target content encoding, as returned by {@link #getTargetContentEncoding(String)}.
35   * Also, the <tt>'Content-Length'</tt> header is updated to the length of the
36   * encoded content.  If there is no supported encoding in the
37   * corresponding {@link HttpRequest}'s {@code "Accept-Encoding"} header,
38   * {@link #newContentEncoder(String)} should return {@code null} so that no
39   * encoding occurs (i.e. pass-through).
40   * <p>
41   * Please note that this is an abstract class.  You have to extend this class
42   * and implement {@link #newContentEncoder(String)} and {@link #getTargetContentEncoding(String)}
43   * properly to make this class functional.  For example, refer to the source
44   * code of {@link HttpContentCompressor}.
45   * <p>
46   * This handler must be placed after {@link HttpMessageEncoder} in the pipeline
47   * so that this handler can intercept HTTP responses before {@link HttpMessageEncoder}
48   * converts them into {@link ChannelBuffer}s.
49   *
50   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
51   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
52   * @version $Rev: 1107 $, $Date: 2012-04-15 19:00:57 +0200 (dim., 15 avr. 2012) $
53   */
54  public abstract class HttpContentEncoder extends SimpleChannelHandler {
55  
56      private final Queue<String> acceptEncodingQueue = QueueFactory.createQueue(String.class);
57      private volatile EncoderEmbedder<ChannelBuffer> encoder;
58  
59      /**
60       * Creates a new instance.
61       */
62      protected HttpContentEncoder() {
63          super();
64      }
65  
66      @Override
67      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
68              throws Exception {
69          Object msg = e.getMessage();
70          if (!(msg instanceof HttpMessage)) {
71              ctx.sendUpstream(e);
72              return;
73          }
74  
75          HttpMessage m = (HttpMessage) msg;
76          String acceptedEncoding = m.getHeader(HttpHeaders.Names.ACCEPT_ENCODING);
77          if (acceptedEncoding == null) {
78              acceptedEncoding = HttpHeaders.Values.IDENTITY;
79          }
80          boolean offered = acceptEncodingQueue.offer(acceptedEncoding);
81          assert offered;
82  
83          ctx.sendUpstream(e);
84      }
85  
86      @Override
87      public void writeRequested(ChannelHandlerContext ctx, MessageEvent e)
88              throws Exception {
89  
90          Object msg = e.getMessage();
91          if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().getCode() == 100) {
92              // 100-continue response must be passed through.
93              ctx.sendDownstream(e);
94          } else  if (msg instanceof HttpMessage) {
95              HttpMessage m = (HttpMessage) msg;
96  
97              encoder = null;
98  
99              String contentEncoding = m.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
100             if (contentEncoding != null &&
101                 !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) {
102                 // Content-Encoding is set already and it is not 'identity'.
103                 ctx.sendDownstream(e);
104             } else {
105                 // Determine the content encoding.
106                 String acceptEncoding = acceptEncodingQueue.poll();
107                 if (acceptEncoding == null) {
108                     throw new IllegalStateException("cannot send more responses than requests");
109                 }
110 
111                 boolean hasContent = m.isChunked() || m.getContent().readable();
112                 if (hasContent && (encoder = newContentEncoder(acceptEncoding)) != null) {
113                     // Encode the content and remove or replace the existing headers
114                     // so that the message looks like a decoded message.
115                     m.setHeader(
116                             HttpHeaders.Names.CONTENT_ENCODING,
117                             getTargetContentEncoding(acceptEncoding));
118 
119                     if (!m.isChunked()) {
120                         ChannelBuffer content = m.getContent();
121                         // Encode the content.
122                         content = ChannelBuffers.wrappedBuffer(
123                                 encode(content), finishEncode());
124 
125                         // Replace the content.
126                         m.setContent(content);
127                         if (m.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
128                             m.setHeader(
129                                     HttpHeaders.Names.CONTENT_LENGTH,
130                                     Integer.toString(content.readableBytes()));
131                         }
132                     }
133                 }
134 
135                 // Because HttpMessage is a mutable object, we can simply forward the write request.
136                 ctx.sendDownstream(e);
137             }
138         } else if (msg instanceof HttpChunk) {
139             HttpChunk c = (HttpChunk) msg;
140             ChannelBuffer content = c.getContent();
141 
142             // Encode the chunk if necessary.
143             if (encoder != null) {
144                 if (!c.isLast()) {
145                     content = encode(content);
146                     if (content.readable()) {
147                         c.setContent(content);
148                         ctx.sendDownstream(e);
149                     }
150                 } else {
151                     ChannelBuffer lastProduct = finishEncode();
152 
153                     // Generate an additional chunk if the decoder produced
154                     // the last product on closure,
155                     if (lastProduct.readable()) {
156                         Channels.write(
157                                 ctx, Channels.succeededFuture(e.getChannel()), new DefaultHttpChunk(lastProduct), e.getRemoteAddress());
158                     }
159 
160                     // Emit the last chunk.
161                     ctx.sendDownstream(e);
162                 }
163             } else {
164                 ctx.sendDownstream(e);
165             }
166         } else {
167             ctx.sendDownstream(e);
168         }
169     }
170 
171     /**
172      * Returns a new {@link EncoderEmbedder} that encodes the HTTP message
173      * content.
174      *
175      * @param acceptEncoding
176      *        the value of the {@code "Accept-Encoding"} header
177      *
178      * @return a new {@link EncoderEmbedder} if there is a supported encoding
179      *         in {@code acceptEncoding}.  {@code null} otherwise.
180      */
181     protected abstract EncoderEmbedder<ChannelBuffer> newContentEncoder(String acceptEncoding) throws Exception;
182 
183     /**
184      * Returns the expected content encoding of the encoded content.
185      *
186      * @param acceptEncoding the value of the {@code "Accept-Encoding"} header
187      * @return the expected content encoding of the new content
188      */
189     protected abstract String getTargetContentEncoding(String acceptEncoding) throws Exception;
190 
191     private ChannelBuffer encode(ChannelBuffer buf) {
192         encoder.offer(buf);
193         return ChannelBuffers.wrappedBuffer(encoder.pollAll(new ChannelBuffer[encoder.size()]));
194     }
195 
196     private ChannelBuffer finishEncode() {
197         ChannelBuffer result;
198         if (encoder.finish()) {
199             result = ChannelBuffers.wrappedBuffer(encoder.pollAll(new ChannelBuffer[encoder.size()]));
200         } else {
201             result = ChannelBuffers.EMPTY_BUFFER;
202         }
203         encoder = null;
204         return result;
205     }
206 }