📄 bindsessionsfilter.java
字号:
/* * $Id: BindSessionsFilter.java,v 1.2 2004/10/26 14:44:13 dobisekm Exp $ * * Copyright (C) 2000-2004 Polarion Software * All rights reserved. * Email: info@polarion.com * * * Copyright (C) 2000-2004 Polarion Software * All Rights Reserved. No use, copying or distribution of this * work may be made except in accordance with a valid license * agreement from Polarion Software This notice must be * included on all copies, modifications and derivatives of this * work. * * Polarion Software MAKES NO REPRESENTATIONS OR WARRANTIES * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. Polarion Software * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * */package org.polarion.svnwebclient.authorization.impl;import java.io.IOException;import java.net.InetAddress;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import javax.security.auth.Subject;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;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;import com.polarion.portal.tomcat.SubjectExchange;import com.polarion.subterra.base.SubterraURI;import com.polarion.subterra.server.core.SubterraServerCorePlugin;import com.polarion.subterra.server.core.cli.locate.ISubterraClient;import com.polarion.subterra.server.core.cli.locate.SubterraClientLocator;import com.polarion.subterra.server.core.cli.session.IClientSession;import com.polarion.subterra.server.core.cli.session.IClientSessionManager;import com.polarion.subterra.server.core.session.ISession;import com.polarion.subterra.server.core.session.ISessionManager;import com.polarion.core.util.MeasureTime;import com.polarion.core.util.logging.Logger;/** * This filter tries to bind correct subterra session to each http session - the subterra session is stored in * httpSession attributes and is restored for every request. * * @author <A HREF="mailto:michal.dobisek@polarion.com">Michal Dobisek</A>, Polarion Software * @version $Revision: 1.2 $ $Date: 2004/10/26 14:44:13 $ */public class BindSessionsFilter implements Filter { private static Logger log = Logger.getLogger(BindSessionsFilter.class); public static final String SUBTERRA_SESSION = "BindSessionsFilter.SubterraSession"; private ISubterraClient subterraClient = null; private IClientSessionManager cliSessMgr = null; private ISessionManager sessMgr = null; /* @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig config) throws ServletException { try { cliSessMgr = (IClientSessionManager) SubterraServerCorePlugin.getDefault() .getServerContext().getServiceManager() .getService(IClientSessionManager.WELLKNOWN_ID); sessMgr = SubterraServerCorePlugin.getDefault().getServerContext().getSessionManager(); // TODO There might be a better way to find subterraClient SubterraURI localhostURI = cliSessMgr.getNamingURI(); subterraClient = SubterraClientLocator.getInstance().locateClient(localhostURI); } catch(Exception e) { log.error("FATAL ERROR: Unable to initialize SessionBindingfilter!", e); throw new ServletException("Unable to initialize SessionBindingfilter!", e); } } private static Object lock = new Object(); private static final ThreadLocal currentSessionId = new ThreadLocal(); /* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { MeasureTime mt = null; if(log.isDebugEnabled()) { mt = new MeasureTime(); HttpServletRequest htrq = (HttpServletRequest) request; log.debug("REQUEST PROCESSING STARTED, "+htrq.getRequestURL()); } HttpSession sessionHttp = ((HttpServletRequest) request).getSession(); currentSessionId.set(sessionHttp.getId()); if (log.isDebugEnabled()) { log.debug("doFilter - httpSession: "+sessionHttp.getId()); } SessionInfo sessNfo = null; IClientSession origSession = null; Subject subject = null; try { // Is it OK to synchronize on session? synchronized(lock) { sessNfo = (SessionInfo) sessionHttp.getAttribute(SUBTERRA_SESSION); origSession = null; if(sessNfo != null && sessNfo.session != null) { if (log.isDebugEnabled()) { log.debug("doFilter - binding session "+sessNfo.session); } // Check here - if http session contains a subterra session, then // use the subterra session subterraClient.setCurrentSession(sessNfo.session); origSession = sessNfo.session; if(origSession != null) { // We can't get the subject from the client session ISession session = sessMgr.convertToServerSession(origSession); if ((session != null) && !session.isClosed()) { subject = session.getSubject(); SubjectExchange.setSessionSubject(sessionHttp.getId(), subject); } } } else { subject = CredentialsManager.getSubject((HttpServletRequest) request); sessMgr = SubterraServerCorePlugin.getDefault().getServerContext().getSessionManager(); sessMgr.login(false, subject, "SVNWebClient Thread", "", InetAddress.getLocalHost()); } } } catch(Exception e) { log.error("Some exception in session binding INIT.", e); } try { Subject.doAs(subject, new PrivilegedExceptionAction() { public Object run() throws Exception { // *** Process the request chain.doFilter(request, response); return null; } }); } catch(PrivilegedActionException e) { throw new ServletException("", e); } finally { currentSessionId.set(null); try { IClientSession session = subterraClient.getCurrentSession(); synchronized(lock) { sessNfo = (SessionInfo) sessionHttp.getAttribute(SUBTERRA_SESSION); // Session info could have been removed by // the login action if(sessNfo == null) { if (log.isDebugEnabled()) { log.debug("doFilter - creating session info"); } sessNfo = new SessionInfo(null); sessionHttp.setAttribute(SUBTERRA_SESSION, sessNfo); } if((session == null && origSession != null) || (session != null && !session.equals(origSession))) { if (log.isDebugEnabled()) { log.debug("doFilter - session changes, storing. New session "+session); } sessNfo.session = session; } } subterraClient.setCurrentSession(null); } catch(Exception e) { log.error("Some exception in session binding FINALIZE.", e); } if(log.isDebugEnabled()) { HttpServletRequest htrq = (HttpServletRequest) request; log.debug("REQUEST PROCESSING DONE"+mt+" "+htrq.getRequestURL()); } } } /** * Can be called from within a request only. Otherwise returns null. * @return */ public static String getSessionId() { return (String) currentSessionId.get(); } /* @see javax.servlet.Filter#destroy() */ public void destroy() { // ignore } public class SessionInfo implements HttpSessionBindingListener { public IClientSession session; public SessionInfo(IClientSession session) { this.session = session; } /* @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent) */ public void valueBound(HttpSessionBindingEvent event) { // ignore } /* @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent) */ public void valueUnbound(HttpSessionBindingEvent event) { try { if (log.isDebugEnabled()) { log.debug("valueUnbound - removing session info. httpSession: "+event.getSession().getId()); } // Remove the binding SubjectExchange.removeSessionSubject(event.getSession().getId()); if(session == null) { return; } // close the SubterraSession subterraClient.setCurrentSession(session); // prevention of closing already closed session if (subterraClient.getCurrentSession() != null) { cliSessMgr.logout(); } } catch (Exception ee) { log.error("valueUnbound() - Got exception while closing SubterraSession. ", ee); } } }}/* * $Log: BindSessionsFilter.java,v $ * Revision 1.2 2004/10/26 14:44:13 dobisekm * RefProxy dependency removed * * Revision 1.1 2004/09/22 13:18:44 dobisekm * adding * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -