Question

I am building a Dropwizard app to replace a legacy webserver, due to a lot of links pointing at this server, I have to be backwards compatible and support all kinds of resources that the legacy server used to serve.

The problem is that I have a bunch of static JS files being served from the same directory/path as the services themselves. Apparently you can't serve both types (service and static) from a single directory. so if I add the static bundle (now located in the resources directory), like so:

bootstrap.addBundle(new AssetsBundle("/javascript", "/media")); 

The dynamic services (served from /media/) start returning 404 if I comment it, the dynamic services work fine.

As I said, I can't change any of the links, static or dynamic... Any suggestions?

Update:

Resolved, here is a working example inspired by @airborn 's answer, this question which also pointed me to this module, which claims to solve this problem generically.

This example will apply a filter to ALL incoming requests, and allow you to redirect to wherever you want (similar to .htaccess functionality on apache).

Place the following line in the run function your main dropwizard class (the one that extends **Service<AppConfiguration> **

environment.addFilter(new UrlRewriteFilter(),"/*");

Place this class somewhere accessible to the above class

@WebFilter
public class UrlRewriteFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        //
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String requestURI = request.getRequestURI();
        // Place Redirection logic here (else continues the filter chain).
        if (redirectConditionTrue)
            String newURI = ""//New URl to do to;
            req.getRequestDispatcher(newURI).forward(req, res);
        } else {
            chain.doFilter(req, res);
        }
    }

    @Override
    public void destroy() {
        //
    }
}
Was it helpful?

Solution

Using Environment in your Service class, you should be able to add a filter to Jetty. In this proxy filter, you should be able to detect those js files and redirect them to the new location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top