Search in sources :

Example 1 with ContainerRequestFilter

use of jakarta.ws.rs.container.ContainerRequestFilter in project resteasy by resteasy.

the class ContextParameterInjectionTest method createProxy.

private Object createProxy() {
    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getAttribute(ResteasyDeployment.class.getName())).thenReturn(null);
    ConfigurationBootstrap configBootstrap = new ListenerBootstrap(mockServletContext);
    ResteasyContext.pushContext(ResteasyDeployment.class, configBootstrap.createDeployment());
    try {
        final Class<ContainerRequestFilter> filterClass = ContainerRequestFilter.class;
        final ContainerRequestFilter coolInstance = new CoolFilter();
        ResteasyProviderFactory mockFactory = mock(ResteasyProviderFactory.class);
        when(mockFactory.getContextData(filterClass, filterClass, null, false)).thenReturn(coolInstance);
        ResteasyProviderFactory.setInstance(mockFactory);
        ContextParameterInjector cpi = new ContextParameterInjector(null, filterClass, filterClass, null, mockFactory);
        return cpi.createProxy();
    } finally {
        ResteasyContext.removeContextDataLevel();
    }
}
Also used : ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) ListenerBootstrap(org.jboss.resteasy.plugins.server.servlet.ListenerBootstrap) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) ServletContext(jakarta.servlet.ServletContext) ConfigurationBootstrap(org.jboss.resteasy.plugins.server.servlet.ConfigurationBootstrap) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory)

Example 2 with ContainerRequestFilter

use of jakarta.ws.rs.container.ContainerRequestFilter in project resteasy by resteasy.

the class ServerHelper method processProviderInstanceContracts.

protected void processProviderInstanceContracts(Object provider, Map<Class<?>, Integer> contracts, Integer priorityOverride, boolean builtIn, Map<Class<?>, Integer> newContracts) {
    ConstrainedTo constrainedTo = (ConstrainedTo) provider.getClass().getAnnotation(ConstrainedTo.class);
    if (constrainedTo != null && constrainedTo.value() != RuntimeType.SERVER)
        return;
    super.processProviderInstanceContracts(provider, contracts, priorityOverride, builtIn, newContracts);
    if (Utils.isA(provider, ContainerRequestFilter.class, contracts)) {
        int priority = Utils.getPriority(priorityOverride, contracts, ContainerRequestFilter.class, provider.getClass());
        JaxrsInterceptorRegistry<ContainerRequestFilter> registry = getRequestFiltersForWrite();
        registry.registerSingleton((ContainerRequestFilter) provider, priority);
        attachedRequestFilters = false;
        requestFilters = registry;
        newContracts.put(ContainerRequestFilter.class, priority);
    }
    if (Utils.isA(provider, ContainerResponseFilter.class, contracts)) {
        int priority = Utils.getPriority(priorityOverride, contracts, ContainerResponseFilter.class, provider.getClass());
        JaxrsInterceptorRegistry<ContainerResponseFilter> registry = getResponseFiltersForWrite();
        registry.registerSingleton((ContainerResponseFilter) provider, priority);
        attachedResponseFilters = false;
        responseFilters = registry;
        newContracts.put(ContainerResponseFilter.class, priority);
    }
    if (Utils.isA(provider, AsyncResponseProvider.class, contracts)) {
        try {
            addAsyncResponseProvider((AsyncResponseProvider) provider, provider.getClass());
            int priority = Utils.getPriority(priorityOverride, contracts, AsyncResponseProvider.class, provider.getClass());
            newContracts.put(AsyncResponseProvider.class, priority);
        } catch (Exception e) {
            throw new RuntimeException(Messages.MESSAGES.unableToInstantiateAsyncResponseProvider(), e);
        }
    }
    if (Utils.isA(provider, AsyncStreamProvider.class, contracts)) {
        try {
            addAsyncStreamProvider((AsyncStreamProvider) provider, provider.getClass());
            int priority = Utils.getPriority(priorityOverride, contracts, AsyncStreamProvider.class, provider.getClass());
            newContracts.put(AsyncStreamProvider.class, priority);
        } catch (Exception e) {
            throw new RuntimeException(Messages.MESSAGES.unableToInstantiateAsyncStreamProvider(), e);
        }
    }
    if (Utils.isA(provider, ExceptionMapper.class, contracts)) {
        try {
            addExceptionMapper((ExceptionMapper) provider, provider.getClass(), builtIn);
            int priority = Utils.getPriority(priorityOverride, contracts, ExceptionMapper.class, provider.getClass());
            newContracts.put(ExceptionMapper.class, priority);
        } catch (Exception e) {
            throw new RuntimeException(Messages.MESSAGES.unableToInstantiateExceptionMapper(), e);
        }
    }
}
Also used : ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) ConstrainedTo(jakarta.ws.rs.ConstrainedTo) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter)

