Access PortletSession (ApplicationScope) in Liferay Filter Hook

415 views Asked by At

I need to add a filter on some page's that checks if some Session-attributes are set.

What I want to achieve is: User tries to directly navigate to a page. On that page, there are 3 Portlets that need SessionVariables. Those are set by the previous page. So, if those variables are not available, there has to happen a redirect to the previous page.

So I was looking to add a filter hook, so the class implements Filter

public class SampleFilter implements Filter {

    @Override
    public void destroy() {
        /* Destroy method*/
    }

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

        /* I need to access the PortletSession here! */

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) {
        /*Method to init filter..*/
    }

}

Is it possible to check a variable in the doFilter() Method, that I've set with the session.setAttribute("name", value, PortletSession.APPLICATION_SCOPE);?

1

There are 1 answers

0
Kornelito Benito On

Ok, I found this:

Because the Session Attributes where scoped "APPLICATION_SCOPE", It is possible to access them through the HttpSession: Example

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpSession session = httpServletRequest.getSession();
        log.debug(session.getAttribute("applicationScopedName"));

        filterChain.doFilter(servletRequest, servletResponse);
}

That's all!