1

Well, after my older questions:

Are there some issue at inserting some check into template?

and

Is it a good idea to filter inside a JSF template?

I'm writing the filter, but sometimes (not even at the same point) the application returns NulPointerException at this line:



        chain.doFilter(req, res);

and I don't understand why... The full code for my filter is:



    @WebFilter(filterName = "RolesFilter", urlPatterns = {"/*"},
     initParams = {
      @WebInitParam(name = "param", value = "value")})

    public class RolesFilter implements Filter {

        @Override
        public void init(FilterConfig config) throws ServletException {
            // NOOP.
        }

        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            HttpSession session = request.getSession(false);
            UserBean user = (session.getAttribute("userBean") != null) ? (UserBean) session.getAttribute("userBean") : null;
                String uri=request.getRequestURI();

    if(!uri.endsWith("index.xhtml") && !uri.endsWith(".css") && !uri.endsWith(".js")  && !uri.endsWith("template.xhtml"))
    {
            if (user == null || user.getCognome() == null) 
                response.sendRedirect("/cdg/faces/index.xhtml");
            else {
                String app=uri.substring(0,uri.lastIndexOf("/"));
                app=app.substring(app.lastIndexOf("/")+1,app.length());
                HashMap> privilegi= (HashMap)user.getPrivilegi();
                Long idAppl=((AbstractController)session.getAttribute(app+"Controller")).getIdApplicazione();
                Short[] privPoss=((AbstractController)session.getAttribute(app+"Controller")).getPrivilegiPossibili();
    //--- if privPoss has only 3 value the application is a classic application with 2 roles so I can use a generic check, if the number is different I must do a specific check.
                            if(privilegi.get(idAppl)!=null && privPoss.length==3)
                            {
         if((privilegi.get(idAppl)).get(privPoss[0])==null && (uri.endsWith("Edit.xhtml") || uri.endsWith("Create.xhtml")))
                response.sendRedirect("/cdg/faces/index.xhtml");
            } else {
                                if(idAppl==35)
                                {
                                    ProgettiController pc=(session.getAttribute("progettiController")!=null) ? (ProgettiController)session.getAttribute("progettiController") : null;
         if(privilegi.get(idAppl).get(privPoss[1])==null && uri.endsWith("progetti/Create.xhtml"))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
         else if(uri.endsWith("progetti/Edit.xhtml")
     && (!privilegi.get(idAppl).get(privPoss[5]).equals("0") && !UserBean.isInString(pc.getSelected().getIdAzienda().getId()+"",privilegi.get(idAppl).get(privPoss[5]),"#")))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
         else if((uri.endsWith("attivita/Create.xhtml") || uri.endsWith("attivita/Create.xhtml"))
     &&(!pc.getSelected().getIdResponsabile().equals(user.getUtente())) )
                response.sendRedirect"/cdg/faces/index.xhtml"); 
         else if(uri.endsWith("attivita/Edit.xhtml")
     && (!privilegi.get(idAppl).get(privPoss[2]).equals("0") && !UserBean.isInString(pc.getSelected().getIdAzienda().getId()+"",privilegi.get(idAppl).get(privPoss[2]),"#")))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
         else if((uri.endsWith("progettiQualifica/Create.xhtml") || uri.endsWith("progettiQualifica/Create.xhtml"))
     && (!pc.getSelected().getIdResponsabile().equals(user.getUtente())))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
         else if((uri.endsWith("progettiSchede/Create.xhtml") || uri.endsWith("attivita/Create.xhtml"))
     && (!pc.getSelected().getIdResponsabile().equals(user.getUtente())))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
    }
                                else if(idAppl==2)
                                {
                                    RisorseController rc=(session.getAttribute("risorseController")!=null) ? (RisorseController)session.getAttribute("risorseController") : null;
         if(privilegi.get(idAppl).get(privPoss[0])==null && (uri.endsWith("Create.xhtml") || uri.endsWith("Edit.xhtml")))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
         else if(uri.endsWith("Edit.xhtml")
     && (!rc.getSelected().equals(user.getUtente())))
                response.sendRedirect("/cdg/faces/index.xhtml"); 
                                }
                                }
                            }
        } 
                chain.doFilter(req, res);

    }

        @Override
        public void destroy() {
            // NOOP.
        }

    }

Where could the problem be?

well, before this answer I delete a row from my code and now the filter seems work correctly, could this row cause the problem?



    System.out.println("The uri is:"+request.getRequestURI());

it was after the first if in doFilter method... Thank you for your help!

Community
  • 1
  • 1
Filippo1980
  • 2,745
  • 5
  • 30
  • 44

1 Answers1

0

You're probably calling the doFilter(ServletRequest, ServletResponse, FilterChain) method with null value as the third parameter.

Did you check that?

KARASZI István
  • 30,900
  • 8
  • 101
  • 128