use of org.eclipse.jetty.websocket.common.message.MessageOutputStream 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);
}
use of org.eclipse.jetty.websocket.common.message.MessageOutputStream 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);
}
Aggregations