4

I made a small webapp and I want to add credits note at the bottom of the index page. It would be easy to remove the note from the html page in the WAR file. So I thought I could modify resource 'index.html' this way:

public class MainPageTransformer implements ResourceTransformer {
    @Override
    public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
        String html = IOUtils.toString(resource.getInputStream(), UTF_8);
        html = html.replace("</body>", "<div style=\"position: absolute; bottom: 5px;\">Autor</div>\n</body>");
        return new ByteArrayResource(html.getBytes());
    }
}

... and:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/client/index.html")
                .addResourceLocations("classpath:/static/client")
                .resourceChain(false)
                .addTransformer(new MainPageTransformer());
    }
}

After the MainPageTransformer.transform method is executed it throws exception:

java.io.FileNotFoundException: Byte array resource [resource loaded from byte array] cannot be resolved to absolute file path
at org.springframework.core.io.AbstractResource.getFile(AbstractResource.java:114)
at org.springframework.core.io.AbstractResource.getFileForLastModifiedCheck(AbstractResource.java:169)
at org.springframework.core.io.AbstractResource.lastModified(AbstractResource.java:153)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:240)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)

What do I do wrong? Is there a better way to achieve my goal? Thanks a lot!

1 Answer 1

10

I ran into this today, try using a org.springframework.web.servlet.resource.TransformedResource.

public class MainPageTransformer implements ResourceTransformer {
 @Override
 public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
   String html = IOUtils.toString(resource.getInputStream(), UTF_8);
   html = html.replace("</body>", "<div style=\"position: absolute; bottom: 5px;\">Autor</div>\n</body>");
   return new TransformedResource(resource, html.getBytes());
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing how hard this was to find haha. I guess it's not a particularly common use case. Thanks for sharing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.