package com.javaeye.jert.interceptor;
import com.javaeye.core.Application;
import com.javaeye.jert.action.UserPrefsAware;
import com.javaeye.jert.domain.user.UserPrefs;
import com.javaeye.jert.service.UserPrefsService;
import com.javaeye.user.util.RemoteUser;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
/**
* sitemesh can not get session thru ActionContext.getContext().getSession(),
* have to use this interceptor to inject UserPrefs.
* TODO submit an issue to webwork jira
*
* @author Quake Wang
* @since 2005-1-23
* @version $Revision: 1.2 $
*/
public class UserPrefsInterceptor extends AroundInterceptor {
public static final String USER_PREFS_SESSION_KEY = "_USER_PREFS_SESSION_KEY_";
protected void before(ActionInvocation invocation) {
Action action = invocation.getAction();
if (action instanceof UserPrefsAware) {
UserPrefs userPrefs = (UserPrefs) invocation.getInvocationContext().getSession().get(USER_PREFS_SESSION_KEY);
if (userPrefs == null) {
UserPrefsService service = (UserPrefsService) Application.getInstance().getContainer().getComponent(UserPrefsService.class);
userPrefs = service.loadUserPrefs(RemoteUser.get().getName());
invocation.getInvocationContext().getSession().put(USER_PREFS_SESSION_KEY, userPrefs);
}
((UserPrefsAware) action).setUserPrefs(userPrefs);
}
}
protected void after(ActionInvocation arg0, String arg1) throws Exception {
}
}