Corrigé headerchoose.jsp

Mis en place SessionService pour la mise en session et la recuperation automatique de la conference courante
SessionService est un filtre maintenant, etape suivante : en faire une classe statique pour en faciliter l'acces, et supprimer les parametres HttpRequest dans les methodes
Modifié les classes ayant besoin de la conference courante pour qu'elles utilisent SessionService
This commit is contained in:
2008-01-28 09:22:35 +00:00
parent 88e40466ac
commit 6d3bdf02dc
14 changed files with 216 additions and 48 deletions

View File

@@ -12,8 +12,9 @@
<div id="rightchoice"> <div id="rightchoice">
<c:choose> <c:choose>
<c:when test="${idCurrentConference!=null}"> <c:when test="${currentConferenceId != null}">
La conf<6E>rence courante est : ${idCurrentConference}.<br /> La conf<6E>rence courante est :<br />
<span class="currentConferenceTitle">${currentConference.title}</span>
<a href="<c:url value="listConference.htm"/>"><fmt:message key="conference.linkMenu.change" /></a> <a href="<c:url value="listConference.htm"/>"><fmt:message key="conference.linkMenu.change" /></a>
</c:when> </c:when>
<c:otherwise> <c:otherwise>

View File

@@ -37,6 +37,12 @@
<url-pattern>/dwr/*</url-pattern> <url-pattern>/dwr/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<!-- OpenSessionInView for JPA -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<!-- SiteMesh Integration declaration Start --> <!-- SiteMesh Integration declaration Start -->
<filter> <filter>
<filter-name>sitemesh</filter-name> <filter-name>sitemesh</filter-name>
@@ -77,6 +83,19 @@
</context-param> </context-param>
<!-- Acegi Security declaration End --> <!-- Acegi Security declaration End -->
<!-- SessionService Integration declaration Start -->
<filter>
<filter-name>SessionService</filter-name>
<filter-class>
org.yacos.web.system.controller.SessionService
</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionService</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- SessionService Integration declaration End -->
</web-app> </web-app>

View File

@@ -84,6 +84,7 @@
<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"
@@ -120,6 +121,7 @@
<property name="articleManager" ref="articleManager" /> <property name="articleManager" ref="articleManager" />
<property name="usersManager" ref="usersManager" /> <property name="usersManager" ref="usersManager" />
<property name="conferenceManager" ref="conferenceManager" /> <property name="conferenceManager" ref="conferenceManager" />
<property name="sessionService" ref="sessionService"/>
</bean> </bean>
<bean id="DispatchArticleController" <bean id="DispatchArticleController"
@@ -131,6 +133,7 @@
<property name="formView" value="dispatchArticle" /> <property name="formView" value="dispatchArticle" />
<property name="successView" value="dispatchArticle.htm" /> <property name="successView" value="dispatchArticle.htm" />
<property name="articleManager" ref="articleManager" /> <property name="articleManager" ref="articleManager" />
<property name="sessionService" ref="sessionService"/>
</bean> </bean>
<bean id="AddConferenceController5" <bean id="AddConferenceController5"

View File

@@ -3,7 +3,6 @@ package org.yacos.web.PCmember.controller;
import java.util.*; import java.util.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@@ -12,7 +11,6 @@ import org.springframework.validation.BindException;
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.springframework.web.util.WebUtils;
import org.yacos.core.article.*; import org.yacos.core.article.*;
import org.yacos.core.conferences.IConferenceManager; import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.users.IUserManager; import org.yacos.core.users.IUserManager;
@@ -29,12 +27,14 @@ 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 = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId"); Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
if(currentConferenceId == null){ if(currentConferenceId == null){
throw new NoConferenceSelectedException(); throw new NoConferenceSelectedException();
} }
@@ -112,4 +112,12 @@ 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

@@ -1,8 +1,6 @@
package org.yacos.web.PCmember.controller; package org.yacos.web.PCmember.controller;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -11,9 +9,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
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.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager; import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.Conference;
public class CreateReportController extends SimpleFormController { public class CreateReportController extends SimpleFormController {

View File

@@ -2,7 +2,6 @@ package org.yacos.web.PCmember.controller;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@@ -10,18 +9,19 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
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.util.WebUtils;
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;
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;
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,6 +30,14 @@ 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");
@@ -41,7 +49,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 = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId"); Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
// 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));
@@ -63,7 +71,8 @@ public class DispatchArticleController extends SimpleFormController{
request.getSession().setAttribute("members",members); request.getSession().setAttribute("members",members);
Integer currentConferenceId = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId"); Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles(currentConferenceId) ); return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles(currentConferenceId) );
} }

View File

@@ -3,7 +3,6 @@ package org.yacos.web.PCmember.controller;
import java.util.*; import java.util.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;

View File

@@ -2,7 +2,6 @@ package org.yacos.web.PCmember.controller;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -32,7 +31,7 @@ public class ListArticleController extends SimpleFormController {
List<Article> listArticle; List<Article> listArticle;
try { try {
listArticle = articleManager.getArticles((Integer) WebUtils.getSessionAttribute(request, "currentConferenceId")); listArticle = articleManager.getArticles(sessionService.getCurrentConferenceId(request));
getServletContext().setAttribute("listArticle", listArticle); getServletContext().setAttribute("listArticle", listArticle);
} catch (ConferenceDoesntExistException e) { } catch (ConferenceDoesntExistException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());

View File

@@ -2,8 +2,6 @@ package org.yacos.web.author.controller;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.yacos.core.conferences.Conference;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -20,7 +18,7 @@ public class MainController extends SimpleFormController {
logger.info("Returning main view"); logger.info("Returning main view");
/*ajout en session d'un idconf et loginUser;*/ // FIXME : this is no longer necessary
request.getSession().setAttribute("user","toto"); request.getSession().setAttribute("user","toto");
request.getSession().setAttribute("conference",1); request.getSession().setAttribute("conference",1);
//FIXME //FIXME

