Search in sources :

Example 6 with Decoder

use of javax.websocket.Decoder in project undertow by undertow-io.

the class EncodingFactory method createFactory.

public static EncodingFactory createFactory(final ClassIntrospecter classIntrospecter, final List<Class<? extends Decoder>> decoders, final List<Class<? extends Encoder>> encoders) throws DeploymentException {
    final Map<Class<?>, List<InstanceFactory<? extends Encoder>>> binaryEncoders = new HashMap<>();
    final Map<Class<?>, List<InstanceFactory<? extends Decoder>>> binaryDecoders = new HashMap<>();
    final Map<Class<?>, List<InstanceFactory<? extends Encoder>>> textEncoders = new HashMap<>();
    final Map<Class<?>, List<InstanceFactory<? extends Decoder>>> textDecoders = new HashMap<>();
    for (Class<? extends Decoder> decoder : decoders) {
        if (Decoder.Binary.class.isAssignableFrom(decoder)) {
            try {
                Method method = decoder.getMethod("decode", ByteBuffer.class);
                final Class<?> type = resolveReturnType(method, decoder);
                List<InstanceFactory<? extends Decoder>> list = binaryDecoders.get(type);
                if (list == null) {
                    binaryDecoders.put(type, list = new ArrayList<>());
                }
                list.add(classIntrospecter.createInstanceFactory(decoder));
            } catch (NoSuchMethodException e) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
            }
        } else if (Decoder.BinaryStream.class.isAssignableFrom(decoder)) {
            try {
                Method method = decoder.getMethod("decode", InputStream.class);
                final Class<?> type = resolveReturnType(method, decoder);
                List<InstanceFactory<? extends Decoder>> list = binaryDecoders.get(type);
                if (list == null) {
                    binaryDecoders.put(type, list = new ArrayList<>());
                }
                list.add(classIntrospecter.createInstanceFactory(decoder));
            } catch (NoSuchMethodException e) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
            }
        } else if (Decoder.Text.class.isAssignableFrom(decoder)) {
            try {
                Method method = decoder.getMethod("decode", String.class);
                final Class<?> type = resolveReturnType(method, decoder);
                List<InstanceFactory<? extends Decoder>> list = textDecoders.get(type);
                if (list == null) {
                    textDecoders.put(type, list = new ArrayList<>());
                }
                list.add(classIntrospecter.createInstanceFactory(decoder));
            } catch (NoSuchMethodException e) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
            }
        } else if (Decoder.TextStream.class.isAssignableFrom(decoder)) {
            try {
                Method method = decoder.getMethod("decode", Reader.class);
                final Class<?> type = resolveReturnType(method, decoder);
                List<InstanceFactory<? extends Decoder>> list = textDecoders.get(type);
                if (list == null) {
                    textDecoders.put(type, list = new ArrayList<>());
                }
                list.add(createInstanceFactory(classIntrospecter, decoder));
            } catch (NoSuchMethodException e) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
            }
        } else {
            throw JsrWebSocketMessages.MESSAGES.didNotImplementKnownDecoderSubclass(decoder);
        }
    }
    for (Class<? extends Encoder> encoder : encoders) {
        if (Encoder.Binary.class.isAssignableFrom(encoder)) {
            final Class<?> type = findEncodeMethod(encoder, ByteBuffer.class);
            List<InstanceFactory<? extends Encoder>> list = binaryEncoders.get(type);
            if (list == null) {
                binaryEncoders.put(type, list = new ArrayList<>());
            }
            list.add(createInstanceFactory(classIntrospecter, encoder));
        } else if (Encoder.BinaryStream.class.isAssignableFrom(encoder)) {
            final Class<?> type = findEncodeMethod(encoder, void.class, OutputStream.class);
            List<InstanceFactory<? extends Encoder>> list = binaryEncoders.get(type);
            if (list == null) {
                binaryEncoders.put(type, list = new ArrayList<>());
            }
            list.add(createInstanceFactory(classIntrospecter, encoder));
        } else if (Encoder.Text.class.isAssignableFrom(encoder)) {
            final Class<?> type = findEncodeMethod(encoder, String.class);
            List<InstanceFactory<? extends Encoder>> list = textEncoders.get(type);
            if (list == null) {
                textEncoders.put(type, list = new ArrayList<>());
            }
            list.add(createInstanceFactory(classIntrospecter, encoder));
        } else if (Encoder.TextStream.class.isAssignableFrom(encoder)) {
            final Class<?> type = findEncodeMethod(encoder, void.class, Writer.class);
            List<InstanceFactory<? extends Encoder>> list = textEncoders.get(type);
            if (list == null) {
                textEncoders.put(type, list = new ArrayList<>());
            }
            list.add(createInstanceFactory(classIntrospecter, encoder));
        }
    }
    return new EncodingFactory(binaryEncoders, binaryDecoders, textEncoders, textDecoders);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) Method(java.lang.reflect.Method) Decoder(javax.websocket.Decoder) InstanceFactory(io.undertow.servlet.api.InstanceFactory) Encoder(javax.websocket.Encoder) ArrayList(java.util.ArrayList) List(java.util.List) Writer(java.io.Writer)

