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