You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ekoje ekoje <jo...@gmail.com> on 2007/02/14 13:02:40 UTC

[S2] Filter Cookie

Hi Guys,

I would like to check a cookie every time a user try to access my website.
So, I've created an interceptor, it work fine but it didn't work if the user
ask for a JSP page. The interceptor was never called.
So, I've created a filter who check all cookies at every request to my
server. Unfortunetly it doesn't work too, every time my request parameters
and cookies are empty even if I'm sure that I can find a cookie.

Is there anything in Struts 2 who remove all data in the request object ???

Please find the code:
public final class LoginFilter implements Filter {
    private final static String FILTER_APPLIED =
"_clickstream_filter_applied";

    private FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
            throws IOException, ServletException {
        if (request.getAttribute (FILTER_APPLIED) == null) {
            request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
            Cookie[] cookies = ((HttpServletRequest) request).getCookies();

            if (cookies != null) {
                System.out.println(cookies.length);

                for (int i = 0; i < cookies.length; i++) {
                    System.out.println(cookies[i].getName());
                    if (cookies != null &&
cookies[i].getName().equals("ebespoke"))
{
                        System.out.println("cookie found");
                        if
(AccountManager.isCookieLogin(cookies[i].getValue()))
{
                            System.out.println("cookie alive");
                            String[] values =
StringUtils.split(cookies[i].getComment(),
"|");
                            ((HttpServletRequest)
request).getSession().setAttribute("userD",
                                    AccountManager.getUserDetails
(values[0]));
                        }
                    }
                }
            }
        }
        chain.doFilter(request, response);
    }
}

Thanks for your help