I guess I am dealing with a bug in Glassfish 4; but I am not sure. Basically I am trying to inject a service into a ContainerRequestFilter; but I am getting an exception while trying. I can do injection in resources where I make rest calls, but I can't use injection for filters. There is a filed bug in glassfish jira: https://java.net/jira/browse/GLASSFISH-20597. I tried the workaround mentioned in there but it didn't solve my problem.
Here is the exception I get:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Authenticator,parent=SecurityInterceptor,qualifiers
Here is the code that I am working with, do you have any idea what I am missing here?
@Provider
@PreMatching
public class SecurityInterceptor implements ContainerRequestFilter {
@Inject
private Authenticator authenticator; // can't inject this service here, but I can inject this to RequestScoped Resources
private static final Logger LOG = Logger.getLogger(SecurityInterceptor.class.getName());
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (!requestContext.getMethod().equals("OPTIONS")) {
String path = OyumBuFunctions.normalizeString(requestContext.getUriInfo().getPath());
String authToken = requestContext.getHeaderString(OyumbuRestHeaders.AUTH_TOKEN_HEADER);
if (!authenticator.isAuthTokenValid(authToken)) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
else{
LOG.log(Level.INFO, "authenticated");
}
}
}
}