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.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.MessageAggregator;
20  import io.netty.handler.codec.TooLongFrameException;
21  
22  /**
23   * Handler that aggregate fragmented WebSocketFrame's.
24   *
25   * Be aware if PING/PONG/CLOSE frames are send in the middle of a fragmented {@link WebSocketFrame} they will
26   * just get forwarded to the next handler in the pipeline.
27   */
28  public class WebSocketFrameAggregator
29          extends MessageAggregator<WebSocketFrame, WebSocketFrame, ContinuationWebSocketFrame, WebSocketFrame> {
30  
31      /**
32       * Creates a new instance
33       *
34       * @param maxContentLength If the size of the aggregated frame exceeds this value,
35       *                         a {@link TooLongFrameException} is thrown.
36       */
37      public WebSocketFrameAggregator(int maxContentLength) {
38          super(maxContentLength);
39      }
40  
41      @Override
42      protected boolean isStartMessage(WebSocketFrame msg) throws Exception {
43          return msg instanceof TextWebSocketFrame || msg instanceof BinaryWebSocketFrame;
44      }
45  
46      @Override
47      protected boolean isContentMessage(WebSocketFrame msg) throws Exception {
48          return msg instanceof ContinuationWebSocketFrame;
49      }
50  
51      @Override
52      protected boolean isLastContentMessage(ContinuationWebSocketFrame msg) throws Exception {
53          return isContentMessage(msg) && msg.isFinalFragment();
54      }
55  
56      @Override
57      protected boolean isAggregated(WebSocketFrame msg) throws Exception {
58          if (msg.isFinalFragment()) {
59              return !isContentMessage(msg);
60          }
61  
62          return !isStartMessage(msg) && !isContentMessage(msg);
63      }
64  
65      @Override
66      protected boolean hasContentLength(WebSocketFrame start) throws Exception {
67          return false;
68      }
69  
70      @Override
71      protected long contentLength(WebSocketFrame start) throws Exception {
72          throw new UnsupportedOperationException();
73      }
74  
75      @Override
76      protected Object newContinueResponse(WebSocketFrame start) throws Exception {
77          return null;
78      }
79  
80      @Override
81      protected WebSocketFrame beginAggregation(WebSocketFrame start, ByteBuf content) throws Exception {
82          if (start instanceof TextWebSocketFrame) {
83              return new TextWebSocketFrame(true, start.rsv(), content);
84          }
85  
86          if (start instanceof BinaryWebSocketFrame) {
87              return new BinaryWebSocketFrame(true, start.rsv(), content);
88          }
89  
90          // Should not reach here.
91          throw new Error();
92      }
93  }