View File

@@ -6,11 +6,9 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
@@ -18,9 +16,6 @@ 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.springframework.web.util.WebUtils;
import org.yacos.core.article.*;
import org.yacos.web.author.form.*;
import org.yacos.web.system.controller.SessionService; import org.yacos.web.system.controller.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;
@@ -34,15 +29,16 @@ 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 id // Fetching conference
Conference conf = (Conference)request.getSession().getAttribute("conference"); Conference conf = sessionService.getCurrentConference(request);
// Fetching user login // Fetching user
User user = (User)request.getSession().getAttribute("user"); User user = sessionService.getCurrentUser(request);
String title = ((FormSubmission) command).getTitle(); String title = ((FormSubmission) command).getTitle();
String theme = ((FormSubmission) command).getTheme(); String theme = ((FormSubmission) command).getTheme();

View File

@@ -1,14 +1,10 @@
package org.yacos.web.chairman.controller; package org.yacos.web.chairman.controller;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.naming.Context;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
@@ -17,9 +13,10 @@ import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException; import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.mvc.AbstractWizardFormController; import org.springframework.web.servlet.mvc.AbstractWizardFormController;
import org.yacos.core.conferences.IConferenceManager; import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.Criterion;
import org.yacos.web.chairman.form.FormConference; import org.yacos.web.chairman.form.FormConference;
// TODO : Clean this class
public class AddConferenceController extends AbstractWizardFormController { public class AddConferenceController extends AbstractWizardFormController {
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());

View File

@@ -2,10 +2,8 @@ package org.yacos.web.system.controller;
import javax.servlet.http.HttpServletRequest; 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.springframework.web.util.WebUtils;
public class ChooseConferenceController implements Controller { public class ChooseConferenceController implements Controller {
private SessionService sessionService; private SessionService sessionService;

View File

@@ -45,7 +45,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 = (User) request.getSession().getAttribute("currentUser"); User user = sessionService.getCurrentUser(request);
if( (userCommand.getIsNew()) && (usersManager.getUser(userCommand.getLogin()) != null)){ if( (userCommand.getIsNew()) && (usersManager.getUser(userCommand.getLogin()) != null)){
errors.rejectValue("login", "form.register.error.alreadyExists"); errors.rejectValue("login", "form.register.error.alreadyExists");

View File

@@ -1,5 +1,17 @@
package org.yacos.web.system.controller; package org.yacos.web.system.controller;
import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
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;
@@ -12,7 +24,10 @@ import org.yacos.core.users.User;
* @author christiancorsano * @author christiancorsano
* *
*/ */
public class SessionService { public class SessionService implements Filter {
private static ServletContext servletContext = null;
private static HttpServletRequest request = null;
private IConferenceManager conferenceManager; private IConferenceManager conferenceManager;
private IUserManager userManager; private IUserManager userManager;
@@ -20,7 +35,31 @@ public class SessionService {
private Conference currentConference; private Conference currentConference;
public SessionService(){ public SessionService(){
}
/**
* Init method to be called at filter time
* Makes the sessionAttributes available in the request context
*/
private void init(){
if(request != null && servletContext != null){
try {
if(userManager == null || conferenceManager == null){
Context context = new InitialContext();
userManager = (IUserManager) context.lookup("UserManagerBean/remote");
conferenceManager = (IConferenceManager) context.lookup("ConferenceManagerBean/remote");
}
// Put the current conference in the request context to make it accessible in JSPs
// This avoid to put the whole object in the session
request.setAttribute("currentConference", getCurrentConference(request));
// Put the current conference in the request context to make it accessible in JSPs
// This avoid to put the whole object in the session
request.setAttribute("currentUser", getCurrentUser(request));
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
public void setConferenceManager(IConferenceManager conferenceManager) { public void setConferenceManager(IConferenceManager conferenceManager) {
@@ -39,52 +78,158 @@ public class SessionService {
return userManager; return userManager;
} }
/**
* Gets the current conference Id
* Additionally, put the current conference object in the request context
* with the key "currentConference".
* @param request The current HTTP request object, necessary for session operations
* @return The current conference id
*/
public Integer getCurrentConferenceId(HttpServletRequest request) { public Integer getCurrentConferenceId(HttpServletRequest request) {
return (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId"); // Retrieve the ID from the session
Integer confId = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId");
return confId;
} }
/**
* Sets the current conference id
* @param request The current HTTP request object, necessary for session operations
* @param currentConferenceId The id to set as the new current conference
*/
public void setCurrentConferenceId(HttpServletRequest request,Integer currentConferenceId) { public void setCurrentConferenceId(HttpServletRequest request,Integer currentConferenceId) {
WebUtils.setSessionAttribute(request, "currentConferenceId", currentConferenceId); WebUtils.setSessionAttribute(request, "currentConferenceId", currentConferenceId);
currentConference = conferenceManager.getConference(currentConferenceId);
request.setAttribute("currentConference", currentConference);
} }
/**
* Sets the current conference using a conference object
* @param request The current HTTP request object, necessary for session operations
* @param c The conference object to set as current
*/
public void setCurrentConference(HttpServletRequest request, Conference c){ public void setCurrentConference(HttpServletRequest request, Conference c){
currentConference = c;
setCurrentConferenceId(request, c.getId() ); setCurrentConferenceId(request, c.getId() );
currentConference = c;
} }
/**
* Gets (if necessary) and returns the current conference of the session
* @param request The current HTTP request object, necessary for session operations
* @return The conference object
*/
public Conference getCurrentConference(HttpServletRequest request){ public Conference getCurrentConference(HttpServletRequest request){
if(currentConference == null){ Integer id = getCurrentConferenceId(request);
currentConference = conferenceManager.getConference(getCurrentConferenceId(request));
// The currentConference can be out to date : force the update
if(currentConference != null && id != currentConference.getId()){
currentConference = null;
}
if( currentConference == null && id != null ){
currentConference = conferenceManager.getConference(id);
} }
return currentConference; return currentConference;
} }
/**
* Sets the current user by providing the user object
* @param request The current HTTP request object, necessary for session operations
* @param currentUser The new current user to set
*/
public void setCurrentUser(HttpServletRequest request,User currentUser) { public void setCurrentUser(HttpServletRequest request,User currentUser) {
this.currentUser = currentUser; this.currentUser = currentUser;
this.setCurrentUserLogin(request,currentUser.getLogin()); this.setCurrentUserLogin(request,currentUser.getLogin());
} }
/**
* Sets the current user login
* @param request The current HTTP request object, necessary for session operations
* @param login The new current user login
*/
public void setCurrentUserLogin(HttpServletRequest request, String login) { public void setCurrentUserLogin(HttpServletRequest request, String login) {
WebUtils.setSessionAttribute(request, "currentUserLogin", login); WebUtils.setSessionAttribute(request, "currentUserLogin", login);
currentUser = userManager.getUser(login);
request.setAttribute("currentUser", currentUser);
} }
/**
* Gets the current user login from the session
* Additionally, puts the current User object in the request context
* with the key "currentUser"
* @param request The current HTTP request object, necessary for session operations
* @return The current user login
*/
public String getCurrentUserLogin(HttpServletRequest request){ public String getCurrentUserLogin(HttpServletRequest request){
return (String)WebUtils.getSessionAttribute(request, "currentUserLogin"); // Retrieve the login from the session
String login = (String) WebUtils.getSessionAttribute(request, "currentUserLogin");
return login;
} }
/**
* Get (if necessary) and returns the current user object
* @param request The current HTTP request object, necessary for session operations
* @return The current User
*/
public User getCurrentUser(HttpServletRequest request) { public User getCurrentUser(HttpServletRequest request) {
if(currentUser == null){ // Retrieve the login from the session
String login = this.getCurrentUserLogin(request); String login = this.getCurrentUserLogin(request);
// The user can be out to date : force the update
if(currentUser != null && (! currentUser.getLogin().equals(login)) ){
currentUser = null;
}
// Check if the user object is already in cache
if(currentUser == null || (this.getCurrentUser(request).getLogin().equals(login)) ){
// Checks login for validity
if(login == null || login.equals("")){ if(login == null || login.equals("")){
currentUser = null; currentUser = null;
} else { } else {
// Puts the user object in the cache object
currentUser = userManager.getUser(login); currentUser = userManager.getUser(login);
} }
} }
return currentUser; return currentUser;
} }
/**
* 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
*/
public boolean isUserLogged(HttpServletRequest request){ public boolean isUserLogged(HttpServletRequest request){
return getCurrentUser(request) != null; return getCurrentUser(request) != 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
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request instanceof HttpServletRequest){
SessionService.request = (HttpServletRequest) request;
init();
}
chain.doFilter(request, response);
}
/**
* Clean the static attributes
*/
public void destroy() {
SessionService.servletContext = null;
SessionService.request = null;
}
} }