Skip to content
Advertisement

ContainerRequestFilter being ignored in my Quarkus extension

I want to implement a custom extension to be used in my services. One of the key features is to filter all the incoming requests:

@Provider
public class RequestInterceptor implements ContainerRequestFilter {

    private static final Logger logger = Logger.getLogger(RequestInterceptor.class.getName());

    @Override
    public void filter(ContainerRequestContext requestContext) {
        logger.info("Testing extension interceptor");
    }
}

And it’s being declared in that way:

class LoggingExtensionProcessor {

    private static final Logger logger = Logger.getLogger(LoggingExtensionProcessor.class.getName());

    private static final String FEATURE = "logging-extension";

    @BuildStep
    FeatureBuildItem feature() {
        return new FeatureBuildItem(FEATURE);
    }

    @BuildStep
    ServletBuildItem createServlet() {
        logger.info("createServlet");
        return ServletBuildItem.builder(FEATURE, GreetingExtensionServlet.class.getName())
                .addMapping("/greeting")
                .build();
    }

    @BuildStep
    AdditionalBeanBuildItem registerAdditionalBeans() {
        logger.info("registerAdditionalBeans");
        return AdditionalBeanBuildItem.builder()
                .setUnremovable()
                .addBeanClass(RequestInterceptor.class)
                .build();

    }

    @BuildStep
    ResteasyJaxrsProviderBuildItem registerConfiguration() {
        logger.info("registerConfiguration");
        return new ResteasyJaxrsProviderBuildItem(RequestInterceptor.class.getName());
    }
}

I also tried adding the Jandex plugin in the runtime pom

            <plugin>
                <groupId>org.jboss.jandex</groupId>
                <artifactId>jandex-maven-plugin</artifactId>
                <version>1.2.2</version>
                <executions>
                    <execution>
                        <id>make-index</id>
                        <goals>
                            <goal>jandex</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

But it’s not being called.

I saw a similar scenario here but the solution doesn’t work for me.

Any suggestion? thanks in advance.

Advertisement

Answer

Finally, I got a solution.

I am using the quarkus-resteasy-reactive-jackson library that provided @ServerRequestFilter and you can build a CustomContainerRequestFilterBuildItem that is used to register classes that are annotated with @ServerRequestFilter:

public class RequestInterceptor {

    private static final Logger logger = Logger.getLogger(RequestInterceptor.class.getName());

    @ServerRequestFilter(preMatching = true)
    public void filter(ContainerRequestContext requestContext) {
        logger.info("Testing extension interceptor");
    }
}
class LoggingExtensionProcessor {

    private static final Logger logger = Logger.getLogger(LoggingExtensionProcessor.class.getName());

    private static final String FEATURE = "logging-extension";

    @BuildStep
    FeatureBuildItem feature() {
        return new FeatureBuildItem(FEATURE);
    }

    @BuildStep
    ServletBuildItem createServlet() {
        logger.info("createServlet");
        return ServletBuildItem.builder(FEATURE, GreetingExtensionServlet.class.getName())
                .addMapping("/greeting")
                .build();
    }

    @BuildStep
    CustomContainerRequestFilterBuildItem buildContainerRequestFilter() {
        return new CustomContainerRequestFilterBuildItem(RequestInterceptor.class.getName());
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement