1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28
29
30
31 public class WebSocketFrameAggregator
32 extends MessageAggregator<WebSocketFrame, WebSocketFrame, ContinuationWebSocketFrame, WebSocketFrame> {
33
34
35
36
37
38
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
99 throw new Error("Unexpected websocket frame type: " + className(start));
100 }
101 }