View Javadoc
1   /*
2    * Copyright 2014 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    *   https://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.stomp;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandler;
20  import io.netty.channel.ChannelPipeline;
21  import io.netty.handler.codec.MessageAggregator;
22  import io.netty.handler.codec.TooLongFrameException;
23  
24  /**
25   * A {@link ChannelHandler} that aggregates an {@link StompHeadersSubframe}
26   * and its following {@link StompContentSubframe}s into a single {@link StompFrame}.
27   * It is useful when you don't want to take care of STOMP frames whose content is 'chunked'.  Insert this
28   * handler after {@link StompSubframeDecoder} in the {@link ChannelPipeline}:
29   */
30  public class StompSubframeAggregator
31          extends MessageAggregator<StompSubframe, StompHeadersSubframe, StompContentSubframe, StompFrame> {
32  
33      /**
34       * Creates a new instance.
35       *
36       * @param maxContentLength
37       *        the maximum length of the aggregated content.
38       *        If the length of the aggregated content exceeds this value,
39       *        a {@link TooLongFrameException} will be raised.
40       */
41      public StompSubframeAggregator(int maxContentLength) {
42          super(maxContentLength);
43      }
44  
45      @Override
46      protected boolean isStartMessage(StompSubframe msg) throws Exception {
47          return msg instanceof StompHeadersSubframe;
48      }
49  
50      @Override
51      protected boolean isContentMessage(StompSubframe msg) throws Exception {
52          return msg instanceof StompContentSubframe;
53      }
54  
55      @Override
56      protected boolean isLastContentMessage(StompContentSubframe msg) throws Exception {
57          return msg instanceof LastStompContentSubframe;
58      }
59  
60      @Override
61      protected boolean isAggregated(StompSubframe msg) throws Exception {
62          return msg instanceof StompFrame;
63      }
64  
65      @Override
66      protected boolean isContentLengthInvalid(StompHeadersSubframe start, int maxContentLength) {
67          return (int) Math.min(Integer.MAX_VALUE, start.headers().getLong(StompHeaders.CONTENT_LENGTH, -1)) >
68                       maxContentLength;
69      }
70  
71      @Override
72      protected Object newContinueResponse(StompHeadersSubframe start, int maxContentLength, ChannelPipeline pipeline) {
73          return null;
74      }
75  
76      @Override
77      protected boolean closeAfterContinueResponse(Object msg) throws Exception {
78          throw new UnsupportedOperationException();
79      }
80  
81      @Override
82      protected boolean ignoreContentAfterContinueResponse(Object msg) throws Exception {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      protected StompFrame beginAggregation(StompHeadersSubframe start, ByteBuf content) throws Exception {
88          StompFrame ret = new DefaultStompFrame(start.command(), content);
89          ret.headers().set(start.headers());
90          return ret;
91      }
92  }