package com.opensymphony.webwork.interceptor;

import com.opensymphony.webwork.WebWorkStatics;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;

import javax.servlet.http.HttpServletRequest;

/**
 * PrincipalInterceptor gives access to principal and roles information from action, without
 * making action class tied to HttpServlerRequest.
 *
 * Date: 2004.9.4
 * Time: 12.28.02
 * @author $Author: Remigijus Bauzys
 * @version 1.0
 */
public class PrincipalInterceptor extends AroundInterceptor implements WebWorkStatics {

    protected void after(ActionInvocation dispatcher, String result) throws Exception {
    }

    protected void before(ActionInvocation invocation) throws Exception {
        Action action = invocation.getAction();
        ActionContext context = ActionContext.getContext();

        if (action instanceof PrincipalAware) {
            HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
            ((PrincipalAware) action).setPrincipalProxy(new PrincipalProxy(request));
        }
    }
}
