Subversion Repositories bacoAlunos

Rev

Rev 1814 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.web.filters;

import org.apache.log4j.Logger;
import pt.estgp.estgweb.domain.UserSessionImpl;
import pt.estgp.estgweb.utils.Globals;
import pt.estgp.estgweb.web.UserSessionProxy;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 *
 * @author  Jorge Machado
 */


public class AuthenticationAllFilter extends UserFilter {

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AuthenticationAllFilter.class);


    public AuthenticationAllFilter()
    {
    }

    static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsContains;
    static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsStartsWith;
    static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsEqual;

    public void init(FilterConfig config)
    {
        allowedPatternsContains = config.getInitParameter("allowedPatternsContains").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(",");
        allowedPatternsStartsWith = config.getInitParameter("allowedPatternsStartsWith").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(",");
        allowedPatternsEqual = config.getInitParameter("allowedPatternsEqual").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(",");;
    }

    static 1.5.0/docs/api/java/lang/String.html">String linkSubmitLogin = Globals.SYSTEM_REDIRECTIONS_POLICY_AUTHENTICATION_ACTION;
    /**
     *
     * @param request The servlet request we are processing
     * @param chain The filter chain we are processing
     *
     * @exception java.io.IOException if an input/output error occurs
     * @exception javax.servlet.ServletException if a servlet error occurs
     */

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
    {
        //String linkLogin = Globals.SYSTEM_REDIRECTIONS_POLICY_INDEX_WELCOME;

        HttpServletRequest hrequest = (HttpServletRequest) request;

        UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSessionFromRequest(hrequest);
        if(userSession != null && userSession.isAuthenticated())
        {
            chain.doFilter(request,response);
            return;
        }



        if(hrequest.getServletPath().equals("")
                || hrequest.getServletPath().equals("/")
                || hrequest.getServletPath().startsWith(linkSubmitLogin))
        {
            chain.doFilter(request,response);
            return;
        }

        if(FilterUtils.checkIfIsIgnoredExtension(request, hrequest))
        {
            chain.doFilter(request, response);
            return;
        }

        for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsEqual)
        {
            if(hrequest.getServletPath().equals(patternEqual))
            {
                chain.doFilter(request,response);
                return;
            }
        }

        for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsContains)
        {
            if(hrequest.getServletPath().contains(patternEqual))
            {
                chain.doFilter(request,response);
                return;
            }
        }

        for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsStartsWith)
        {
            if(hrequest.getServletPath().startsWith(patternEqual))
            {
                chain.doFilter(request,response);
                return;
            }
        }


        logger.warn("URL nao permitido, enviando filtragem para a Super Classe UserFilter");
        super.doFilter(request,response,chain);
    }




    public void destroy()
    {
        // Nothing
    }

}