1 5 package com.terracotta.session; 6 7 import com.tc.object.bytecode.ManagerUtil; 8 import com.terracotta.session.util.Assert; 9 import com.terracotta.session.util.ConfigProperties; 10 import com.terracotta.session.util.ContextMgr; 11 import com.terracotta.session.util.DefaultContextMgr; 12 import com.terracotta.session.util.DefaultCookieWriter; 13 import com.terracotta.session.util.DefaultIdGenerator; 14 import com.terracotta.session.util.DefaultLifecycleEventMgr; 15 import com.terracotta.session.util.LifecycleEventMgr; 16 import com.terracotta.session.util.SessionCookieWriter; 17 import com.terracotta.session.util.SessionIdGenerator; 18 19 import java.io.IOException ; 20 21 import javax.servlet.Filter ; 22 import javax.servlet.FilterChain ; 23 import javax.servlet.FilterConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 public class SessionFilter implements Filter { 32 33 private TerracottaSessionManager mgr = null; 34 private ServletContext servletContext = null; 35 public static final String FILTER_CLASS = SessionFilter.class.getName(); 36 public static final String FILTER_NAME = "Terracotta Session Filter"; 37 38 public SessionFilter() { 39 } 41 42 public void init(FilterConfig config) { 43 servletContext = config.getServletContext(); 44 } 45 46 public void destroy() { 47 } 49 50 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , 51 ServletException { 52 if (request instanceof HttpServletRequest && response instanceof HttpServletResponse ) { 53 doHttpFilter((HttpServletRequest ) request, (HttpServletResponse ) response, chain); 54 } else { 55 chain.doFilter(request, response); 56 } 57 } 58 59 private void doHttpFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) 60 throws IOException , ServletException { 61 Assert.pre(request != null); 62 Assert.pre(response != null); 63 Assert.pre(chain != null); 64 65 TerracottaSessionManager tcMgr = getManager(request); 66 TerracottaRequest sReq = tcMgr.preprocess(request, response); 67 TerracottaResponse sRes = tcMgr.createResponse(sReq, response); 68 try { 69 chain.doFilter(sReq, sRes); 70 } finally { 71 tcMgr.postprocess(sReq); 72 } 73 } 74 75 protected synchronized TerracottaSessionManager getManager(HttpServletRequest req) { 76 if (mgr == null) { 77 if (servletContext instanceof WebAppConfig) mgr = createWebAppConfigManager(req, (WebAppConfig) servletContext, 78 servletContext); 79 else mgr = createDefaultManager(req, servletContext); 80 } 81 Assert.post(mgr != null); 82 return mgr; 83 } 84 85 protected static TerracottaSessionManager createDefaultManager(final HttpServletRequest req, 86 final ServletContext servletContext) { 87 final TerracottaSessionManager rv = createManager(req, null, new BaseRequestResponseFactory(), servletContext); 88 return rv; 89 } 90 91 protected static TerracottaSessionManager createWebAppConfigManager(final HttpServletRequest req, 92 final WebAppConfig wac, 93 final ServletContext servletContext) { 94 final TerracottaSessionManager rv = createManager(req, wac, pickFactory(), servletContext); 95 return rv; 96 } 97 98 protected static TerracottaSessionManager createManager(final HttpServletRequest req, final WebAppConfig wac, 99 final RequestResponseFactory factory, 100 final ServletContext servletContext) { 101 final ConfigProperties cp = new ConfigProperties(wac); 102 103 String appName = DefaultContextMgr.computeAppName(req); 104 int lockType = ManagerUtil.getSessionLockType(appName); 105 final SessionIdGenerator sig = DefaultIdGenerator.makeInstance(cp, lockType); 106 107 final SessionCookieWriter scw = DefaultCookieWriter.makeInstance(cp); 108 final LifecycleEventMgr eventMgr = DefaultLifecycleEventMgr.makeInstance(cp); 109 final ContextMgr contextMgr = DefaultContextMgr.makeInstance(req, servletContext); 110 final TerracottaSessionManager rv = new TerracottaSessionManager(sig, scw, eventMgr, contextMgr, factory, cp); 111 return rv; 112 } 113 114 private static RequestResponseFactory pickFactory() { 115 118 try { 119 ClassLoader.getSystemClassLoader().loadClass("weblogic.Server"); 120 return (RequestResponseFactory) Class.forName("com.terracotta.session.WeblogicRequestResponseFactory") 121 .newInstance(); 122 } catch (Exception e) { 123 return new BaseRequestResponseFactory(); 124 } 125 } 126 127 } 128 | Popular Tags |