Search in sources :

Example 1 with EncodeException

use of javax.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(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) DeploymentException(javax.websocket.DeploymentException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) EncodeException(javax.websocket.EncodeException) Encoder(javax.websocket.Encoder) Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) SendResult(javax.websocket.SendResult) Writer(java.io.Writer)

Example 2 with EncodeException

use of javax.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 : Encoder(javax.websocket.Encoder) Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) OutputStream(java.io.OutputStream) EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) Writer(java.io.Writer)

Example 3 with EncodeException

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

the class InitializeEventMessenger method send.

protected void send(final LanguageServerInitializeEventDto message) {
    try {
        final ChannelBroadcastMessage bm = new ChannelBroadcastMessage();
        bm.setChannel("languageserver");
        bm.setBody(new Gson().toJson(message));
        WSConnectionContext.sendMessage(bm);
    } catch (EncodeException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : ChannelBroadcastMessage(org.everrest.websockets.message.ChannelBroadcastMessage) Gson(com.google.gson.Gson) EncodeException(javax.websocket.EncodeException) IOException(java.io.IOException)

Example 4 with EncodeException

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

the class PublishDiagnosticsParamsMessenger method onEvent.

public void onEvent(final PublishDiagnosticsParams event) {
    try {
        if (event instanceof PublishDiagnosticsParamsImpl && event.getUri().startsWith("file:///projects")) {
            ((PublishDiagnosticsParamsImpl) event).setUri(event.getUri().substring(16));
        }
        final ChannelBroadcastMessage bm = new ChannelBroadcastMessage();
        bm.setChannel("languageserver/textDocument/publishDiagnostics");
        bm.setBody(new Gson().toJson(event));
        WSConnectionContext.sendMessage(bm);
    } catch (EncodeException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : ChannelBroadcastMessage(org.everrest.websockets.message.ChannelBroadcastMessage) Gson(com.google.gson.Gson) EncodeException(javax.websocket.EncodeException) PublishDiagnosticsParamsImpl(io.typefox.lsapi.impl.PublishDiagnosticsParamsImpl) IOException(java.io.IOException)

Example 5 with EncodeException

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

Aggregations

EncodeException (javax.websocket.EncodeException)11 IOException (java.io.IOException)7 Encoder (javax.websocket.Encoder)6 ByteBuffer (java.nio.ByteBuffer)4 ChannelBroadcastMessage (org.everrest.websockets.message.ChannelBroadcastMessage)4 Gson (com.google.gson.Gson)3 InstanceHandle (io.undertow.servlet.api.InstanceHandle)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 List (java.util.List)2 Map (java.util.Map)2 SendResult (javax.websocket.SendResult)2 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)2 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)2 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)2 PublishDiagnosticsParamsImpl (io.typefox.lsapi.impl.PublishDiagnosticsParamsImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1