Search in sources :

Example 1 with MessageHandlerWrapper

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;
    }
}
Also used : MessageHandlerWrapper(org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper) TextWholeMessage(org.eclipse.jetty.websocket.jsr356.messages.TextWholeMessage) TextPartialMessage(org.eclipse.jetty.websocket.jsr356.messages.TextPartialMessage) MessageInputStream(org.eclipse.jetty.websocket.common.message.MessageInputStream) MessageHandler(javax.websocket.MessageHandler) MessageReader(org.eclipse.jetty.websocket.common.message.MessageReader) Reader(java.io.Reader) MessageReader(org.eclipse.jetty.websocket.common.message.MessageReader) Whole(javax.websocket.MessageHandler.Whole)

Example 2 with MessageHandlerWrapper

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);
}
Also used : ByteBufferPartialHandler(org.eclipse.jetty.websocket.jsr356.handlers.ByteBufferPartialHandler) Test(org.junit.Test)

Example 3 with MessageHandlerWrapper

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);
}
Also used : StringWholeHandler(org.eclipse.jetty.websocket.jsr356.handlers.StringWholeHandler) ByteArrayWholeHandler(org.eclipse.jetty.websocket.jsr356.handlers.ByteArrayWholeHandler) Test(org.junit.Test)

Example 4 with MessageHandlerWrapper

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();
    }
}
Also used : MessageHandlerMetadata(org.eclipse.jetty.websocket.jsr356.metadata.MessageHandlerMetadata)

Example 5 with MessageHandlerWrapper

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;
    }
}
Also used : MessageHandlerWrapper(org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper) BinaryWholeMessage(org.eclipse.jetty.websocket.jsr356.messages.BinaryWholeMessage) MessageInputStream(org.eclipse.jetty.websocket.common.message.MessageInputStream) MessageHandler(javax.websocket.MessageHandler) BinaryPartialMessage(org.eclipse.jetty.websocket.jsr356.messages.BinaryPartialMessage) MessageInputStream(org.eclipse.jetty.websocket.common.message.MessageInputStream) InputStream(java.io.InputStream) Whole(javax.websocket.MessageHandler.Whole)

Aggregations

Test (org.junit.Test)4 MessageHandler (javax.websocket.MessageHandler)3 Whole (javax.websocket.MessageHandler.Whole)3 MessageHandlerWrapper (org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper)3 StringWholeHandler (org.eclipse.jetty.websocket.jsr356.handlers.StringWholeHandler)3 MessageInputStream (org.eclipse.jetty.websocket.common.message.MessageInputStream)2 ByteArrayWholeHandler (org.eclipse.jetty.websocket.jsr356.handlers.ByteArrayWholeHandler)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 ByteBuffer (java.nio.ByteBuffer)1 PongMessage (javax.websocket.PongMessage)1 MessageReader (org.eclipse.jetty.websocket.common.message.MessageReader)1 JsrPongMessage (org.eclipse.jetty.websocket.jsr356.JsrPongMessage)1 ByteBufferPartialHandler (org.eclipse.jetty.websocket.jsr356.handlers.ByteBufferPartialHandler)1 LongMessageHandler (org.eclipse.jetty.websocket.jsr356.handlers.LongMessageHandler)1 BinaryPartialMessage (org.eclipse.jetty.websocket.jsr356.messages.BinaryPartialMessage)1 BinaryWholeMessage (org.eclipse.jetty.websocket.jsr356.messages.BinaryWholeMessage)1 TextPartialMessage (org.eclipse.jetty.websocket.jsr356.messages.TextPartialMessage)1 TextWholeMessage (org.eclipse.jetty.websocket.jsr356.messages.TextWholeMessage)1 MessageHandlerMetadata (org.eclipse.jetty.websocket.jsr356.metadata.MessageHandlerMetadata)1