Dropwizard的两中filter

发布于 2017-07-03  309 次阅读


Dropwizard Jersey Filter

方法一 :使用名称绑定

  1. 实现 ContainerRequestFilter创建 filter
    public class DateNotSpecifiedFilter implements ContainerRequestFilter {
        @Override
        public void filter(ContainerRequestContext requestContext) throws IOException {
            String dateHeader = requestContext.getHeaderString(HttpHeaders.DATE);
    
            if (dateHeader == null) {
                Exception cause = new IllegalArgumentException("Date Header was not specified");
                throw new WebApplicationException(cause, Response.Status.BAD_REQUEST);
            }
        }
    }
    
  2. 创建自定义注解@DateRequired
    @Target({ElementType.METHOD, ElementType.TYPE})
    @Retention (RetentionPolicy.RUNTIME)
    public @interface DateRequired {}
    
  3. 使用DynamicFeature动态绑定到资源方法
    @Provider
    public class DateRequiredFeature implements DynamicFeature {
        @Override
        public void configure(ResourceInfo resourceInfo, FeatureContext context) {
            if (resourceInfo.getResourceMethod().getAnnotation(DateRequired.class) != null) {
                context.register(DateNotSpecifiedFilter.class);
            }
        }
    }
    
  4. 在应用程序类中注册该功能
    environment.jersey().register(DateRequiredFeature.class);
    
  5. 在想要执行过滤器的接口上增加 @DateRequired注解

方法二:使用DynamicFeature

  1. 创建使用@NameBinding注释的自定义注释:
    @NameBinding
    @Target({ElementType.METHOD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AnnotationForResourceOne {}
    
  2. 使用注释创建过滤器:
    @Provider
    @PreMatching
    @AnnotationForResourceOne
    public class ResourceOneFilter implements ContainerRequestFilter {
    ...
    }
    
  3. 并用选定的资源方法绑定创建的过滤器:
    @Path("/resources")
    public class Resources {
        @GET
        @Path("/resourceOne")
        @AnnotationForResourceOne
        public String getResourceOne() {...}
    }
    

Dropwizard Servlet Filter

  1. 实现javax.servlet.Filter创建filter
    public class DateNotSpecifiedServletFilter implements javax.servlet.Filter {
        // Other methods in interface omitted for brevity
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            if (request instanceof HttpServletRequest) {
                String dateHeader = ((HttpServletRequest) request).getHeader(HttpHeaders.DATE);
    
                if (dateHeader != null) {
                    chain.doFilter(request, response); // This signals that the request should pass this filter
                } else {
                    HttpServletResponse httpResponse = (HttpServletResponse) response;
                    httpResponse.setStatus(HttpStatus.BAD_REQUEST_400);
                    httpResponse.getWriter().print("Date Header was not specified");
                }
            }
        }
    }
    
  2. 在应用程序中注册改功能
    environment.servlets().addFilter("DateNotSpecifiedServletFilter", new DateNotSpecifiedServletFilter())
                          .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
    


梦想还是要有的,万一实现了呢