Example 7 with Decoder

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

the class Util method getDecoders.

public static List<DecoderEntry> getDecoders(List<Class<? extends Decoder>> decoderClazzes) 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
            @SuppressWarnings("unused") Decoder instance;
            try {
                instance = decoderClazz.newInstance();
            } catch (InstantiationException | IllegalAccessException 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) DeploymentException(javax.websocket.DeploymentException) Decoder(javax.websocket.Decoder)

Example 8 with Decoder

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

the class ServerContainerInitializeListener method createEventbusServerEndpointConfig.

protected ServerEndpointConfig createEventbusServerEndpointConfig(ServletContext servletContext) {
    final List<Class<? extends Encoder>> encoders = new LinkedList<>();
    final List<Class<? extends Decoder>> decoders = new LinkedList<>();
    encoders.add(OutputMessageEncoder.class);
    decoders.add(InputMessageDecoder.class);
    final ServerEndpointConfig endpointConfig = create(CheWSConnection.class, websocketContext + eventBusEndPoint).configurator(createConfigurator()).encoders(encoders).decoders(decoders).build();
    endpointConfig.getUserProperties().put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext));
    endpointConfig.getUserProperties().put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext));
    endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext));
    return endpointConfig;
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Encoder(javax.websocket.Encoder) BaseTextEncoder(org.everrest.websockets.message.BaseTextEncoder) Decoder(javax.websocket.Decoder) BaseTextDecoder(org.everrest.websockets.message.BaseTextDecoder) LinkedList(java.util.LinkedList)

Example 9 with Decoder

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

the class TextWholeMessage method messageComplete.

@SuppressWarnings("unchecked")
@Override
public void messageComplete() {
    finished = true;
    DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
    Decoder.Text<Object> textDecoder = (Decoder.Text<Object>) decoder.getDecoder();
    try {
        Object obj = textDecoder.decode(utf.toString());
        wholeHandler.onMessage(obj);
    } catch (DecodeException e) {
        throw new WebSocketException("Unable to decode text data", e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) DecoderFactory(org.eclipse.jetty.websocket.jsr356.DecoderFactory) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Aggregations

Decoder (javax.websocket.Decoder)9 DecodeException (javax.websocket.DecodeException)4 Encoder (javax.websocket.Encoder)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)2 Binary (javax.websocket.Decoder.Binary)2 DeploymentException (javax.websocket.DeploymentException)2 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)2 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)2 DecoderFactory (org.eclipse.jetty.websocket.jsr356.DecoderFactory)2 BaseTextDecoder (org.everrest.websockets.message.BaseTextDecoder)2 BaseTextEncoder (org.everrest.websockets.message.BaseTextEncoder)2 InstanceFactory (io.undertow.servlet.api.InstanceFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1