Patrik
Patrik

Reputation: 1

Manually called Servlet-Filter - injected EJB always null (NullPointerException)

I do have a problem with ServletFiltering and EJB Injection. I've configured a ServletFilter and included it to my web.xml.

Filter-Class:

package at.dot.web.rest.common.utils;
public class AuthRequestFilter implements Filter {

@EJB
private RequestValidator rv;

@Override
public void doFilter(ServletRequest servletRequest,
        ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {

  AuthRequestWrapper wr = new AuthRequestWrapper((HttpServletRequest) servletRequest);
  if (rv.isRequestAccepted(wr)) {
     filterChain.doFilter(wr, servletResponse);
  } 

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void destroy() {
}
}

web.xml:

<filter>
  <filter-name>AuthRequestFilter</filter-name>
  <filter-class>at.dot.web.rest.common.utils.AuthRequestFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>AuthRequestFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Everthing looks great and works without any problem. I now do wanted to make the whole thing a little much more flexible and came to this article: How to add filters to servlet without modifying web.xml I added that GodFilter including the FilterChain and the Pattern as suggested in the answere. Thats working pretty well. Filter is called as defined. What my problem is: The EJB (rv) is not injected. It's always null for me. I'm pretty sure I'm missing any (simple) issue - but nevertheless I do not find the mistake.

Any ideas?

Thanks in advance

Upvotes: 0

Views: 1041

Answers (1)

Patrik
Patrik

Reputation: 1

So i fixed the issue by myself:

@Stateless
@LocalBean
@RequestScoped
public class AuthRequestFilter implements Filter { .. }

@RequestScoped
public class MasterFilter implements Filter { 
   @EJB(beanName = "AuthRequestFilter")
   private AuthRequestFilter arf;

   ..
}

Of course new AuthRequestFilter() does not inject the EJB - I do have to inject the filter.

Upvotes: 0

Related Questions