use of io.undertow.servlet.api.InstanceHandle 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 io.undertow.servlet.api.InstanceHandle 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");
}
use of io.undertow.servlet.api.InstanceHandle in project undertow by undertow-io.
the class EndpointSessionHandler method onConnect.
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
ConfiguredServerEndpoint config = HandshakeUtil.getConfig(channel);
try {
if (container.isClosed()) {
// if the underlying container is closed we just reject
channel.sendClose();
channel.resumeReceives();
return;
}
InstanceFactory<?> endpointFactory = config.getEndpointFactory();
ServerEndpointConfig.Configurator configurator = config.getEndpointConfiguration().getConfigurator();
final InstanceHandle<?> instance;
DefaultContainerConfigurator.setCurrentInstanceFactory(endpointFactory);
final Object instanceFromConfigurator = configurator.getEndpointInstance(config.getEndpointConfiguration().getEndpointClass());
final InstanceHandle<?> factoryInstance = DefaultContainerConfigurator.clearCurrentInstanceFactory();
if (factoryInstance == null) {
instance = new ImmediateInstanceHandle<>(instanceFromConfigurator);
} else if (factoryInstance.getInstance() == instanceFromConfigurator) {
instance = factoryInstance;
} else {
// the default instance has been wrapped
instance = new InstanceHandle<Object>() {
@Override
public Object getInstance() {
return instanceFromConfigurator;
}
@Override
public void release() {
factoryInstance.release();
}
};
}
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
Principal principal = exchange.getAttachment(HandshakeUtil.PRINCIPAL);
if (principal == null) {
if (src.getServletRequest() instanceof HttpServletRequest) {
principal = ((HttpServletRequest) src.getServletRequest()).getUserPrincipal();
} else {
principal = src.getOriginalRequest().getUserPrincipal();
}
}
final InstanceHandle<Endpoint> endpointInstance;
if (config.getAnnotatedEndpointFactory() != null) {
final AnnotatedEndpoint annotated = config.getAnnotatedEndpointFactory().createInstance(instance);
endpointInstance = new InstanceHandle<Endpoint>() {
@Override
public Endpoint getInstance() {
return annotated;
}
@Override
public void release() {
instance.release();
}
};
} else {
endpointInstance = (InstanceHandle<Endpoint>) instance;
}
UndertowSession session = new UndertowSession(channel, URI.create(exchange.getRequestURI()), exchange.getAttachment(HandshakeUtil.PATH_PARAMS), exchange.getRequestParameters(), this, principal, endpointInstance, config.getEndpointConfiguration(), exchange.getQueryString(), config.getEncodingFactory().createEncoding(config.getEndpointConfiguration()), config, channel.getSubProtocol(), Collections.<Extension>emptyList(), null);
config.addOpenSession(session);
session.setMaxBinaryMessageBufferSize(getContainer().getDefaultMaxBinaryMessageBufferSize());
session.setMaxTextMessageBufferSize(getContainer().getDefaultMaxTextMessageBufferSize());
session.setMaxIdleTimeout(getContainer().getDefaultMaxSessionIdleTimeout());
session.getAsyncRemote().setSendTimeout(getContainer().getDefaultAsyncSendTimeout());
try {
endpointInstance.getInstance().onOpen(session, config.getEndpointConfiguration());
} catch (Exception e) {
endpointInstance.getInstance().onError(session, e);
IoUtils.safeClose(session);
}
channel.resumeReceives();
} catch (Exception e) {
JsrWebSocketLogger.REQUEST_LOGGER.endpointCreationFailed(e);
IoUtils.safeClose(channel);
}
}
use of io.undertow.servlet.api.InstanceHandle in project undertow by undertow-io.
the class ServerSentEventSCI method onStartup.
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
if (c == null || c.isEmpty()) {
return;
}
try {
final Map<String, ServerSentEventConnectionCallback> callbacks = new HashMap<>();
ServletContextImpl servletContext = (ServletContextImpl) ctx;
final List<InstanceHandle<?>> handles = new ArrayList<>();
for (Class<?> clazz : c) {
final ServerSentEvent annotation = clazz.getAnnotation(ServerSentEvent.class);
if (annotation == null) {
continue;
}
String path = annotation.value();
final InstanceHandle<?> instance = servletContext.getDeployment().getDeploymentInfo().getClassIntrospecter().createInstanceFactory(clazz).createInstance();
handles.add(instance);
callbacks.put(path, (ServerSentEventConnectionCallback) instance.getInstance());
}
if (callbacks.isEmpty()) {
return;
}
servletContext.getDeployment().getDeploymentInfo().addInnerHandlerChainWrapper(new HandlerWrapper() {
@Override
public HttpHandler wrap(HttpHandler handler) {
PathTemplateHandler pathTemplateHandler = new PathTemplateHandler(handler, false);
for (Map.Entry<String, ServerSentEventConnectionCallback> e : callbacks.entrySet()) {
pathTemplateHandler.add(e.getKey(), new ServerSentEventHandler(e.getValue()));
}
return pathTemplateHandler;
}
});
servletContext.addListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
for (InstanceHandle<?> h : handles) {
h.release();
}
}
});
} catch (Exception e) {
throw new ServletException(e);
}
}
Aggregations