All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.reactive.server.handlers.PerRequestInstanceHandler Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package org.jboss.resteasy.reactive.server.handlers;

import java.util.function.Function;

import jakarta.ws.rs.container.CompletionCallback;

import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.injection.ResteasyReactiveInjectionTarget;
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler;
import org.jboss.resteasy.reactive.spi.BeanFactory;

public class PerRequestInstanceHandler implements ServerRestHandler {

    /**
     * CDI Manages the lifecycle. If this is a per request resource then this will be a client proxy
     *
     */
    private final BeanFactory factory;
    private final Function clientProxyUnwrapper;

    public PerRequestInstanceHandler(BeanFactory factory, Function clientProxyUnwrapper) {
        this.factory = factory;
        this.clientProxyUnwrapper = clientProxyUnwrapper;
    }

    @Override
    public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
        requestContext.requireCDIRequestScope();
        BeanFactory.BeanInstance instance = factory.createInstance();
        requestContext.setEndpointInstance(instance.getInstance());
        Object unwrapped = instance.getInstance();
        if (clientProxyUnwrapper != null) {
            unwrapped = clientProxyUnwrapper.apply(unwrapped);
        }
        ((ResteasyReactiveInjectionTarget) unwrapped)
                .__quarkus_rest_inject(requestContext);
        requestContext.registerCompletionCallback(new CompletionCallback() {
            @Override
            public void onComplete(Throwable throwable) {
                instance.close();
            }
        });
    }
}