View Javadoc

1   /*
2    * Copyright 2010 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.rtsp;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.channel.Channel;
20  import org.jboss.netty.channel.ChannelHandlerContext;
21  import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
22  import org.jboss.netty.handler.codec.frame.TooLongFrameException;
23  import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
24  import org.jboss.netty.handler.codec.http.HttpMessage;
25  import org.jboss.netty.handler.codec.http.HttpMessageDecoder;
26  
27  /**
28   * Decodes {@link ChannelBuffer}s into RTSP messages represented in
29   * {@link HttpMessage}s.
30   * <p>
31   * <h3>Parameters that prevents excessive memory consumption</h3>
32   * <table border="1">
33   * <tr>
34   * <th>Name</th><th>Meaning</th>
35   * </tr>
36   * <tr>
37   * <td>{@code maxInitialLineLength}</td>
38   * <td>The maximum length of the initial line
39   *     (e.g. {@code "SETUP / RTSP/1.0"} or {@code "RTSP/1.0 200 OK"})
40   *     If the length of the initial line exceeds this value, a
41   *     {@link TooLongFrameException} will be raised.</td>
42   * </tr>
43   * <tr>
44   * <td>{@code maxHeaderSize}</td>
45   * <td>The maximum length of all headers.  If the sum of the length of each
46   *     header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
47   * </tr>
48   * <tr>
49   * <td>{@code maxContentLength}</td>
50   * <td>The maximum length of the content.  If the content length exceeds this
51   *     value, a {@link TooLongFrameException} will be raised.</td>
52   * </tr>
53   * </table>
54   *
55   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
56   * @author <a href="http://amitbhayani.blogspot.com/">Amit Bhayani</a>
57   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
58   * @version $Rev: 2282 $, $Date: 2010-05-19 16:51:38 +0900 (Wed, 19 May 2010) $
59   *
60   * @apiviz.landmark
61   */
62  public abstract class RtspMessageDecoder extends HttpMessageDecoder {
63  
64      private final DecoderEmbedder<HttpMessage> aggregator;
65  
66      /**
67       * Creates a new instance with the default
68       * {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
69       * {@code maxContentLength (8192)}.
70       */
71      protected RtspMessageDecoder() {
72          this(4096, 8192, 8192);
73      }
74  
75      /**
76       * Creates a new instance with the specified parameters.
77       */
78      protected RtspMessageDecoder(int maxInitialLineLength, int maxHeaderSize, int maxContentLength) {
79          super(maxInitialLineLength, maxHeaderSize, maxContentLength * 2);
80          aggregator = new DecoderEmbedder<HttpMessage>(new HttpChunkAggregator(maxContentLength));
81      }
82  
83      @Override
84      protected Object decode(ChannelHandlerContext ctx, Channel channel,
85              ChannelBuffer buffer, State state) throws Exception {
86          Object o = super.decode(ctx, channel, buffer, state);
87          if (o != null && aggregator.offer(o)) {
88              return aggregator.poll();
89          } else {
90              return null;
91          }
92      }
93  
94      @Override
95      protected boolean isContentAlwaysEmpty(HttpMessage msg) {
96          // Unlike HTTP, RTSP always assumes zero-length body if Content-Length
97          // header is absent.
98          boolean empty = super.isContentAlwaysEmpty(msg);
99          if (empty) {
100             return true;
101         }
102         if (!msg.containsHeader(RtspHeaders.Names.CONTENT_LENGTH)) {
103             return true;
104         }
105         return empty;
106     }
107 }