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