Search in sources :

Example 1 with ServerWebSocketContainer

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);
}
Also used : ServerWebSocketContainer(io.undertow.websockets.ServerWebSocketContainer) IndexView(org.jboss.jandex.IndexView) WebSocketDeploymentInfo(io.undertow.websockets.WebSocketDeploymentInfo) ServletContextAttributeBuildItem(io.quarkus.undertow.deployment.ServletContextAttributeBuildItem) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo) BuildStep(io.quarkus.deployment.annotations.BuildStep) Record(io.quarkus.deployment.annotations.Record)

Example 2 with ServerWebSocketContainer

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);
}
Also used : ContextSetupHandler(io.undertow.websockets.util.ContextSetupHandler) Instance(javax.enterprise.inject.Instance) ObjectFactory(io.undertow.websockets.util.ObjectFactory) ObjectFactory(io.undertow.websockets.util.ObjectFactory) ServerWebSocketContainer(io.undertow.websockets.ServerWebSocketContainer) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ObjectIntrospecter(io.undertow.websockets.util.ObjectIntrospecter) WebSocketDeploymentInfo(io.undertow.websockets.WebSocketDeploymentInfo) ObjectHandle(io.undertow.websockets.util.ObjectHandle) EventLoopGroup(io.netty.channel.EventLoopGroup) CurrentIdentityAssociation(io.quarkus.security.identity.CurrentIdentityAssociation) ManagedContext(io.quarkus.arc.ManagedContext) UndertowSession(io.undertow.websockets.UndertowSession) RuntimeValue(io.quarkus.runtime.RuntimeValue) Principal(java.security.Principal)

Aggregations

ServerWebSocketContainer (io.undertow.websockets.ServerWebSocketContainer)2 WebSocketDeploymentInfo (io.undertow.websockets.WebSocketDeploymentInfo)2 EventLoopGroup (io.netty.channel.EventLoopGroup)1 ManagedContext (io.quarkus.arc.ManagedContext)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 Record (io.quarkus.deployment.annotations.Record)1 ReflectiveClassBuildItem (io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem)1 RuntimeValue (io.quarkus.runtime.RuntimeValue)1 CurrentIdentityAssociation (io.quarkus.security.identity.CurrentIdentityAssociation)1 ServletContextAttributeBuildItem (io.quarkus.undertow.deployment.ServletContextAttributeBuildItem)1 UndertowSession (io.undertow.websockets.UndertowSession)1 ContextSetupHandler (io.undertow.websockets.util.ContextSetupHandler)1 ObjectFactory (io.undertow.websockets.util.ObjectFactory)1 ObjectHandle (io.undertow.websockets.util.ObjectHandle)1 ObjectIntrospecter (io.undertow.websockets.util.ObjectIntrospecter)1 Principal (java.security.Principal)1 HashSet (java.util.HashSet)1 Instance (javax.enterprise.inject.Instance)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1