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.security.Principal;
015import java.util.ArrayList;
016import java.util.HashMap;
017import java.util.List;
018
019import javax.servlet.ServletRequest;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022
023/**
024 * An HTTP request context
025 *
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class RequestContext extends HashMap<String, Object> {
029
030    private static final long serialVersionUID = 1L;
031
032    private static final ThreadLocal<RequestContext> CTX = new ThreadLocal<RequestContext>();
033
034    public static RequestContext getActiveContext() {
035        return CTX.get();
036    }
037
038    public static RequestContext getActiveContext(ServletRequest request) {
039        return (RequestContext) request.getAttribute(RequestContext.class.getName());
040    }
041
042    protected HttpServletRequest request;
043
044    protected HttpServletResponse response;
045
046    protected List<RequestCleanupHandler> cleanupHandlers;
047
048    public RequestContext(HttpServletRequest request, HttpServletResponse response) {
049        this.request = request;
050        this.response = response;
051        this.cleanupHandlers = new ArrayList<RequestCleanupHandler>();
052        CTX.set(this);
053        request.setAttribute(RequestContext.class.getName(), this);
054    }
055
056    public HttpServletRequest getRequest() {
057        return request;
058    }
059
060    public HttpServletResponse getResponse() {
061        return response;
062    }
063
064    public Principal getUserPrincipal() {
065        return request.getUserPrincipal();
066    }
067
068    @SuppressWarnings("unchecked")
069    public <T> T get(String key, Class<T> type) {
070        Object o = get(key);
071        return (T) o;
072    }
073
074    public void addRequestCleanupHandler(RequestCleanupHandler handler) {
075        cleanupHandlers.add(handler);
076    }
077
078    public boolean removeCleanupHandler(RequestCleanupHandler handler) {
079        return cleanupHandlers.remove(handler);
080    }
081
082    public void dispose() {
083        request.removeAttribute(RequestContext.class.getName());
084        CTX.remove();
085        RuntimeException suppressed = null;
086        for (RequestCleanupHandler handler : cleanupHandlers) {
087            try {
088                handler.cleanup(request);
089            } catch (RuntimeException e) {
090                // allow other cleanup handlers to do their work
091                if (suppressed == null) {
092                    suppressed = new RuntimeException("Exceptions during cleanup");
093                }
094                suppressed.addSuppressed(e);
095            }
096        }
097        cleanupHandlers = null;
098        request = null;
099        response = null;
100        if (suppressed != null) {
101            throw suppressed;
102        }
103    }
104}