-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
OutgoingMessageCapture.java
178 lines (162 loc) · 6.25 KB
/
OutgoingMessageCapture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.common;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.NullByteBufferPool;
import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
import org.eclipse.jetty.websocket.core.internal.messages.ByteBufferMessageSink;
import org.eclipse.jetty.websocket.core.internal.messages.MessageSink;
import org.eclipse.jetty.websocket.core.internal.messages.StringMessageSink;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OutgoingMessageCapture extends CoreSession.Empty implements CoreSession
{
private static final Logger LOG = LoggerFactory.getLogger(OutgoingMessageCapture.class);
public BlockingQueue<String> textMessages = new LinkedBlockingDeque<>();
public BlockingQueue<ByteBuffer> binaryMessages = new LinkedBlockingDeque<>();
public BlockingQueue<String> events = new LinkedBlockingDeque<>();
private final ByteBufferPool bufferPool = new NullByteBufferPool();
private final MethodHandle wholeTextHandle;
private final MethodHandle wholeBinaryHandle;
private MessageSink messageSink;
private long maxMessageSize = 2 * 1024 * 1024;
public OutgoingMessageCapture()
{
MethodHandles.Lookup lookup = JettyWebSocketFrameHandlerFactory.getApplicationMethodHandleLookup(this.getClass());
try
{
MethodHandle text = lookup.findVirtual(this.getClass(), "onWholeText", MethodType.methodType(Void.TYPE, String.class));
this.wholeTextHandle = text.bindTo(this);
MethodHandle binary = lookup.findVirtual(this.getClass(), "onWholeBinary", MethodType.methodType(Void.TYPE, ByteBuffer.class));
this.wholeBinaryHandle = binary.bindTo(this);
}
catch (NoSuchMethodException | IllegalAccessException e)
{
throw new IllegalStateException("Unable to setup OutgoingMessageCapture", e);
}
}
@Override
public void sendFrame(Frame frame, Callback callback, boolean batch)
{
switch (frame.getOpCode())
{
case OpCode.CLOSE:
{
CloseStatus closeStatus = new CloseStatus(frame.getPayload());
String event = String.format("CLOSE:%s:%s", CloseStatus.codeString(closeStatus.getCode()), closeStatus.getReason());
LOG.debug(event);
events.offer(event);
break;
}
case OpCode.PING:
{
String event = String.format("PING:%s", dataHint(frame.getPayload()));
LOG.debug(event);
events.offer(event);
break;
}
case OpCode.PONG:
{
String event = String.format("PONG:%s", dataHint(frame.getPayload()));
LOG.debug(event);
events.offer(event);
break;
}
case OpCode.TEXT:
{
String event = String.format("TEXT:fin=%b:len=%d", frame.isFin(), frame.getPayloadLength());
LOG.debug(event);
events.offer(event);
messageSink = new StringMessageSink(this, MethodHolder.from(wholeTextHandle));
break;
}
case OpCode.BINARY:
{
String event = String.format("BINARY:fin=%b:len=%d", frame.isFin(), frame.getPayloadLength());
LOG.debug(event);
events.offer(event);
messageSink = new ByteBufferMessageSink(this, MethodHolder.from(wholeBinaryHandle));
break;
}
case OpCode.CONTINUATION:
{
String event = String.format("CONTINUATION:fin=%b:len=%d", frame.isFin(), frame.getPayloadLength());
LOG.debug(event);
events.offer(event);
break;
}
}
if (OpCode.isDataFrame(frame.getOpCode()))
{
Frame copy = Frame.copy(frame);
messageSink.accept(copy, Callback.from(() -> {}, Throwable::printStackTrace));
if (frame.isFin())
messageSink = null;
}
callback.succeeded();
}
@Override
public ByteBufferPool getByteBufferPool()
{
return bufferPool;
}
@SuppressWarnings("unused")
public void onWholeText(String msg)
{
this.textMessages.offer(msg);
}
@SuppressWarnings("unused")
public void onWholeBinary(ByteBuffer buf)
{
ByteBuffer copy = null;
if (buf != null)
{
copy = ByteBuffer.allocate(buf.remaining());
copy.put(buf);
copy.flip();
}
this.binaryMessages.offer(copy);
}
private String dataHint(ByteBuffer payload)
{
if (payload == null)
return "<null>";
StringBuilder hint = new StringBuilder();
hint.append('[');
ByteBuffer sliced = payload.slice();
if (sliced.remaining() > 20)
{
sliced.limit(20);
hint.append(Hex.asHex(sliced));
hint.append("...");
}
else
{
hint.append(Hex.asHex(sliced));
}
hint.append(']');
return hint.toString();
}
}