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 javax.servlet.http.HttpServletRequest;
015import javax.servlet.http.HttpSession;
016
017import org.nuxeo.ecm.core.api.CoreSession;
018import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext;
019import org.nuxeo.ecm.webengine.jaxrs.session.impl.PerRequestCoreProvider;
020
021/**
022 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
023 */
024public class SessionFactory {
025
026    public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();
027
028    private static volatile String defaultRepository = "default";
029
030    public static void setDefaultRepository(String repoName) {
031        defaultRepository = repoName;
032    }
033
034    public static String getRepositoryName(HttpServletRequest request) {
035        String v = request.getHeader("X-NXRepository");
036        if (v == null) {
037            v = request.getParameter("nxrepository");
038        }
039        return v != null ? v : defaultRepository;
040    }
041
042    public static CoreSessionProvider<?> getCoreProvider(HttpServletRequest request) {
043        CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY);
044        if (provider == null) {
045            HttpSession s = request.getSession(false);
046            if (s != null) {
047                provider = (CoreSessionProvider<?>) s.getAttribute(SESSION_FACTORY_KEY);
048            }
049            if (provider == null) {
050                provider = new PerRequestCoreProvider();
051            }
052            request.setAttribute(SESSION_FACTORY_KEY, provider);
053        }
054        return provider;
055    }
056
057    public static void dispose(HttpServletRequest request) {
058        CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY);
059        if (provider != null) {
060            request.removeAttribute(SESSION_FACTORY_KEY);
061            provider.onRequestDone(request);
062        }
063    }
064
065    public static CoreSession getSession() {
066        RequestContext ctx = RequestContext.getActiveContext();
067        if (ctx == null) {
068            throw new IllegalStateException(
069                    "You are trying to acces RequestContext data but you are not in web request a context. Make sure you have the RequestContextFilter installed and you call this method from the HTTP request thread");
070        }
071        return getSession(ctx.getRequest());
072    }
073
074    public static CoreSession getSession(String repositoryName) {
075        RequestContext ctx = RequestContext.getActiveContext();
076        if (ctx == null) {
077            throw new IllegalStateException(
078                    "You are trying to acces RequestContext data but you are not in web request a context. Make sure you have the RequestContextFilter installed and you call this method from the HTTP request thread");
079        }
080        return getSession(ctx.getRequest(), repositoryName);
081    }
082
083    public static CoreSession getSession(HttpServletRequest request) {
084        return getSession(request, getRepositoryName(request));
085    }
086
087    public static CoreSession getSession(HttpServletRequest request, String repositoryName) {
088        return getCoreProvider(request).getSession(request, repositoryName);
089    }
090
091    public static SessionRef getSessionRef(HttpServletRequest request) {
092        return getSessionRef(request, getRepositoryName(request));
093    }
094
095    public static SessionRef getSessionRef(HttpServletRequest request, String repositoryName) {
096        return getCoreProvider(request).getSessionRef(request, repositoryName);
097    }
098
099}