Search in sources :

Example 6 with EncodeException

use of javax.websocket.EncodeException in project tomcat by apache.

the class TestEncodingDecoding method testUnsupportedObject.

@Test
public void testUnsupportedObject() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgramaticServerEndpointConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);
    // This should fail
    Object msg1 = new Object();
    try {
        session.getBasicRemote().sendObject(msg1);
        Assert.fail("No exception thrown ");
    } catch (EncodeException e) {
    // Expected
    } catch (Throwable t) {
        Assert.fail("Wrong exception type");
    } finally {
        session.close();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) EncodeException(javax.websocket.EncodeException) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with EncodeException

use of javax.websocket.EncodeException in project jetty.project by eclipse.

the class AbstractJsrRemote method sendObjectViaFuture.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Future<Void> sendObjectViaFuture(Object data) {
    assertMessageNotNull(data);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendObject({})", data);
    }
    Encoder encoder = encoders.getEncoderFor(data.getClass());
    if (encoder == null) {
        throw new IllegalArgumentException("No encoder for type: " + data.getClass());
    }
    if (encoder instanceof Encoder.Text) {
        Encoder.Text text = (Encoder.Text) encoder;
        try {
            String msg = text.encode(data);
            return jettyRemote.sendStringByFuture(msg);
        } catch (EncodeException e) {
            return new EncodeFailedFuture(data, text, Encoder.Text.class, e);
        }
    } else if (encoder instanceof Encoder.TextStream) {
        Encoder.TextStream etxt = (Encoder.TextStream) encoder;
        FutureWriteCallback callback = new FutureWriteCallback();
        try (MessageWriter writer = new MessageWriter(session)) {
            writer.setCallback(callback);
            etxt.encode(data, writer);
            return callback;
        } catch (EncodeException | IOException e) {
            return new EncodeFailedFuture(data, etxt, Encoder.Text.class, e);
        }
    } else if (encoder instanceof Encoder.Binary) {
        Encoder.Binary ebin = (Encoder.Binary) encoder;
        try {
            ByteBuffer buf = ebin.encode(data);
            return jettyRemote.sendBytesByFuture(buf);
        } catch (EncodeException e) {
            return new EncodeFailedFuture(data, ebin, Encoder.Binary.class, e);
        }
    } else if (encoder instanceof Encoder.BinaryStream) {
        Encoder.BinaryStream ebin = (Encoder.BinaryStream) encoder;
        FutureWriteCallback callback = new FutureWriteCallback();
        try (MessageOutputStream out = new MessageOutputStream(session)) {
            out.setCallback(callback);
            ebin.encode(data, out);
            return callback;
        } catch (EncodeException | IOException e) {
            return new EncodeFailedFuture(data, ebin, Encoder.Binary.class, e);
        }
    }
    throw new IllegalArgumentException("Unknown encoder type: " + encoder);
}
Also used : FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback) EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) MessageOutputStream(org.eclipse.jetty.websocket.common.message.MessageOutputStream) Encoder(javax.websocket.Encoder) MessageWriter(org.eclipse.jetty.websocket.common.message.MessageWriter) EncodeFailedFuture(org.eclipse.jetty.websocket.jsr356.encoders.EncodeFailedFuture)

Example 8 with EncodeException

use of javax.websocket.EncodeException in project jetty.project by eclipse.

the class JsrAsyncRemote method sendObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void sendObject(Object data, SendHandler handler) {
    assertMessageNotNull(data);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendObject({},{})", data, handler);
    }
    Encoder encoder = encoders.getEncoderFor(data.getClass());
    if (encoder == null) {
        throw new IllegalArgumentException("No encoder for type: " + data.getClass());
    }
    if (encoder instanceof Encoder.Text) {
        Encoder.Text etxt = (Encoder.Text) encoder;
        try {
            String msg = etxt.encode(data);
            sendText(msg, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.TextStream) {
        Encoder.TextStream etxt = (Encoder.TextStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageWriter writer = new MessageWriter(session)) {
            writer.setCallback(callback);
            etxt.encode(data, writer);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.Binary) {
        Encoder.Binary ebin = (Encoder.Binary) encoder;
        try {
            ByteBuffer buf = ebin.encode(data);
            sendBinary(buf, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.BinaryStream) {
        Encoder.BinaryStream ebin = (Encoder.BinaryStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageOutputStream out = new MessageOutputStream(session)) {
            out.setCallback(callback);
            ebin.encode(data, out);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    }
    throw new IllegalArgumentException("Unknown encoder type: " + encoder);
}
Also used : EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) MessageOutputStream(org.eclipse.jetty.websocket.common.message.MessageOutputStream) Encoder(javax.websocket.Encoder) SendResult(javax.websocket.SendResult) MessageWriter(org.eclipse.jetty.websocket.common.message.MessageWriter)

Example 9 with EncodeException

use of javax.websocket.EncodeException in project che by eclipse.

the class ShowMessageMessenger method onEvent.

public void onEvent(final MessageParams event) {
    try {
        final ChannelBroadcastMessage bm = new ChannelBroadcastMessage();
        bm.setChannel("languageserver/window/showMessage");
        bm.setBody(new Gson().toJson(event));
        WSConnectionContext.sendMessage(bm);
    } catch (EncodeException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : ChannelBroadcastMessage(org.everrest.websockets.message.ChannelBroadcastMessage) Gson(com.google.gson.Gson) EncodeException(javax.websocket.EncodeException) IOException(java.io.IOException)

Example 10 with EncodeException

use of javax.websocket.EncodeException in project che by eclipse.

the class MavenWebSocketCommunication method send.

@Override
public void send(JsonObject dto, MessageType type) {
    try {
        ChannelBroadcastMessage message = new ChannelBroadcastMessage();
        message.setChannel(MavenAttributes.MAVEN_CHANEL_NAME);
        dto.addProperty("$type", type.getType());
        message.setBody(dto.toString());
        WSConnectionContext.sendMessage(message);
    } catch (EncodeException | IOException e) {
        LOG.error("Can't send maven message:", e);
    }
}
Also used : ChannelBroadcastMessage(org.everrest.websockets.message.ChannelBroadcastMessage) EncodeException(javax.websocket.EncodeException) IOException(java.io.IOException)

Aggregations

EncodeException (javax.websocket.EncodeException)11 IOException (java.io.IOException)7 Encoder (javax.websocket.Encoder)6 ByteBuffer (java.nio.ByteBuffer)4 ChannelBroadcastMessage (org.everrest.websockets.message.ChannelBroadcastMessage)4 Gson (com.google.gson.Gson)3 InstanceHandle (io.undertow.servlet.api.InstanceHandle)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 List (java.util.List)2 Map (java.util.Map)2 SendResult (javax.websocket.SendResult)2 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)2 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)2 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)2 PublishDiagnosticsParamsImpl (io.typefox.lsapi.impl.PublishDiagnosticsParamsImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1