1

Is there a way to add a filter/interceptor to static resources served from META-INF/resources?

I seem have tried all the possible options: @ServerResponseFilter, ContainerResponseFilter,WriterInterceptor however all these functions are called only for @Path or @Route...

Is there anything similar to @RouteFilter but for response?

Sam Ivichuk
  • 999
  • 1
  • 10
  • 22
  • But this would essentially make those resources non-static (as it would let you transform them on the fly). If you only want to influence cache-control response headers, i think it would be easier to configure them at a reverse-proxy (ingress) level – Michail Alexakis May 17 '22 at 11:36
  • @MichailAlexakis yes, that makes sense, i'm referencing them as `static` only because quarkus calls them as so but what i need is basically a transformer for `META-INF/resources`, sorry for the confusion – Sam Ivichuk May 17 '22 at 11:47

2 Answers2

1

Those are the only ones available:

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;
}

You could try with ClientResponseFilter and see:

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

JCompetence
  • 6,997
  • 3
  • 19
  • 26
0

There seem to be nothing built-in to modify the response of a static content for now but nothing stops you from serving content in a dynamic way:

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();
        }
    }
}
Sam Ivichuk
  • 999
  • 1
  • 10
  • 22