use of io.undertow.websockets.ServerWebSocketContainer in project quarkus by quarkusio.
the class WebsocketClientProcessor method deploy.
@BuildStep
@Record(ExecutionTime.STATIC_INIT)
public ServerWebSocketContainerBuildItem deploy(final CombinedIndexBuildItem indexBuildItem, WebsocketCoreRecorder recorder, BuildProducer<ReflectiveClassBuildItem> reflection, List<AnnotatedWebsocketEndpointBuildItem> annotatedEndpoints, BeanContainerBuildItem beanContainerBuildItem, WebsocketConfig websocketConfig, BuildProducer<WebSocketDeploymentInfoBuildItem> infoBuildItemBuildProducer, Optional<ServerWebSocketContainerFactoryBuildItem> factoryBuildItem, BuildProducer<ServletContextAttributeBuildItem> servletContextAttributeBuildItemBuildProducer) throws Exception {
final Set<String> endpoints = new HashSet<>();
final Set<String> config = new HashSet<>();
final IndexView index = indexBuildItem.getIndex();
final Collection<ClassInfo> subclasses = index.getAllKnownImplementors(SERVER_APPLICATION_CONFIG);
for (final ClassInfo clazz : subclasses) {
if (!Modifier.isAbstract(clazz.flags())) {
config.add(clazz.name().toString());
}
}
final Collection<ClassInfo> epClasses = index.getAllKnownSubclasses(ENDPOINT);
for (final ClassInfo clazz : epClasses) {
if (!Modifier.isAbstract(clazz.flags())) {
endpoints.add(clazz.name().toString());
}
}
if (annotatedEndpoints.isEmpty() && endpoints.isEmpty() && config.isEmpty()) {
return null;
}
Set<String> annotated = new HashSet<>();
for (AnnotatedWebsocketEndpointBuildItem i : annotatedEndpoints) {
annotated.add(i.className);
}
reflection.produce(new ReflectiveClassBuildItem(true, false, annotated.toArray(new String[annotated.size()])));
registerCodersForReflection(reflection, index.getAnnotations(CLIENT_ENDPOINT));
reflection.produce(new ReflectiveClassBuildItem(true, true, ClientEndpointConfig.Configurator.class.getName()));
RuntimeValue<WebSocketDeploymentInfo> deploymentInfo = recorder.createDeploymentInfo(annotated, endpoints, config, websocketConfig.maxFrameSize, websocketConfig.dispatchToWorker);
infoBuildItemBuildProducer.produce(new WebSocketDeploymentInfoBuildItem(deploymentInfo));
RuntimeValue<ServerWebSocketContainer> serverContainer = recorder.createServerContainer(beanContainerBuildItem.getValue(), deploymentInfo, factoryBuildItem.map(ServerWebSocketContainerFactoryBuildItem::getFactory).orElse(null));
servletContextAttributeBuildItemBuildProducer.produce(new ServletContextAttributeBuildItem(ServerContainer.class.getName(), serverContainer));
return new ServerWebSocketContainerBuildItem(serverContainer);
}
use of io.undertow.websockets.ServerWebSocketContainer in project quarkus by quarkusio.
the class WebsocketCoreRecorder method createServerContainer.
public RuntimeValue<ServerWebSocketContainer> createServerContainer(BeanContainer beanContainer, RuntimeValue<WebSocketDeploymentInfo> infoVal, ServerWebSocketContainerFactory serverContainerFactory) throws DeploymentException {
WebSocketDeploymentInfo info = infoVal.getValue();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ManagedContext requestContext = Arc.container().requestContext();
if (serverContainerFactory == null) {
serverContainerFactory = ServerWebSocketContainer::new;
}
Instance<CurrentIdentityAssociation> currentIdentityAssociation = Arc.container().select(CurrentIdentityAssociation.class);
ServerWebSocketContainer container = serverContainerFactory.create(new ObjectIntrospecter() {
@Override
public <T> ObjectFactory<T> createInstanceFactory(Class<T> clazz) {
BeanContainer.Factory<T> factory = beanContainer.instanceFactory(clazz);
return new ObjectFactory<T>() {
@Override
public ObjectHandle<T> createInstance() {
BeanContainer.Instance<T> instance = factory.create();
return new ObjectHandle<T>() {
@Override
public T getInstance() {
return instance.get();
}
@Override
public void release() {
instance.close();
}
};
}
};
}
}, Thread.currentThread().getContextClassLoader(), new Supplier<EventLoopGroup>() {
@Override
public EventLoopGroup get() {
return ((VertxInternal) VertxCoreRecorder.getVertx().get()).getEventLoopGroup();
}
}, Collections.singletonList(new ContextSetupHandler() {
@Override
public <T, C> Action<T, C> create(Action<T, C> action) {
return new Action<T, C>() {
CurrentIdentityAssociation getCurrentIdentityAssociation() {
if (currentIdentityAssociation.isResolvable()) {
return currentIdentityAssociation.get();
}
return null;
}
@Override
public T call(C context, UndertowSession session) throws Exception {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(cl);
boolean required = !requestContext.isActive();
if (required) {
requestContext.activate();
Principal p = session.getUserPrincipal();
if (p instanceof WebSocketPrincipal) {
var current = getCurrentIdentityAssociation();
if (current != null) {
current.setIdentity(((WebSocketPrincipal) p).getSecurityIdentity());
}
}
}
try {
return action.call(context, session);
} finally {
try {
if (required) {
requestContext.terminate();
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
}
};
}
}), info.isDispatchToWorkerThread(), null, null, info.getExecutor(), Collections.emptyList(), info.getMaxFrameSize(), new Supplier<Principal>() {
@Override
public Principal get() {
if (currentIdentityAssociation.isResolvable()) {
return new WebSocketPrincipal(currentIdentityAssociation.get().getIdentity());
}
return null;
}
});
for (Class<?> i : info.getAnnotatedEndpoints()) {
container.addEndpoint(i);
}
for (ServerEndpointConfig i : info.getProgramaticEndpoints()) {
container.addEndpoint(i);
}
UndertowContainerProvider.setDefaultContainer(container);
return new RuntimeValue<>(container);
}
Aggregations