use of org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper in project jetty.project by eclipse.
the class JsrEndpointEventDriver method onTextFrame.
@Override
public void onTextFrame(ByteBuffer buffer, boolean fin) throws IOException {
if (activeMessage == null) {
final MessageHandlerWrapper wrapper = jsrsession.getMessageHandlerWrapper(MessageType.TEXT);
if (wrapper == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No TEXT MessageHandler declared");
}
return;
}
if (wrapper.wantsPartialMessages()) {
activeMessage = new TextPartialMessage(wrapper);
} else if (wrapper.wantsStreams()) {
final MessageReader stream = new MessageReader(new MessageInputStream());
activeMessage = stream;
dispatch(new Runnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
MessageHandler.Whole<Reader> handler = (Whole<Reader>) wrapper.getHandler();
handler.onMessage(stream);
}
});
} else {
activeMessage = new TextWholeMessage(this, wrapper);
}
}
activeMessage.appendFrame(buffer, fin);
if (fin) {
activeMessage.messageComplete();
activeMessage = null;
}
}
use of org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper in project jetty.project by eclipse.
the class JsrSessionTest method testMessageHandlerBinary.
@Test
public void testMessageHandlerBinary() throws DeploymentException {
session.addMessageHandler(new ByteBufferPartialHandler());
MessageHandlerWrapper wrapper = session.getMessageHandlerWrapper(MessageType.BINARY);
Assert.assertThat("Binary Handler", wrapper.getHandler(), instanceOf(ByteBufferPartialHandler.class));
Assert.assertEquals("Message Class", wrapper.getMetadata().getMessageClass(), ByteBuffer.class);
}
use of org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper in project jetty.project by eclipse.
the class JsrSessionTest method testMessageHandlerBoth.
@Test
public void testMessageHandlerBoth() throws DeploymentException {
session.addMessageHandler(new StringWholeHandler());
session.addMessageHandler(new ByteArrayWholeHandler());
MessageHandlerWrapper wrapper = session.getMessageHandlerWrapper(MessageType.TEXT);
Assert.assertThat("Text Handler", wrapper.getHandler(), instanceOf(StringWholeHandler.class));
Assert.assertEquals("Message Class", wrapper.getMetadata().getMessageClass(), String.class);
wrapper = session.getMessageHandlerWrapper(MessageType.BINARY);
Assert.assertThat("Binary Handler", wrapper.getHandler(), instanceOf(ByteArrayWholeHandler.class));
Assert.assertEquals("Message Class", wrapper.getMetadata().getMessageClass(), byte[].class);
}
use of org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper in project jetty.project by eclipse.
the class JsrSession method addMessageHandler.
@Override
public void addMessageHandler(MessageHandler handler) throws IllegalStateException {
Objects.requireNonNull(handler, "MessageHandler cannot be null");
synchronized (wrappers) {
for (MessageHandlerMetadata metadata : messageHandlerFactory.getMetadata(handler.getClass())) {
DecoderFactory.Wrapper wrapper = decoderFactory.getWrapperFor(metadata.getMessageClass());
if (wrapper == null) {
StringBuilder err = new StringBuilder();
err.append("Unable to find decoder for type <");
err.append(metadata.getMessageClass().getName());
err.append("> used in <");
err.append(metadata.getHandlerClass().getName());
err.append(">");
throw new IllegalStateException(err.toString());
}
MessageType key = wrapper.getMetadata().getMessageType();
MessageHandlerWrapper other = wrappers[key.ordinal()];
if (other != null) {
StringBuilder err = new StringBuilder();
err.append("Encountered duplicate MessageHandler handling message type <");
err.append(wrapper.getMetadata().getObjectType().getName());
err.append(">, ").append(metadata.getHandlerClass().getName());
err.append("<");
err.append(metadata.getMessageClass().getName());
err.append("> and ");
err.append(other.getMetadata().getHandlerClass().getName());
err.append("<");
err.append(other.getMetadata().getMessageClass().getName());
err.append("> both implement this message type");
throw new IllegalStateException(err.toString());
} else {
MessageHandlerWrapper handlerWrapper = new MessageHandlerWrapper(handler, metadata, wrapper);
wrappers[key.ordinal()] = handlerWrapper;
}
}
// Update handlerSet
updateMessageHandlerSet();
}
}
use of org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper in project jetty.project by eclipse.
the class JsrEndpointEventDriver method onBinaryFrame.
@Override
public void onBinaryFrame(ByteBuffer buffer, boolean fin) throws IOException {
if (activeMessage == null) {
final MessageHandlerWrapper wrapper = jsrsession.getMessageHandlerWrapper(MessageType.BINARY);
if (wrapper == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No BINARY MessageHandler declared");
}
return;
}
if (wrapper.wantsPartialMessages()) {
activeMessage = new BinaryPartialMessage(wrapper);
} else if (wrapper.wantsStreams()) {
final MessageInputStream stream = new MessageInputStream();
activeMessage = stream;
dispatch(new Runnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
MessageHandler.Whole<InputStream> handler = (Whole<InputStream>) wrapper.getHandler();
handler.onMessage(stream);
}
});
} else {
activeMessage = new BinaryWholeMessage(this, wrapper);
}
}
activeMessage.appendFrame(buffer, fin);
if (fin) {
activeMessage.messageComplete();
activeMessage = null;
}
}
Aggregations