Question

I am using Guice to setup a filter to process all requests on AppEngine. Guice setup is as follows:

public void configureServlets() {
    filter("*").through(RedirectFilter.class);
}

The filter:

@Singleton
public class RedirectFilter implements Filter {
    private static final Logger logger = Logger.getLogger(RedirectFilter.class.getName());

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

        URL url = new URL(request.getRequestURL().toString());
        String domain = url.getHost();

        logger.info("host: " + domain + " path: " + url.getPath());

        if (!domain.startsWith("www")) {
            if (domain.startsWith("en")) {
                response.sendRedirect("http://www.mysite.com/en" + url.getPath());
            }
        }
        chain.doFilter(req, res);
    }
    ...
}

However, the filter is not being called for any request.

Any ideas on why it might not be called?

UPDATE: The filter seems to be called with URLs such as http://en.mysite.com/xxx but not with http://en.mysite.com

Was it helpful?

Solution

From AppEngine documentation on static files:

There are many cases where you want to serve static files directly to the web browser. Images, CSS stylesheets, JavaScript code, movies and Flash animations are all typically served directly to the browser. For efficiency, App Engine serves static files from separate servers than those that invoke servlets.

By default, App Engine makes all files in the WAR available as static files except JSPs and files in WEB-INF/. Any request for a URL whose path matches a static file serves the file directly to the browser—even if the path also matches a servlet or filter mapping. You can configure which files App Engine treats as static files using the appengine-web.xml file.

More info on this topic at this link.

To have your filter called when static HTML files are requested place the following in your appengine-web.xml file:

<static-files>
  <include path="/**.png" />
  <!-- other includes go here -->
  <exclude path="/**.html" />
</static-files>

More info on static file configuration for AppEngine at this link.

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