Search in sources :

Example 1 with ObjectFactory

use of io.undertow.websockets.util.ObjectFactory 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

EventLoopGroup (io.netty.channel.EventLoopGroup)1 ManagedContext (io.quarkus.arc.ManagedContext)1 RuntimeValue (io.quarkus.runtime.RuntimeValue)1 CurrentIdentityAssociation (io.quarkus.security.identity.CurrentIdentityAssociation)1 ServerWebSocketContainer (io.undertow.websockets.ServerWebSocketContainer)1 UndertowSession (io.undertow.websockets.UndertowSession)1 WebSocketDeploymentInfo (io.undertow.websockets.WebSocketDeploymentInfo)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 Instance (javax.enterprise.inject.Instance)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1