Search in sources :

Example 1 with Encoder

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

the class WsServerContainer method validateEncoders.

private static void validateEncoders(Class<? extends Encoder>[] encoders, InstanceManager instanceManager) throws DeploymentException {
    for (Class<? extends Encoder> encoder : encoders) {
        // Need to instantiate encoder to ensure it is valid and that
        // deployment can be failed if it is not. The encoder is then
        // discarded immediately.
        Encoder instance;
        try {
            if (instanceManager == null) {
                instance = encoder.getConstructor().newInstance();
            } else {
                instance = (Encoder) instanceManager.newInstance(encoder);
                instanceManager.destroyInstance(instance);
            }
        } catch (ReflectiveOperationException | NamingException e) {
            throw new DeploymentException(sm.getString("serverContainer.encoderFail", encoder.getName()), e);
        }
    }
}
Also used : Encoder(jakarta.websocket.Encoder) NamingException(javax.naming.NamingException) DeploymentException(jakarta.websocket.DeploymentException)

Example 2 with Encoder

use of jakarta.websocket.Encoder 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 3 with Encoder

use of jakarta.websocket.Encoder 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 4 with Encoder

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

the class WsRemoteEndpointImplBase method setEncoders.

protected void setEncoders(EndpointConfig endpointConfig) throws DeploymentException {
    encoderEntries.clear();
    for (Class<? extends Encoder> encoderClazz : endpointConfig.getEncoders()) {
        Encoder instance;
        InstanceManager instanceManager = wsSession.getInstanceManager();
        try {
            if (instanceManager == null) {
                instance = encoderClazz.getConstructor().newInstance();
            } else {
                instance = (Encoder) instanceManager.newInstance(encoderClazz);
            }
            instance.init(endpointConfig);
        } catch (ReflectiveOperationException | NamingException e) {
            throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
        }
        EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
        encoderEntries.add(entry);
    }
}
Also used : Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) Encoder(jakarta.websocket.Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) InstanceManager(org.apache.tomcat.InstanceManager) NamingException(javax.naming.NamingException) DeploymentException(jakarta.websocket.DeploymentException)

Aggregations

Encoder (jakarta.websocket.Encoder)4 DeploymentException (jakarta.websocket.DeploymentException)3 CharsetEncoder (java.nio.charset.CharsetEncoder)3 NamingException (javax.naming.NamingException)3 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)3 EncodeException (jakarta.websocket.EncodeException)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 ByteBuffer (java.nio.ByteBuffer)2 SendResult (jakarta.websocket.SendResult)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 InstanceManager (org.apache.tomcat.InstanceManager)1