Quarkus 静态内容响应过滤器

Quarkus static content response filter

有没有办法向 META-INF/resources 提供的静态资源添加 filter/interceptor?

我似乎已经尝试了所有可能的选项:@ServerResponseFilterContainerResponseFilterWriterInterceptor 但是所有这些函数仅在 @Path@Route 时被调用...

有没有类似于@RouteFilter但要回复的东西?

只有这些可用:

public interface ClientRequestFilter {
    void filter(ClientRequestContext requestContext) throws IOException;
}

public interface ClientResponseFilter {
    void filter(ClientRequestContext requestContext,
        ClientResponseContext responseContext) throws IOException;
}

public interface ContainerRequestFilter {
    void filter(ContainerRequestContext requestContext) throws IOException;
}

public interface ContainerResponseFilter {
    void filter(ContainerRequestContext requestContext,
    ContainerResponseContext responseContext) throws IOException;
}

public interface ReaderInterceptor {
    Object aroundReadFrom(ReaderInterceptorContext context)
        throws java.io.IOException, javax.ws.rs.WebApplicationException;
}

public interface WriterInterceptor {
    void aroundWriteTo(WriterInterceptorContext context)
        throws java.io.IOException, javax.ws.rs.WebApplicationException;
}

您可以尝试使用 ClientResponseFilter 看看:

In the Client API, a ClientRequestFilter is executed as part of the invocation pipeline, before the HTTP request is delivered to the network.

ClientResponseFilter is executed upon receiving a server response, before control is returned to the application.

https://quarkus.io/specs/jaxrs/2.1/index.html#filters_and_interceptors

目前似乎没有什么可以 built-in 修改静态内容的响应,但没有什么可以阻止您以动态方式提供内容:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

@Path("/")
@Produces(MediaType.TEXT_HTML)
public class FrontendController {

    @GET
    public Response index() throws IOException {
        try (var index = getClass().getResourceAsStream(FrontendConstants.INDEX_RESOURCE)) {
            if (index == null) {
                throw new IOException("index.html file does not exist");
            }

            //modify index

            return Response
                .ok(index)
                .cacheControl(FrontendConstants.NO_CACHE)
                .build();
        }
    }

    @GET
    @Path("/{fileName:.+}")
    public Response staticFile(@PathParam("fileName") String fileName) throws IOException {
        try (var file = FrontendController.class.getResourceAsStream(FrontendConstants.FRONTEND_DIR + "/" + fileName)) {
            if (file == null) {
                return index();
            }

            var contentType = URLConnection.guessContentTypeFromStream(file);

            return Response
                .ok(
                    new String(file.readAllBytes(), StandardCharsets.UTF_8),
                    contentType == null ? MediaType.TEXT_PLAIN : contentType
                )
                .cacheControl(FrontendConstants.NO_CACHE)
                .build();
        }
    }
}