0

having a bit of difficulty getting my url to write properly. I just found out about UrlRewriteFilter and it seems to be exactly what I need, since it's kind of difficult to rewrite urls using Java with GAE (it's disgustingly simple using python/go and app.yaml, but with Java it's a major pain in the ass for some reason, and I have to use Java). Here's what I have —

<rule>
    <from>^/test.html$</from>
    <to>/test</to>
</rule>

I just want people to be able to type mysite.com/test instead of mysite.com/test.html. Am I going about this the right way? When I use this code above I get a HTTP ERROR 500 caused by java.lang.StackOverflowError.

Any help would be appreciated! Thanks.

konqi
  • 5,137
  • 3
  • 34
  • 52
shan
  • 3,035
  • 5
  • 34
  • 50

1 Answers1

0

I tried to use the UrlRewriteFilter by Paul Tuckey during my research for your other question. That didn't work well for me, for two reasons.

  • I didn't like the idea of filtering all requests with the UrlRewriteFilter because all requests will have to be routed through app engine instances, and therefor cutting out the content delivery network
  • I could be wrong about this but if i remember correctly, during my quick excourse into the UrlRewriteFilter i found that the filter forwards internally to the static content, which doesn't work because the appengine instance itself is not set up to serve static content - only the content delivery network does that.

What i did recently looks like this:

public class RewriteFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        log.info("Proxy Servlet for: " + httpServletRequest.getRequestURI());

        byte[] buffer = new byte[1024];
        try (InputStream is = new FileInputStream("index.html"); OutputStream os = httpServletResponse.getOutputStream()) {
            int read = is.read(buffer);
            while (read > 0) {
                os.write(buffer, 0, read);
                read = is.read(buffer);
            }

        }

        // chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}

While being really ugly, this works, even if just for the index.html page (well i could change that with filter init params and defining multiple filters).

But why does it work where the UrlRewriteFilter doesn't?

The answer is quite simple. This filter servlet doesn't forward internally to static content but serves the content itself. Since this filter will only become active for urls i specify by <url-pattern> below <filter-mapping> in the web.xml, i can live with the slight performance dip when serving through this filter. If anyone found a better way to do this, please leave a comment.

Community
  • 1
  • 1
konqi
  • 5,137
  • 3
  • 34
  • 52