Search in sources :

Example 1 with EncodeException

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

the class WsRemoteEndpointImplBase method sendObjectByCompletion.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void sendObjectByCompletion(Object obj, SendHandler completion) {
    if (obj == null) {
        throw new IllegalArgumentException(sm.getString("wsRemoteEndpoint.nullData"));
    }
    if (completion == null) {
        throw new IllegalArgumentException(sm.getString("wsRemoteEndpoint.nullHandler"));
    }
    /*
         * Note that the implementation will convert primitives and their object
         * equivalents by default but that users are free to specify their own
         * encoders and decoders for this if they wish.
         */
    Encoder encoder = findEncoder(obj);
    if (encoder == null && Util.isPrimitive(obj.getClass())) {
        String msg = obj.toString();
        sendStringByCompletion(msg, completion);
        return;
    }
    if (encoder == null && byte[].class.isAssignableFrom(obj.getClass())) {
        ByteBuffer msg = ByteBuffer.wrap((byte[]) obj);
        sendBytesByCompletion(msg, completion);
        return;
    }
    try {
        if (encoder instanceof Encoder.Text) {
            String msg = ((Encoder.Text) encoder).encode(obj);
            sendStringByCompletion(msg, completion);
        } else if (encoder instanceof Encoder.TextStream) {
            try (Writer w = getSendWriter()) {
                ((Encoder.TextStream) encoder).encode(obj, w);
            }
            completion.onResult(new SendResult());
        } else if (encoder instanceof Encoder.Binary) {
            ByteBuffer msg = ((Encoder.Binary) encoder).encode(obj);
            sendBytesByCompletion(msg, completion);
        } else if (encoder instanceof Encoder.BinaryStream) {
            try (OutputStream os = getSendStream()) {
                ((Encoder.BinaryStream) encoder).encode(obj, os);
            }
            completion.onResult(new SendResult());
        } else {
            throw new EncodeException(obj, sm.getString("wsRemoteEndpoint.noEncoder", obj.getClass()));
        }
    } catch (Exception e) {
        SendResult sr = new SendResult(e);
        completion.onResult(sr);
    }
}
Also used : OutputStream(java.io.OutputStream) EncodeException(jakarta.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) EncodeException(jakarta.websocket.EncodeException) NamingException(javax.naming.NamingException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DeploymentException(jakarta.websocket.DeploymentException) Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) Encoder(jakarta.websocket.Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) SendResult(jakarta.websocket.SendResult) Writer(java.io.Writer)

Example 2 with EncodeException

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

the class WsRemoteEndpointImplBase method sendObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void sendObject(Object obj) throws IOException, EncodeException {
    if (obj == null) {
        throw new IllegalArgumentException(sm.getString("wsRemoteEndpoint.nullData"));
    }
    /*
         * Note that the implementation will convert primitives and their object
         * equivalents by default but that users are free to specify their own
         * encoders and decoders for this if they wish.
         */
    Encoder encoder = findEncoder(obj);
    if (encoder == null && Util.isPrimitive(obj.getClass())) {
        String msg = obj.toString();
        sendString(msg);
        return;
    }
    if (encoder == null && byte[].class.isAssignableFrom(obj.getClass())) {
        ByteBuffer msg = ByteBuffer.wrap((byte[]) obj);
        sendBytes(msg);
        return;
    }
    if (encoder instanceof Encoder.Text) {
        String msg = ((Encoder.Text) encoder).encode(obj);
        sendString(msg);
    } else if (encoder instanceof Encoder.TextStream) {
        try (Writer w = getSendWriter()) {
            ((Encoder.TextStream) encoder).encode(obj, w);
        }
    } else if (encoder instanceof Encoder.Binary) {
        ByteBuffer msg = ((Encoder.Binary) encoder).encode(obj);
        sendBytes(msg);
    } else if (encoder instanceof Encoder.BinaryStream) {
        try (OutputStream os = getSendStream()) {
            ((Encoder.BinaryStream) encoder).encode(obj, os);
        }
    } else {
        throw new EncodeException(obj, sm.getString("wsRemoteEndpoint.noEncoder", obj.getClass()));
    }
}
Also used : Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) Encoder(jakarta.websocket.Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) OutputStream(java.io.OutputStream) EncodeException(jakarta.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) Writer(java.io.Writer)

Example 3 with EncodeException

use of jakarta.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(ProgrammaticServerEndpointConfig.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(jakarta.websocket.WebSocketContainer) EncodeException(jakarta.websocket.EncodeException) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

EncodeException (jakarta.websocket.EncodeException)3 Encoder (jakarta.websocket.Encoder)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 ByteBuffer (java.nio.ByteBuffer)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)2 DeploymentException (jakarta.websocket.DeploymentException)1 SendResult (jakarta.websocket.SendResult)1 Session (jakarta.websocket.Session)1 WebSocketContainer (jakarta.websocket.WebSocketContainer)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 NamingException (javax.naming.NamingException)1 Context (org.apache.catalina.Context)1 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1