Example 3 with ContainerRequestFilter

use of jakarta.ws.rs.container.ContainerRequestFilter in project minijax by minijax.

the class MinijaxApplication method runRequestFilters.

private void runRequestFilters(final MinijaxRequestContext context) throws IOException {
    for (final Class<? extends ContainerRequestFilter> filterClass : requestFilters) {
        final ContainerRequestFilter filter = context.getResource(filterClass);
        filter.filter(context);
    }
}
Also used : ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter)

Example 4 with ContainerRequestFilter

use of jakarta.ws.rs.container.ContainerRequestFilter in project tomee by apache.

the class JAXRSUtils method runContainerRequestFilters.

public static boolean runContainerRequestFilters(ServerProviderFactory pf, Message m, boolean preMatch, Set<String> names) throws IOException {
    List<ProviderInfo<ContainerRequestFilter>> containerFilters = preMatch ? pf.getPreMatchContainerRequestFilters() : pf.getPostMatchContainerRequestFilters(names);
    if (!containerFilters.isEmpty()) {
        ContainerRequestContext context = new ContainerRequestContextImpl(m, preMatch, false);
        for (ProviderInfo<ContainerRequestFilter> filter : containerFilters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, m);
            filter.getProvider().filter(context);
            Response response = m.getExchange().get(Response.class);
            if (response != null) {
                setMessageContentType(m, response);
                return true;
            }
        }
    }
    return false;
}
Also used : AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Response(jakarta.ws.rs.core.Response) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter)

Example 5 with ContainerRequestFilter

use of jakarta.ws.rs.container.ContainerRequestFilter in project resteasy by resteasy.

the class ProviderFactoryTest method testRegisterProviderInstancePriorityContainerRequestFilter.

/**
 * @tpTestDetails Regression test for JBEAP-4706
 *                Test whether the priority is supplied to the container request filter registry.
 * @tpSince RESTEasy 3.0.17
 */
@Test
public void testRegisterProviderInstancePriorityContainerRequestFilter() throws Exception {
    ContainerRequestFilter requestFilter = new ContainerRequestFilter() {

        public void filter(ContainerRequestContext requestContext) {
        }
    };
    this.testRegisterProviderInstancePriority(requestFilter, factory.getContainerRequestFilterRegistry());
}
Also used : ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Aggregations

ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)8 Response (jakarta.ws.rs.core.Response)3 IOException (java.io.IOException)3 BuiltResponse (org.jboss.resteasy.specimpl.BuiltResponse)3 RESTEasyTracingLogger (org.jboss.resteasy.tracing.RESTEasyTracingLogger)3 NotFoundException (jakarta.ws.rs.NotFoundException)2 ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)2 CompletionException (java.util.concurrent.CompletionException)2 PreMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext)2 HttpRequestPreprocessor (org.jboss.resteasy.spi.HttpRequestPreprocessor)2 HttpResponse (org.jboss.resteasy.spi.HttpResponse)2 InternalServerErrorException (org.jboss.resteasy.spi.InternalServerErrorException)2 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)2 UnhandledException (org.jboss.resteasy.spi.UnhandledException)2 ServletContext (jakarta.servlet.ServletContext)1 ConstrainedTo (jakarta.ws.rs.ConstrainedTo)1 AsyncResponse (jakarta.ws.rs.container.AsyncResponse)1 ContainerResponseFilter (jakarta.ws.rs.container.ContainerResponseFilter)1 ResourceContext (jakarta.ws.rs.container.ResourceContext)1 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)1