package com.opensymphony.webwork.interceptor; import java.util.List; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionContext; import com.opensymphony.xwork.ActionInvocation; import com.opensymphony.xwork.config.entities.ActionConfig; import com.opensymphony.xwork.interceptor.AroundInterceptor; import com.opensymphony.xwork.util.OgnlValueStack; /** * The aim of this Interceptor is to set values in the stack/action based on * cookie name/values we are interested in. * * an Action's cookie expression can be expressed in the form of a map or list * * * In the case of a list, matching cookie names will be matched directly with * setters from the action. ex: { 'pageSize'} If * a cookie exists with the name "pageSize", its value will be set on a action * with a setter of "PageSize" --> setPageSize(int pageSize) * * Using a Map expression allows a level of in-direction. A map entry's key must * match the cookie name, but the value of the map entry is used to look up * which setter to set it to. ex: #{ * 'domainActionPageSize' : 'pageSize'} If a cookie exists with the * name "domainActionPageSize", the value of that cookie will be set on a action * with a setter of "PageSize" --> setPageSize(int pageSize) * * * @author Matthew Payne * */ public class CookieInterceptor extends AroundInterceptor { private static final Log log = LogFactory.getLog(CookieInterceptor.class); private static final String DEFAULT_COOKIE_KEY = "cookies"; String cookieKey = DEFAULT_COOKIE_KEY; public void setCookieKey(String cookieKey) { this.cookieKey = cookieKey; } public void destroy() { } public void init() { } protected void before(ActionInvocation invocation) throws Exception { final OgnlValueStack stack = ActionContext.getContext().getValueStack(); HttpServletRequest request = ServletActionContext.getRequest(); ActionConfig config = invocation.getProxy().getConfig(); // get the action's parameters final Map parameters = config.getParams(); if (parameters.containsKey(cookieKey)) { String cookieExpression = (String) parameters.get(cookieKey); Object obj = stack.findValue(cookieExpression); if (obj != null) { Cookie cookies[] = request.getCookies(); if (obj instanceof List) { List cookieNames = (List) obj; for (int i = 0; i < cookies.length; i++) { String value = cookies[i].getValue(); String name = cookies[i].getName(); if (cookieNames != null && cookieNames.contains(name)) { stack.setValue(name, value); } } } else if (obj instanceof Map) { Map cookieMap = (Map) obj; for (int i = 0; i < cookies.length; i++) { String value = cookies[i].getValue(); String name = cookies[i].getName(); if (cookieMap != null && cookieMap.containsKey(name)) { String alias = (String) cookieMap.get(name); stack.setValue(alias, value); } } } } } } protected void after(ActionInvocation invocation, String result) throws Exception { } }