I am using dropwizard. I created a filter for authenticating the user. Here in the code sessionRepository is the one that do all the talking with session table in database. The function works fine when used inside of resources, but fails when used with ContainerRequestFilter. Giving the error:

java.lang.IllegalStateException: No thread bound entity manager.

@Slf4j
@javax.ws.rs.ext.Provider
@Authenticate
public class TestFilter implements ContainerRequestFilter {

  public static final String USER_ID = "X_USER_ID";

  @Context
  private ResourceInfo resourceInfo;

  private Provider<SessionRepository> sessionRepository;

  private Provider<DeviceRepository> deviceProvider;

  @Inject
  public TestFilter(Provider<SessionRepository> sessionRepository,
      Provider<DeviceRepository> deviceProvider) {
    this.sessionRepository = sessionRepository;
    this.deviceProvider = deviceProvider;
  }

  @Override
  public void filter(ContainerRequestContext request) {
    Optional<Session> session = sessionRepository.get().getActiveSessionBySessionId("sample-session");
    log.info("Inside filter");
  }
}

0 Answers