0

I am running a Micronaut app in an AWS Lambda as explained here : https://guides.micronaut.io/latest/mn-application-aws-lambda-java11.html

How am I supposed to implement a Request/Response filter ? I want a functionality like a good old Servlet filter. I read this documentation : https://docs.micronaut.io/latest/guide/#filters. Unfortunately it only consider the case where Micronaut is running in a Netty Http server context where we need to implement reactive way of doing to "not block the underlying Netty event loop". But here, I am running in a Lambda. Each of the incoming HTTP request will be handled by one Lambda instance and its Thread. I don't care about this reactive way of doing here. The need of a thread pool is replaced by Lambda.

I don't know what to do with this Publisher<MutableHttpResponse<?>>. I want the HTTP response directly.

@Filter("/**")
public class SecurityFilter implements HttpServerFilter {

Logger logger = LoggerFactory.getLogger(SecurityFilter.class);

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
    logger.debug("request = " + request);
    // Do something with HTTP request

    // Would like to call chain to go to the next Filter or to the Controller if no other filter
    // and have access to the HTTP Response like in a good old Servlet filter

    // What am I supposed to do with this response publisher. This app is running in AWS Lambda.
    // I don't have a netty http server. I don't need to protect "the underlying Netty event loop"
    // just want to have access to my Http Response here and be able to modify it (add headers for example)
    // in a synchronous manner without all this reactive stuff that I don't care about in this Lambda context

    Publisher<MutableHttpResponse<?>> responsePublisher = chain.proceed(request);
    responsePublisher.subscribe(new Subscriber<MutableHttpResponse<?>>() {
        @Override
        public void onSubscribe(Subscription s) {
            logger.debug("A");
        }

        @Override
        public void onNext(MutableHttpResponse<?> mutableHttpResponse) {
            logger.debug("response");
            logger.debug(mutableHttpResponse + "");
        }

        @Override
        public void onError(Throwable t) {
            logger.debug("B");
        }

        @Override
        public void onComplete() {
            logger.debug("C");
        }
    });

    return responsePublisher;
}

}

Comencau
  • 1,084
  • 15
  • 35
  • please help :-) – Comencau Aug 27 '22 at 12:36
  • The underlying compute will be AWS Lambda, will Micronaut still not bring up Netty on invoke? Are you running a micronaut function or an app? – dmahapatro Aug 29 '22 at 22:41
  • Reading this : https://micronaut-projects.github.io/micronaut-aws/latest/guide/#lambda. – Comencau Aug 31 '22 at 09:22
  • I have API Gateway with proxy (all HTTP requests coming to API Gateway are forwarded to the same Lambda Function). I want the Lambda Function to behave as a Micronaut application and use the [at]Get and [at]Post annotation defining my endpoints in my Controllers. My expectation would be that the [Micronaut to AWS Lambda] integration gets rid of Netty and just use the thread of Lambda instance. I don't see the point of dealing with the underlying Netty event loop and threads created with Reactive libs to handle HTTP requests. The JVM of a Lambda instance will always deal with only 1 request. – Comencau Aug 31 '22 at 09:32

0 Answers0