Skip to content

Allow early decoration of request and response in WebFlux #25633

Closed
@fred84

Description

@fred84

Current behaviour

WebHttpHandlerBuilder.build() creates instance of HttpWebHandlerAdapter (https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java#L356).

However, HttpWebHandlerAdapter allows to override some of it's methods (e.g. createExchange).

Proposed behaviour

Supply HttpWebHandlerAdapterFactory to WebHttpHandlerBuilder.build and create HttpWebHandlerAdapter via this factory. Naive approach could be as simple as:

    public HttpHandler build() {

        WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
        decorated = new ExceptionHandlingWebHandler(decorated,  this.exceptionHandlers);

        HttpWebHandlerAdapter adapted = httpHandlerCreator.apply(decorated);
        ...
        return adapted;
    }

Motivation

Sometimes we need to decorate ServerHttpResponse and ServerHttpRequest (e.g. body logging/caching). Doing this inside a WebFilter is not sufficient in case of thrown exception flow. We have to do it "earlier" than ExceptionHandlingWebHandler

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions