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.context;
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;
023
024/**
025 * Manage the context of the current HTTP request and made it available through a thread local variable to web
026 * components that are invoked inside the request.
027 *
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class RequestContextFilter extends HttpFilter {
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        RequestContext ctx = new RequestContext(request, response);
040        try {
041            chain.doFilter(request, response);
042        } finally {
043            ctx.dispose();
044        }
045    }
046
047    @Override
048    public void destroy() {
049    }
050
051}