Search in sources :

Example 1 with Decoder

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

the class Util method getDecoders.

/**
 * Build the list of decoder entries from a set of decoder implementations.
 *
 * @param decoderClazzes    Decoder implementation classes
 * @param instanceManager   Instance manager to use to create Decoder
 *                              instances
 *
 * @return List of mappings from target type to associated decoder
 *
 * @throws DeploymentException If a provided decoder class is not valid
 */
public static List<DecoderEntry> getDecoders(List<Class<? extends Decoder>> decoderClazzes, InstanceManager instanceManager) throws DeploymentException {
    List<DecoderEntry> result = new ArrayList<>();
    if (decoderClazzes != null) {
        for (Class<? extends Decoder> decoderClazz : decoderClazzes) {
            // Need to instantiate decoder to ensure it is valid and that
            // deployment can be failed if it is not
            Decoder instance;
            try {
                if (instanceManager == null) {
                    instance = decoderClazz.getConstructor().newInstance();
                } else {
                    instance = (Decoder) instanceManager.newInstance(decoderClazz);
                    // Don't need this instance, so destroy it
                    instanceManager.destroyInstance(instance);
                }
            } catch (ReflectiveOperationException | IllegalArgumentException | SecurityException | NamingException e) {
                throw new DeploymentException(sm.getString("pojoMethodMapping.invalidDecoder", decoderClazz.getName()), e);
            }
            DecoderEntry entry = new DecoderEntry(Util.getDecoderType(decoderClazz), decoderClazz);
            result.add(entry);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) DeploymentException(jakarta.websocket.DeploymentException) Decoder(jakarta.websocket.Decoder)

Example 2 with Decoder

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

the class PojoMessageHandlerWholeBinary method decode.

@Override
protected Object decode(ByteBuffer message) throws DecodeException {
    for (Decoder decoder : decoders) {
        if (decoder instanceof Binary) {
            if (((Binary<?>) decoder).willDecode(message)) {
                return ((Binary<?>) decoder).decode(message);
            }
        } else {
            byte[] array = new byte[message.limit() - message.position()];
            message.get(array);
            ByteArrayInputStream bais = new ByteArrayInputStream(array);
            try {
                return ((BinaryStream<?>) decoder).decode(bais);
            } catch (IOException ioe) {
                throw new DecodeException(message, sm.getString("pojoMessageHandlerWhole.decodeIoFail"), ioe);
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryStream(jakarta.websocket.Decoder.BinaryStream) Binary(jakarta.websocket.Decoder.Binary) IOException(java.io.IOException) Decoder(jakarta.websocket.Decoder) DecodeException(jakarta.websocket.DecodeException)

Aggregations

Decoder (jakarta.websocket.Decoder)2 DecodeException (jakarta.websocket.DecodeException)1 Binary (jakarta.websocket.Decoder.Binary)1 BinaryStream (jakarta.websocket.Decoder.BinaryStream)1 DeploymentException (jakarta.websocket.DeploymentException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NamingException (javax.naming.NamingException)1