Integration du Core dans Acegi : authentification via EJB fonctionnelle. \n TODO: corriger les noms de roles (ROLE_AUTHOR ou AUTHOR, il faut decider), mettre en place le role ANONYMOUS quand aucune conference n'est selectionnee, et modifier jsp/secure/userinformation.jsp pour qu'il affiche quelque chose de dynamique ...

This commit is contained in:
2008-01-30 23:15:20 +00:00
parent 31b707615b
commit 62b624fde4
14 changed files with 280 additions and 153 deletions

View File

@@ -68,12 +68,12 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schem
</property> </property>
</bean> </bean>
<bean id="userDetailsService" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"> <bean id="userDetailsService" class="org.yacos.auth.UserDetailsService">
<property name="userProperties"> <!-- <property name="userProperties">-->
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <!-- <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">-->
<property name="location" value="/WEB-INF/users.properties"/> <!-- <property name="location" value="/WEB-INF/users.properties"/>-->
</bean> <!-- </bean>-->
</property> <!-- </property>-->
</bean> </bean>
@@ -137,4 +137,9 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schem
<bean id="loggerListener" class="org.acegisecurity.event.authentication.LoggerListener"/> <bean id="loggerListener" class="org.acegisecurity.event.authentication.LoggerListener"/>
<!--<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">-->
<!-- <property name="persistenceUnitName" value="YACOSCore"/>-->
<!--</bean>-->
</beans> </beans>

View File

@@ -42,6 +42,11 @@
<filter-name>lazyLoadingFilter</filter-name> <filter-name>lazyLoadingFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter> </filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>/onverracaplustard</url-pattern>
</filter-mapping>
<!-- Acegi Security declaration Start --> <!-- Acegi Security declaration Start -->
<filter> <filter>
@@ -88,7 +93,7 @@
<filter> <filter>
<filter-name>SessionService</filter-name> <filter-name>SessionService</filter-name>
<filter-class> <filter-class>
org.yacos.web.system.controller.SessionService org.yacos.web.system.session.SessionServiceFilter
</filter-class> </filter-class>
</filter> </filter>

View File

@@ -84,7 +84,6 @@
<bean id="ListArticleController" <bean id="ListArticleController"
class="org.yacos.web.PCmember.controller.ListArticleController"> class="org.yacos.web.PCmember.controller.ListArticleController">
<property name="articleManager" ref="articleManager" /> <property name="articleManager" ref="articleManager" />
<property name="sessionService" ref="sessionService" />
</bean> </bean>
<bean id="SUserRegistrationController" <bean id="SUserRegistrationController"
@@ -96,7 +95,6 @@
<property name="formView" value="registerUser.htm" /> <property name="formView" value="registerUser.htm" />
<property name="successView" value="listArticle.htm" /> <property name="successView" value="listArticle.htm" />
<property name="userManager" ref="userManager" /> <property name="userManager" ref="userManager" />
<property name="sessionService" ref="sessionService" />
</bean> </bean>
<bean id="SArticleController" <bean id="SArticleController"
@@ -121,7 +119,6 @@
<property name="articleManager" ref="articleManager" /> <property name="articleManager" ref="articleManager" />
<property name="userManager" ref="userManager" /> <property name="userManager" ref="userManager" />
<property name="conferenceManager" ref="conferenceManager" /> <property name="conferenceManager" ref="conferenceManager" />
<property name="sessionService" ref="sessionService" />
</bean> </bean>
<bean id="DispatchArticleController" <bean id="DispatchArticleController"
@@ -135,7 +132,6 @@
<property name="articleManager" ref="articleManager" /> <property name="articleManager" ref="articleManager" />
<property name="userManager" ref="userManager" /> <property name="userManager" ref="userManager" />
<property name="conferenceManager" ref="conferenceManager" /> <property name="conferenceManager" ref="conferenceManager" />
<!-- <property name="sessionService" ref="sessionService" /> -->
</bean> </bean>
<bean id="AddConferenceController5" <bean id="AddConferenceController5"
@@ -218,7 +214,6 @@
<!-- Get chosen conference in session context --> <!-- Get chosen conference in session context -->
<bean id="ChooseConferenceController" <bean id="ChooseConferenceController"
class="org.yacos.web.system.controller.ChooseConferenceController"> class="org.yacos.web.system.controller.ChooseConferenceController">
<property name="sessionService" ref="sessionService" />
</bean> </bean>
@@ -278,7 +273,7 @@
<bean id="sessionService" <bean id="sessionService"
class="org.yacos.web.system.controller.SessionService"> class="org.yacos.web.system.session.SessionService">
<property name="conferenceManager" ref="conferenceManager" /> <property name="conferenceManager" ref="conferenceManager" />
<property name="userManager" ref="userManager" /> <property name="userManager" ref="userManager" />
</bean> </bean>

