use of jakarta.websocket.Encoder in project tomcat by apache.
the class WsServerContainer method validateEncoders.
private static void validateEncoders(Class<? extends Encoder>[] encoders, InstanceManager instanceManager) throws DeploymentException {
for (Class<? extends Encoder> encoder : encoders) {
// Need to instantiate encoder to ensure it is valid and that
// deployment can be failed if it is not. The encoder is then
// discarded immediately.
Encoder instance;
try {
if (instanceManager == null) {
instance = encoder.getConstructor().newInstance();
} else {
instance = (Encoder) instanceManager.newInstance(encoder);
instanceManager.destroyInstance(instance);
}
} catch (ReflectiveOperationException | NamingException e) {
throw new DeploymentException(sm.getString("serverContainer.encoderFail", encoder.getName()), e);
}
}
}
use of jakarta.websocket.Encoder 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 jakarta.websocket.Encoder 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 jakarta.websocket.Encoder in project tomcat by apache.
the class WsRemoteEndpointImplBase method setEncoders.
protected void setEncoders(EndpointConfig endpointConfig) throws DeploymentException {
encoderEntries.clear();
for (Class<? extends Encoder> encoderClazz : endpointConfig.getEncoders()) {
Encoder instance;
InstanceManager instanceManager = wsSession.getInstanceManager();
try {
if (instanceManager == null) {
instance = encoderClazz.getConstructor().newInstance();
} else {
instance = (Encoder) instanceManager.newInstance(encoderClazz);
}
instance.init(endpointConfig);
} catch (ReflectiveOperationException | NamingException e) {
throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
}
EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
encoderEntries.add(entry);
}
}
Aggregations