I am trying to delegate a query filter to a CDI bean. But Glassfish 4.0 gives me an exception when I call RestURL.
Filter definition
@Provider @Authenticated public class AuthenticationFilter implements ContainerRequestFilter{ @Inject private AuthenticationService authenticationService; @Override public void filter(ContainerRequestContext containerRequestContext) throws IOException { String token = containerRequestContext.getHeaderString("x-authentication-token"); if (!authenticationService.isAuthenticated(token)) { containerRequestContext.abortWith(Response.serverError().entity("No authentication").build()); } } }
Name NameBinding so that we can mark specific resource URLs.
@NameBinding @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(value = RetentionPolicy.RUNTIME) public @interface Authenticated { }
And the definition of CDI bean.
@ApplicationScoped public class AuthenticationService { public boolean isAuthenticated(String token) { boolean result = false; if (token != null && token.length() > 0) { .... } return result; } }
And I have a bean.xml file in the WEB-INF directory
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all"> </beans>
Thanks Rudy
source share