use of javax.websocket.Encoder 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");
}
use of javax.websocket.Encoder in project undertow by undertow-io.
the class EncodingFactory method createFactory.
public static EncodingFactory createFactory(final ClassIntrospecter classIntrospecter, final List<Class<? extends Decoder>> decoders, final List<Class<? extends Encoder>> encoders) throws DeploymentException {
final Map<Class<?>, List<InstanceFactory<? extends Encoder>>> binaryEncoders = new HashMap<>();
final Map<Class<?>, List<InstanceFactory<? extends Decoder>>> binaryDecoders = new HashMap<>();
final Map<Class<?>, List<InstanceFactory<? extends Encoder>>> textEncoders = new HashMap<>();
final Map<Class<?>, List<InstanceFactory<? extends Decoder>>> textDecoders = new HashMap<>();
for (Class<? extends Decoder> decoder : decoders) {
if (Decoder.Binary.class.isAssignableFrom(decoder)) {
try {
Method method = decoder.getMethod("decode", ByteBuffer.class);
final Class<?> type = resolveReturnType(method, decoder);
List<InstanceFactory<? extends Decoder>> list = binaryDecoders.get(type);
if (list == null) {
binaryDecoders.put(type, list = new ArrayList<>());
}
list.add(classIntrospecter.createInstanceFactory(decoder));
} catch (NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
}
} else if (Decoder.BinaryStream.class.isAssignableFrom(decoder)) {
try {
Method method = decoder.getMethod("decode", InputStream.class);
final Class<?> type = resolveReturnType(method, decoder);
List<InstanceFactory<? extends Decoder>> list = binaryDecoders.get(type);
if (list == null) {
binaryDecoders.put(type, list = new ArrayList<>());
}
list.add(classIntrospecter.createInstanceFactory(decoder));
} catch (NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
}
} else if (Decoder.Text.class.isAssignableFrom(decoder)) {
try {
Method method = decoder.getMethod("decode", String.class);
final Class<?> type = resolveReturnType(method, decoder);
List<InstanceFactory<? extends Decoder>> list = textDecoders.get(type);
if (list == null) {
textDecoders.put(type, list = new ArrayList<>());
}
list.add(classIntrospecter.createInstanceFactory(decoder));
} catch (NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
}
} else if (Decoder.TextStream.class.isAssignableFrom(decoder)) {
try {
Method method = decoder.getMethod("decode", Reader.class);
final Class<?> type = resolveReturnType(method, decoder);
List<InstanceFactory<? extends Decoder>> list = textDecoders.get(type);
if (list == null) {
textDecoders.put(type, list = new ArrayList<>());
}
list.add(createInstanceFactory(classIntrospecter, decoder));
} catch (NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfDecodeMethodForClass(decoder, e);
}
} else {
throw JsrWebSocketMessages.MESSAGES.didNotImplementKnownDecoderSubclass(decoder);
}
}
for (Class<? extends Encoder> encoder : encoders) {
if (Encoder.Binary.class.isAssignableFrom(encoder)) {
final Class<?> type = findEncodeMethod(encoder, ByteBuffer.class);
List<InstanceFactory<? extends Encoder>> list = binaryEncoders.get(type);
if (list == null) {
binaryEncoders.put(type, list = new ArrayList<>());
}
list.add(createInstanceFactory(classIntrospecter, encoder));
} else if (Encoder.BinaryStream.class.isAssignableFrom(encoder)) {
final Class<?> type = findEncodeMethod(encoder, void.class, OutputStream.class);
List<InstanceFactory<? extends Encoder>> list = binaryEncoders.get(type);
if (list == null) {
binaryEncoders.put(type, list = new ArrayList<>());
}
list.add(createInstanceFactory(classIntrospecter, encoder));
} else if (Encoder.Text.class.isAssignableFrom(encoder)) {
final Class<?> type = findEncodeMethod(encoder, String.class);
List<InstanceFactory<? extends Encoder>> list = textEncoders.get(type);
if (list == null) {
textEncoders.put(type, list = new ArrayList<>());
}
list.add(createInstanceFactory(classIntrospecter, encoder));
} else if (Encoder.TextStream.class.isAssignableFrom(encoder)) {
final Class<?> type = findEncodeMethod(encoder, void.class, Writer.class);
List<InstanceFactory<? extends Encoder>> list = textEncoders.get(type);
if (list == null) {
textEncoders.put(type, list = new ArrayList<>());
}
list.add(createInstanceFactory(classIntrospecter, encoder));
}
}
return new EncodingFactory(binaryEncoders, binaryDecoders, textEncoders, textDecoders);
}
use of javax.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;
try {
instance = encoderClazz.newInstance();
instance.init(endpointConfig);
} catch (InstantiationException | IllegalAccessException e) {
throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
}
EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
encoderEntries.add(entry);
}
}
use of javax.websocket.Encoder in project che by eclipse.
the class ServerContainerInitializeListener method createEventbusServerEndpointConfig.
protected ServerEndpointConfig createEventbusServerEndpointConfig(ServletContext servletContext) {
final List<Class<? extends Encoder>> encoders = new LinkedList<>();
final List<Class<? extends Decoder>> decoders = new LinkedList<>();
encoders.add(OutputMessageEncoder.class);
decoders.add(InputMessageDecoder.class);
final ServerEndpointConfig endpointConfig = create(CheWSConnection.class, websocketContext + eventBusEndPoint).configurator(createConfigurator()).encoders(encoders).decoders(decoders).build();
endpointConfig.getUserProperties().put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext));
endpointConfig.getUserProperties().put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext));
endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext));
return endpointConfig;
}
use of javax.websocket.Encoder 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);
}
Aggregations