View Javadoc

1   package hu.elte.tribus.sf;
2   
3   import java.io.IOException;
4   
5   import hu.elte.tribus.interfaces.UserTracker;
6   import hu.elte.tribus.util.AuthUtil;
7   
8   import javax.servlet.Filter;
9   import javax.servlet.FilterChain;
10  import javax.servlet.FilterConfig;
11  import javax.servlet.ServletContext;
12  import javax.servlet.ServletException;
13  import javax.servlet.ServletRequest;
14  import javax.servlet.ServletResponse;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpSession;
17  
18  import org.springframework.context.ApplicationContext;
19  import org.springframework.transaction.PlatformTransactionManager;
20  import org.springframework.transaction.TransactionStatus;
21  import org.springframework.transaction.support.TransactionCallback;
22  import org.springframework.transaction.support.TransactionTemplate;
23  import org.springframework.web.context.support.WebApplicationContextUtils;
24  
25  public class SessionFilter implements Filter {
26  
27  	private ServletContext context = null;
28  
29  	public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException
30  	{
31  		final ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
32  		final UserTracker tracker = (UserTracker) applicationContext.getBean("userTracker");
33  		PlatformTransactionManager transactionManager = (PlatformTransactionManager) applicationContext.getBean("txManager");
34  		new TransactionTemplate(transactionManager).execute(new TransactionCallback(){
35  
36  			public Object doInTransaction(TransactionStatus arg0) {
37  				HttpSession session = ((HttpServletRequest)request).getSession(true);
38  				session = ((HttpServletRequest)request).getSession();
39  				if(session.getAttribute("counted") == null && AuthUtil.getCurentUserName() != null)
40  				{
41  					session.setAttribute("counted", Boolean.TRUE);
42  					session.setAttribute("remoteuser", AuthUtil.getCurentUserName());
43  					tracker.userLoggedIn(AuthUtil.getCurentUserName());
44  				}
45  				return null;
46  			}});
47  		chain.doFilter(request, response);
48  	}
49  
50  	public void init(FilterConfig filterConfig)
51  	{
52  		context = filterConfig.getServletContext();
53  	}
54  
55  	public void destroy() {
56  	}
57  }