Monday, November 27, 2006

Acegi and Webwork Interceptor

I am currently using Webwork 2.2.4+Spring 2.0+Acegi 1.0.2.The authentication structure from Acegi is awesome.

I was trying to retrieve user details such as display name,company name and email address from database based on user name.Therefore I wrote a UserContextInterceptor that retrieve user's record from database and store to user's session.

//UserContextInterceptor.java

public class UserContextInterceptor extends AroundInterceptor {
...//some other methods

@Override
protected void before(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub

UserContext userContext=null;
Map session=ServletActionContext.getContext().getSession();
if(!session.containsKey(USER_CONTEXT)){
userContext=new UserContext();
userContext.setUser(userManager.getUsersByUsername(
ServletActionContext.getRequest().getUserPrincipal().getName()
));

ServletActionContext.getContext().getSession().put(USER_CONTEXT, userContext);
}else{
userContext=(UserContext)session.get(USER_CONTEXT);;
}
if (invocation.getAction() instanceof UserContextAware){
((UserContextAware) invocation.getAction()).setUserContext(userContext);
}
}
}

No comments: