Jersey InjectableProvider如何依赖于资源的AbstractMethod?

时间:2013-10-29 01:14:11

标签: java dependency-injection annotations jersey jersey-1.0

如果ResourceFilterFactory上有注释,我会ContainerRequestFilter返回AbstractMethod

过滤器读取cookie或API密钥并检查它是否仍然有效,并在请求上设置属性。检查很昂贵,所以我只希望在注释出现时发生。

我想将结果对象注入我的资源,但似乎在应用ResourceFilterFactory过滤器之前进行了注入。如果我将过滤器作为ResourceConfig添加到PROPERTY_CONTAINER_REQUEST_FILTERS,那么它会在注入之前发生,但它无法访问AbstractMethod

如何缩小间隙,以便应用过滤器并仅在方法具有注释时注入字段?

我的代码看起来像这样。在AuthFilter之后调用UserIdProvider,因此c.getProperties().get(USER_ID_PROPERTY)始终为null

public class AuthFilter implements ContainerRequestFilter {
  public ContainerRequest filter(ContainerRequest request) {
    Integer userId = …; // expensive lookup
    request.getProperties().set(USER_ID_PROPERTY, userId);
    return request;
  }
}

public class AuthFilterFactory implements ResourceFilterFactory {
  // …
  public List<ResourceFilter> create(AbstractMethod am) {
    if (am.isAnnotationPresent(RequireAuth.class) || am.getResource().isAnnotationPresent(RequireAuth.class)) {
      return Collections.singletonList(new ResourceFilter() {
        public ContainerRequestFilter getRequestFilter() {
          return authFilter;
        }
      });
    }
    else {
      return null
    }
  }
}

InjectableProvider:

@Provider
public class UserIdProvider implements InjectableProvider<UserId, Parameter> {
  public ComponentScope getScope() {
    return ComponentScope.PerRequest;
  }

  public Injectable<? extends Object> getInjectable(ComponentContext ic, UserId a, final Parameter t) {
    return new AbstractHttpContextInjectable<Integer>() {
      public Integer getValue(HttpContext c) {
        return (Integer) c.getProperties().get(USER_ID_PROPERTY);
      }
    }
  }
}

资源:

@Path("/user")
public class UserResource {
  @UserId
  Integer userId;

  // userId should only be set on this method
  @GET
  @RequireAuth
  public UserDetails get() {
    // …
  }

  // the auth filter should not be called for this method
  @POST
  @Path("/login")
  public String login() {
    // …
  }
}

0 个答案:

没有答案