自定义servlet过滤器谷歌端点

时间:2013-12-17 15:22:55

标签: java google-app-engine java-ee

我尝试调用我的过滤器,但它似乎被忽略了。所以我有两个问题: 1.具有google端点的servlete过滤器的配置如何? 2.使用slf4j进行日志记录(jdk日志记录为后端)应该在过滤器类中正常工作 - 我猜?

web.xml中的配置:

<filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>myCoolFilter</filter-name>
    <filter-class>com.example.MyCoolFilterClass</filter-class>
</filter>
<filter-mapping>
    <filter-name>myCoolFilter</filter-name>
    <url-pattern>/_ah/spi/endpoint-api-name/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>ServiceServlce</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.MyCoolEndpoint</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>ServiceServlce</servlet-name>
    <url-pattern>/_ah/spi/endpoint-api-name/*</url-pattern>
</servlet-mapping>

1 个答案:

答案 0 :(得分:1)

我找到的唯一解决方案是将过滤器添加为通用过滤器(/ *)并处理检查请求URI:

HttpServletRequest httpRequest = (HttpServletRequest) req;
String requestURI = httpRequest.getRequestURI();
log.info("request uri " + requestURI);
if (!requestURI.toLowerCase().contains("endpoint-api-name")) {
  chain.doFilter(req, resp);
  return;
}

值得注意的是,我还尝试在过滤器声明中使用servlet-name,但没有结果。我使过滤器工作的唯一方法是通过/ * url-pattern。