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);
}
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();
}
}
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");
}
Aggregations