Search in sources :

Example 11 with Encoder

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

the class JsrAsyncRemote method sendObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void sendObject(Object data, SendHandler handler) {
    assertMessageNotNull(data);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendObject({},{})", data, handler);
    }
    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 etxt = (Encoder.Text) encoder;
        try {
            String msg = etxt.encode(data);
            sendText(msg, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.TextStream) {
        Encoder.TextStream etxt = (Encoder.TextStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageWriter writer = new MessageWriter(session)) {
            writer.setCallback(callback);
            etxt.encode(data, writer);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.Binary) {
        Encoder.Binary ebin = (Encoder.Binary) encoder;
        try {
            ByteBuffer buf = ebin.encode(data);
            sendBinary(buf, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.BinaryStream) {
        Encoder.BinaryStream ebin = (Encoder.BinaryStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageOutputStream out = new MessageOutputStream(session)) {
            out.setCallback(callback);
            ebin.encode(data, out);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    }
    throw new IllegalArgumentException("Unknown encoder type: " + encoder);
}
Also used : EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) MessageOutputStream(org.eclipse.jetty.websocket.common.message.MessageOutputStream) Encoder(javax.websocket.Encoder) SendResult(javax.websocket.SendResult) MessageWriter(org.eclipse.jetty.websocket.common.message.MessageWriter)

Example 12 with Encoder

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

the class EncoderTest method testSingleQuotes.

@Test
public void testSingleQuotes() throws Exception {
    EchoServer eserver = new EchoServer(server);
    try {
        eserver.start();
        QuotesSocket quoter = new QuotesSocket();
        ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
        List<Class<? extends Encoder>> encoders = new ArrayList<>();
        encoders.add(QuotesEncoder.class);
        builder.encoders(encoders);
        ClientEndpointConfig cec = builder.build();
        client.connectToServer(quoter, cec, server.getWsUri());
        Quotes ben = getQuotes("quotes-ben.txt");
        quoter.write(ben);
        quoter.messageQueue.awaitEventCount(1, 1000, TimeUnit.MILLISECONDS);
        String result = quoter.messageQueue.poll();
        assertReceivedQuotes(result, ben);
    } finally {
        eserver.stop();
    }
}
Also used : Encoder(javax.websocket.Encoder) ArrayList(java.util.ArrayList) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 13 with Encoder

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

the class Encoding method encodeBinary.

public ByteBuffer encodeBinary(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = binaryEncoders.get(o.getClass());
    if (encoders == null) {
        for (Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : binaryEncoders.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.Binary) {
                return ((Encoder.Binary) decoder).encode(o);
            } else {
                try {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ((Encoder.BinaryStream) decoder).encode(o, out);
                    return ByteBuffer.wrap(out.toByteArray());
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode binary", e);
                }
            }
        }
    }
    throw new EncodeException(o, "Could not encode binary");
}
Also used : EncodeException(javax.websocket.EncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Encoder(javax.websocket.Encoder) List(java.util.List) InstanceHandle(io.undertow.servlet.api.InstanceHandle) Map(java.util.Map)

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