Search in sources :

Example 6 with Encoder

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

the class Encoding method encodeText.

public String encodeText(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = textEncoders.get(o.getClass());
    if (encoders == null) {
        for (Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : textEncoders.entrySet()) {
            if (entry.getKey().isAssignableFrom(o.getClass())) {
                encoders = entry.getValue();
                break;
            }
        }
    }
    if (encoders != null) {
        for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
            Encoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Encoder.Text) {
                return ((Encoder.Text) decoder).encode(o);
            } else {
                try {
                    StringWriter out = new StringWriter();
                    ((Encoder.TextStream) decoder).encode(o, out);
                    return out.toString();
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode text", e);
                }
            }
        }
    }
    if (EncodingFactory.isPrimitiveOrBoxed(o.getClass())) {
        return o.toString();
    }
    throw new EncodeException(o, "Could not encode text");
}
Also used : EncodeException(javax.websocket.EncodeException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Encoder(javax.websocket.Encoder) List(java.util.List) InstanceHandle(io.undertow.servlet.api.InstanceHandle) Map(java.util.Map)

Example 7 with Encoder

use of javax.websocket.Encoder 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 8 with Encoder

use of javax.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;
        try {
            instance = encoderClazz.newInstance();
            instance.init(endpointConfig);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
        }
        EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
        encoderEntries.add(entry);
    }
}
Also used : Encoder(javax.websocket.Encoder) Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) DeploymentException(javax.websocket.DeploymentException)

Example 9 with Encoder

use of javax.websocket.Encoder 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 10 with Encoder

use of javax.websocket.Encoder 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)

Aggregations

Encoder (javax.websocket.Encoder)13 EncodeException (javax.websocket.EncodeException)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 Decoder (javax.websocket.Decoder)4 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3 CharsetEncoder (java.nio.charset.CharsetEncoder)3 List (java.util.List)3 DeploymentException (javax.websocket.DeploymentException)3 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)3 InstanceHandle (io.undertow.servlet.api.InstanceHandle)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 SendResult (javax.websocket.SendResult)2 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)2 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)2 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)2