View File

@@ -0,0 +1,111 @@
package org.yacos.auth;
import java.util.ArrayList;
import java.util.List;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.yacos.core.users.Role;
import org.yacos.core.users.User;
import org.yacos.web.system.session.SessionService;
public class UserDetails implements org.acegisecurity.userdetails.UserDetails {
/**
*
*/
private static final long serialVersionUID = 1L;
private String login;
/**
* Password : used for authentication purpose only, is only writable
*/
private String password;
/**
* First Name of the user
*/
private String firstName;
/**
* Last name of the user
*/
private String lastName;
/**
* Organization or lab the user belongs to
*/
private String organization;
/**
* Active email (has to be used frequently) of the user
*/
private String email;
public UserDetails(User user) {
this.login = user.getLogin();
this.password = user.getPassword();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.organization = user.getOrganization();
this.email = user.getEmail();
}
public GrantedAuthority[] getAuthorities() {
Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
GrantedAuthority[] authorities = null;
if(currentConferenceId != null){
List<Role> rolesList = SessionService.getInstance().getConferenceManager().getRoles(login, currentConferenceId);
if(! rolesList.isEmpty()){
authorities = new GrantedAuthority[rolesList.size()];
}
for(int i=0;i<rolesList.size();i++){
authorities[i] = new GrantedAuthorityImpl(rolesList.get(i).getType().name());
}
}
return (GrantedAuthority[]) authorities;
}
public String getPassword() {
return password;
}
public String getUsername() {
return login;
}
public boolean isAccountNonExpired() {
return true;
}
public boolean isAccountNonLocked() {
return true;
}
public boolean isCredentialsNonExpired() {
return true;
}
public boolean isEnabled() {
return true;
}
public String getLogin() {
return login;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getOrganization() {
return organization;
}
}

View File

@@ -0,0 +1,39 @@
/**
*
*/
package org.yacos.auth;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.yacos.core.users.User;
import org.yacos.web.system.session.SessionService;
/**
* User details service
*
* @author christiancorsano
*
*/
public class UserDetailsService implements
org.acegisecurity.userdetails.UserDetailsService {
/**
*
*/
public UserDetails loadUserByUsername(String login)
throws UsernameNotFoundException, DataAccessException {
User user;
try {
user = SessionService.getInstance().getUserManager().getUser(login);
} catch (Exception e){
throw new DataRetrievalFailureException("Couldn't retrieve the User "+login,e);
}
if(user == null){
throw new UsernameNotFoundException(login);
}
return new UserDetails(user);
}
}

View File

@@ -17,7 +17,7 @@ import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User; import org.yacos.core.users.User;
import org.yacos.web.PCmember.form.FormPreference; import org.yacos.web.PCmember.form.FormPreference;
import org.yacos.web.system.controller.NoConferenceSelectedException; import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.controller.SessionService; import org.yacos.web.system.session.SessionService;
public class ChoosePreferenceController extends SimpleFormController { public class ChoosePreferenceController extends SimpleFormController {
@@ -27,13 +27,12 @@ public class ChoosePreferenceController extends SimpleFormController {
private IArticleManager articleManager; private IArticleManager articleManager;
private IUserManager userManager; private IUserManager userManager;
private IConferenceManager conferenceManager; private IConferenceManager conferenceManager;
private SessionService sessionService;
@Override @Override
protected Object formBackingObject(HttpServletRequest request) protected Object formBackingObject(HttpServletRequest request)
throws Exception { throws Exception {
Integer currentConferenceId = sessionService.getCurrentConferenceId(request); Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
if(currentConferenceId == null){ if(currentConferenceId == null){
throw new NoConferenceSelectedException(); throw new NoConferenceSelectedException();
@@ -112,12 +111,4 @@ public class ChoosePreferenceController extends SimpleFormController {
public void setConferenceManager(IConferenceManager conferenceManager) { public void setConferenceManager(IConferenceManager conferenceManager) {
this.conferenceManager = conferenceManager; this.conferenceManager = conferenceManager;
} }
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
} }

View File

@@ -14,14 +14,13 @@ import org.yacos.core.article.IArticleManager;
import org.yacos.core.exceptions.ConferenceDoesntExistException; import org.yacos.core.exceptions.ConferenceDoesntExistException;
import org.yacos.core.users.User; import org.yacos.core.users.User;
import org.yacos.web.PCmember.form.FormDispatcher; import org.yacos.web.PCmember.form.FormDispatcher;
import org.yacos.web.system.controller.SessionService; import org.yacos.web.system.session.SessionService;
public class DispatchArticleController extends SimpleFormController{ public class DispatchArticleController extends SimpleFormController{
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private IArticleManager articleManager; private IArticleManager articleManager;
private SessionService sessionService;
public IArticleManager getArticleManager() { public IArticleManager getArticleManager() {
return articleManager; return articleManager;
@@ -30,15 +29,6 @@ public class DispatchArticleController extends SimpleFormController{
this.articleManager = articleManager; this.articleManager = articleManager;
} }
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
protected Object formBackingObject(HttpServletRequest request) throws ConferenceDoesntExistException { protected Object formBackingObject(HttpServletRequest request) throws ConferenceDoesntExistException {
logger.info("Returning dispatcher view"); logger.info("Returning dispatcher view");
@@ -49,7 +39,7 @@ public class DispatchArticleController extends SimpleFormController{
members.add(m2); members.add(m2);
// TODO : use the formBackingObject to store these // TODO : use the formBackingObject to store these
request.getSession().setAttribute("members",members); request.getSession().setAttribute("members",members);
Integer currentConferenceId = sessionService.getCurrentConferenceId(request); Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
// TODO : Fix that, we should use the form backing object instead of the session to store the articles // TODO : Fix that, we should use the form backing object instead of the session to store the articles
request.getSession().setAttribute("articles",articleManager.getArticles(currentConferenceId)); request.getSession().setAttribute("articles",articleManager.getArticles(currentConferenceId));
logger.info(articleManager.getArticles(currentConferenceId)); logger.info(articleManager.getArticles(currentConferenceId));
@@ -71,7 +61,7 @@ public class DispatchArticleController extends SimpleFormController{
request.getSession().setAttribute("members",members); request.getSession().setAttribute("members",members);
Integer currentConferenceId = sessionService.getCurrentConferenceId(request); Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles(currentConferenceId) ); return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles(currentConferenceId) );
} }

View File

@@ -102,13 +102,8 @@ public class EvaluationController extends SimpleFormController {
if(toto == null){ if(toto == null){
toto = userManager.addUser("toto", "toto", "Toto", "Toto", "Toto corp.", "toto@totocorp.com"); toto = userManager.addUser("toto", "toto", "Toto", "Toto", "Toto corp.", "toto@totocorp.com");
} }
Report rpt =new Report();
rpt.setArticle(article);
rpt.setCommentAuthor(commentAuthor);
rpt.setCommentPCMember(commentPCMember);
rpt.setRatings(listeRating);
rpt.setReferee(toto);
Report rpt =new Report(commentPCMember,commentAuthor,listeRating,toto,article);
request.getSession().setAttribute("report", rpt); request.getSession().setAttribute("report", rpt);
//Map<String, Report> model1 = new HashMap<String, Report>(); //Map<String, Report> model1 = new HashMap<String, Report>();

View File

@@ -11,7 +11,7 @@ import org.apache.commons.logging.LogFactory;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.yacos.web.system.controller.NoConferenceSelectedException; import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.controller.SessionService; import org.yacos.web.system.session.SessionService;
import org.yacos.core.article.Article; import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager; import org.yacos.core.article.IArticleManager;
import org.yacos.core.exceptions.ConferenceDoesntExistException; import org.yacos.core.exceptions.ConferenceDoesntExistException;
@@ -22,8 +22,6 @@ public class ListArticleController extends SimpleFormController {
private IArticleManager articleManager; private IArticleManager articleManager;
private SessionService sessionService;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoConferenceSelectedException { throws ServletException, IOException, NoConferenceSelectedException {
@@ -31,7 +29,7 @@ public class ListArticleController extends SimpleFormController {
List<Article> listArticle; List<Article> listArticle;
try { try {
listArticle = articleManager.getArticles(sessionService.getCurrentConferenceId(request)); listArticle = articleManager.getArticles(SessionService.getInstance().getCurrentConferenceId());
getServletContext().setAttribute("listArticle", listArticle); getServletContext().setAttribute("listArticle", listArticle);
} catch (ConferenceDoesntExistException e) { } catch (ConferenceDoesntExistException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
@@ -47,11 +45,4 @@ public class ListArticleController extends SimpleFormController {
public void setArticleManager(IArticleManager articleManager) { public void setArticleManager(IArticleManager articleManager) {
this.articleManager = articleManager; this.articleManager = articleManager;
} }
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
} }

View File

@@ -16,7 +16,7 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView; import org.springframework.web.servlet.view.RedirectView;
import org.yacos.web.system.controller.SessionService; import org.yacos.web.system.session.SessionService;
import org.yacos.core.article.Article; import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager; import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.Conference; import org.yacos.core.conferences.Conference;
@@ -29,16 +29,15 @@ public class SArticleController extends SimpleFormController {
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private IArticleManager articleManager; private IArticleManager articleManager;
private SessionService sessionService;
public ModelAndView onSubmit(HttpServletRequest request, public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) HttpServletResponse response, Object command, BindException errors)
throws ServletException, IllegalStateException { throws ServletException, IllegalStateException {
// Fetching conference // Fetching conference
Conference conf = sessionService.getCurrentConference(request); Conference conf = SessionService.getInstance().getCurrentConference();
// Fetching user // Fetching user
User user = sessionService.getCurrentUser(request); User user = SessionService.getInstance().getCurrentUser();
String title = ((FormSubmission) command).getTitle(); String title = ((FormSubmission) command).getTitle();
String theme = ((FormSubmission) command).getTheme(); String theme = ((FormSubmission) command).getTheme();
@@ -95,12 +94,4 @@ public class SArticleController extends SimpleFormController {
public void setArticleManager(IArticleManager articleManager) { public void setArticleManager(IArticleManager articleManager) {
this.articleManager = articleManager; this.articleManager = articleManager;
} }
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
} }

View File

@@ -4,17 +4,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.mvc.Controller;
import org.yacos.web.system.session.SessionService;
public class ChooseConferenceController implements Controller { public class ChooseConferenceController implements Controller {
private SessionService sessionService;
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
public ModelAndView handleRequest(HttpServletRequest request, public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception { HttpServletResponse response) throws Exception {
@@ -23,7 +15,7 @@ public class ChooseConferenceController implements Controller {
int idConf = Integer.parseInt(request.getParameter("idConf")); int idConf = Integer.parseInt(request.getParameter("idConf"));
// Set idConf in session context // Set idConf in session context
sessionService.setCurrentConferenceId(request, idConf); SessionService.getInstance().setCurrentConferenceId(idConf);
return new ModelAndView("main"); return new ModelAndView("main");
} }

View File

@@ -13,6 +13,7 @@ import org.springframework.web.servlet.view.RedirectView;
import org.yacos.core.users.IUserManager; import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User; import org.yacos.core.users.User;
import org.yacos.web.system.form.FormUser; import org.yacos.web.system.form.FormUser;
import org.yacos.web.system.session.SessionService;
/** /**
* FormController class for the user Sign In scenario (user registration) * FormController class for the user Sign In scenario (user registration)
@@ -24,17 +25,6 @@ import org.yacos.web.system.form.FormUser;
*/ */
public class SUserRegistrationController extends SimpleFormController { public class SUserRegistrationController extends SimpleFormController {
private IUserManager userManager; private IUserManager userManager;
private SessionService sessionService;
public SessionService getSessionService() {
return sessionService;
}
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
/** /**
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException) * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
*/ */
@@ -45,7 +35,7 @@ public class SUserRegistrationController extends SimpleFormController {
FormUser userCommand = (FormUser) command; FormUser userCommand = (FormUser) command;
// Tries to retrieve the current user : case of user modification // Tries to retrieve the current user : case of user modification
User user = sessionService.getCurrentUser(request); User user = SessionService.getInstance().getCurrentUser();
if( (userCommand.getIsNew()) && (userManager.getUser(userCommand.getLogin()) != null)){ if( (userCommand.getIsNew()) && (userManager.getUser(userCommand.getLogin()) != null)){
errors.rejectValue("login", "form.register.error.alreadyExists"); errors.rejectValue("login", "form.register.error.alreadyExists");
@@ -87,9 +77,9 @@ public class SUserRegistrationController extends SimpleFormController {
User currentUser = new User(); User currentUser = new User();
user.setIsNew(true); user.setIsNew(true);
// TODO : see how JAAS could change the way of getting the current User // TODO : see how JAAS could change the way of getting the current User
if(sessionService.isUserLogged(request)){ if(SessionService.getInstance().isUserLogged()){
// If he is logged, we use his profile information to fill the form // If he is logged, we use his profile information to fill the form
currentUser = (User) sessionService.getCurrentUser(request); currentUser = (User) SessionService.getInstance().getCurrentUser();
user.setIsNew(false); user.setIsNew(false);
} }

View File

@@ -1,17 +1,9 @@
package org.yacos.web.system.controller; package org.yacos.web.system.session;
import java.io.IOException;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
import org.yacos.core.conferences.Conference; import org.yacos.core.conferences.Conference;
@@ -24,9 +16,11 @@ import org.yacos.core.users.User;
* @author christiancorsano * @author christiancorsano
* *
*/ */
public class SessionService implements Filter { public class SessionService {
private static ServletContext servletContext = null; private static SessionService instance = new SessionService();
private static HttpServletRequest request = null;
protected static ServletContext servletContext = null;
protected static HttpServletRequest request = null;
private IConferenceManager conferenceManager; private IConferenceManager conferenceManager;
private IUserManager userManager; private IUserManager userManager;
@@ -34,14 +28,21 @@ public class SessionService implements Filter {
private User currentUser; private User currentUser;
private Conference currentConference; private Conference currentConference;
public SessionService(){ private SessionService(){
}
public static SessionService getInstance(){
if(instance == null){
instance = new SessionService();
}
return instance;
} }
/** /**
* Init method to be called at filter time * Init method to be called at filter time
* Makes the sessionAttributes available in the request context * Makes the sessionAttributes available in the request context
*/ */
private void init(){ protected void init(){
if(request != null && servletContext != null){ if(request != null && servletContext != null){
try { try {
if(userManager == null || conferenceManager == null){ if(userManager == null || conferenceManager == null){
@@ -51,10 +52,10 @@ public class SessionService implements Filter {
} }
// Put the current conference in the request context to make it accessible in JSPs // Put the current conference in the request context to make it accessible in JSPs
// This avoid to put the whole object in the session // This avoid to put the whole object in the session
request.setAttribute("currentConference", getCurrentConference(request)); request.setAttribute("currentConference", getCurrentConference());
// Put the current conference in the request context to make it accessible in JSPs // Put the current conference in the request context to make it accessible in JSPs
// This avoid to put the whole object in the session // This avoid to put the whole object in the session
request.setAttribute("currentUser", getCurrentUser(request)); request.setAttribute("currentUser", getCurrentUser());
} catch (NamingException e) { } catch (NamingException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@@ -85,7 +86,7 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @return The current conference id * @return The current conference id
*/ */
public Integer getCurrentConferenceId(HttpServletRequest request) { public Integer getCurrentConferenceId() {
// Retrieve the ID from the session // Retrieve the ID from the session
Integer confId = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId"); Integer confId = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId");
return confId; return confId;
@@ -96,7 +97,7 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @param currentConferenceId The id to set as the new current conference * @param currentConferenceId The id to set as the new current conference
*/ */
public void setCurrentConferenceId(HttpServletRequest request,Integer currentConferenceId) { public void setCurrentConferenceId(Integer currentConferenceId) {
WebUtils.setSessionAttribute(request, "currentConferenceId", currentConferenceId); WebUtils.setSessionAttribute(request, "currentConferenceId", currentConferenceId);
currentConference = conferenceManager.getConference(currentConferenceId); currentConference = conferenceManager.getConference(currentConferenceId);
request.setAttribute("currentConference", currentConference); request.setAttribute("currentConference", currentConference);
@@ -107,8 +108,8 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @param c The conference object to set as current * @param c The conference object to set as current
*/ */
public void setCurrentConference(HttpServletRequest request, Conference c){ public void setCurrentConference(Conference c){
setCurrentConferenceId(request, c.getId() ); setCurrentConferenceId( c.getId() );
currentConference = c; currentConference = c;
} }
@@ -117,8 +118,8 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @return The conference object * @return The conference object
*/ */
public Conference getCurrentConference(HttpServletRequest request){ public Conference getCurrentConference(){
Integer id = getCurrentConferenceId(request); Integer id = getCurrentConferenceId();
// The currentConference can be out to date : force the update // The currentConference can be out to date : force the update
if(currentConference != null && id != currentConference.getId()){ if(currentConference != null && id != currentConference.getId()){
@@ -136,9 +137,9 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @param currentUser The new current user to set * @param currentUser The new current user to set
*/ */
public void setCurrentUser(HttpServletRequest request,User currentUser) { public void setCurrentUser(User currentUser) {
this.currentUser = currentUser; this.currentUser = currentUser;
this.setCurrentUserLogin(request,currentUser.getLogin()); this.setCurrentUserLogin(currentUser.getLogin());
} }
/** /**
@@ -146,7 +147,7 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @param login The new current user login * @param login The new current user login
*/ */
public void setCurrentUserLogin(HttpServletRequest request, String login) { public void setCurrentUserLogin(String login) {
WebUtils.setSessionAttribute(request, "currentUserLogin", login); WebUtils.setSessionAttribute(request, "currentUserLogin", login);
currentUser = userManager.getUser(login); currentUser = userManager.getUser(login);
request.setAttribute("currentUser", currentUser); request.setAttribute("currentUser", currentUser);
@@ -159,7 +160,7 @@ public class SessionService implements Filter {
* @param request The current HTTP request object, necessary for session operations * @param request The current HTTP request object, necessary for session operations
* @return The current user login * @return The current user login
*/ */
public String getCurrentUserLogin(HttpServletRequest request){ public String getCurrentUserLogin(){
// Retrieve the login from the session // Retrieve the login from the session
String login = (String) WebUtils.getSessionAttribute(request, "currentUserLogin"); String login = (String) WebUtils.getSessionAttribute(request, "currentUserLogin");
return login; return login;
@@ -167,12 +168,11 @@ public class SessionService implements Filter {
/** /**
* Get (if necessary) and returns the current user object * Get (if necessary) and returns the current user object
* @param request The current HTTP request object, necessary for session operations
* @return The current User * @return The current User
*/ */
public User getCurrentUser(HttpServletRequest request) { public User getCurrentUser() {
// Retrieve the login from the session // Retrieve the login from the session
String login = this.getCurrentUserLogin(request); String login = this.getCurrentUserLogin();
// The user can be out to date : force the update // The user can be out to date : force the update
if(currentUser != null && (! currentUser.getLogin().equals(login)) ){ if(currentUser != null && (! currentUser.getLogin().equals(login)) ){
@@ -180,7 +180,7 @@ public class SessionService implements Filter {
} }
// Check if the user object is already in cache // Check if the user object is already in cache
if(currentUser == null || (this.getCurrentUser(request).getLogin().equals(login)) ){ if(currentUser == null || (this.getCurrentUser().getLogin().equals(login)) ){
// Checks login for validity // Checks login for validity
if(login == null || login.equals("")){ if(login == null || login.equals("")){
currentUser = null; currentUser = null;
@@ -194,42 +194,27 @@ public class SessionService implements Filter {
/** /**
* Returns whether the user is logged (is there a current User for this session) * Returns whether the user is logged (is there a current User for this session)
* @param request The current HTTP request object, necessary for session operations
* @return true if there's a current user in the session, false otherwise * @return true if there's a current user in the session, false otherwise
*/ */
public boolean isUserLogged(HttpServletRequest request){ public boolean isUserLogged(){
return getCurrentUser(request) != null; return getCurrentUser() != null;
}
/* ========================= */
/* = Filter implementation = */
/* ========================= */
/**
* Inits the filter, and set the servletContext for this session service
*/
public void init(FilterConfig config) throws ServletException {
SessionService.servletContext = config.getServletContext();
} }
/** /**
* Fetch the current session data so that every JSP has access to it * Puts an object in the session
* @param name The name of the attribute in the session
* @param attribute The attribute object
*/ */
public void doFilter(ServletRequest request, ServletResponse response, public void setSessionAttribute(String name, Object attribute){
FilterChain chain) throws IOException, ServletException { WebUtils.setSessionAttribute(request, name, attribute);
if(request instanceof HttpServletRequest){
SessionService.request = (HttpServletRequest) request;
init();
}
chain.doFilter(request, response);
} }
/** /**
* Clean the static attributes * Retrieves an object from the Session
* @param name The name of the attribute to retrieve
* @return The attribute object
*/ */
public void destroy() { public Object getSessionAttribute(String name){
SessionService.servletContext = null; return WebUtils.getSessionAttribute(request, name);
SessionService.request = null;
} }
} }

View File

@@ -0,0 +1,47 @@
package org.yacos.web.system.session;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
* Filter to initialize the SessionService singleton
* @author christiancorsano
*
*/
public class SessionServiceFilter implements Filter {
/**
* Inits the filter, and set the servletContext for this session service
*/
public void init(FilterConfig config) throws ServletException {
SessionService.servletContext = config.getServletContext();
}
/**
* Puts the request object into SessionService and tells it to init
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request instanceof HttpServletRequest){
SessionService.request = (HttpServletRequest) request;
SessionService.getInstance().init();
}
// Continue the filter chain
chain.doFilter(request, response);
}
/**
* Clean the static attributes
*/
public void destroy() {
SessionService.servletContext = null;
SessionService.request = null;
}
}