001/* 
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.session;
013
014import java.io.IOException;
015
016import javax.servlet.FilterChain;
017import javax.servlet.FilterConfig;
018import javax.servlet.ServletException;
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.nuxeo.ecm.webengine.jaxrs.HttpFilter;
023import org.nuxeo.ecm.webengine.jaxrs.session.impl.PerSessionCoreProvider;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class SessionCleanupFilter extends HttpFilter {
029
030    public final String STATEFUL_KEY = SessionCleanupFilter.class.getName() + ".stateful";
031
032    @Override
033    public void init(FilterConfig filterConfig) throws ServletException {
034    }
035
036    @Override
037    public void run(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException,
038            ServletException {
039        if (getBoolean(request, STATEFUL_KEY)) {
040            PerSessionCoreProvider.install(request);
041        }
042        try {
043            chain.doFilter(request, response);
044        } finally {
045            SessionFactory.dispose(request);
046        }
047    }
048
049    @Override
050    public void destroy() {
051    }
052
053}