001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.jaxrs.session;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpSession;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext;
026import org.nuxeo.ecm.webengine.jaxrs.session.impl.PerRequestCoreProvider;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class SessionFactory {
032
033    public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();
034
035    private static volatile String defaultRepository = "default";
036
037    public static void setDefaultRepository(String repoName) {
038        defaultRepository = repoName;
039    }
040
041    public static String getRepositoryName(HttpServletRequest request) {
042        String v = request.getHeader("X-NXRepository");
043        if (v == null) {
044            v = request.getParameter("nxrepository");
045        }
046        return v != null ? v : defaultRepository;
047    }
048
049    public static CoreSessionProvider<?> getCoreProvider(HttpServletRequest request) {
050        CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY);
051        if (provider == null) {
052            HttpSession s = request.getSession(false);
053            if (s != null) {
054                provider = (CoreSessionProvider<?>) s.getAttribute(SESSION_FACTORY_KEY);
055            }
056            if (provider == null) {
057                provider = new PerRequestCoreProvider();
058            }
059            request.setAttribute(SESSION_FACTORY_KEY, provider);
060        }
061        return provider;
062    }
063
064    public static void dispose(HttpServletRequest request) {
065        CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY);
066        if (provider != null) {
067            request.removeAttribute(SESSION_FACTORY_KEY);
068            provider.onRequestDone(request);
069        }
070    }
071
072    public static CoreSession getSession() {
073        RequestContext ctx = RequestContext.getActiveContext();
074        if (ctx == null) {
075            throw new IllegalStateException(
076                    "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");
077        }
078        return getSession(ctx.getRequest());
079    }
080
081    public static CoreSession getSession(String repositoryName) {
082        RequestContext ctx = RequestContext.getActiveContext();
083        if (ctx == null) {
084            throw new IllegalStateException(
085                    "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");
086        }
087        return getSession(ctx.getRequest(), repositoryName);
088    }
089
090    public static CoreSession getSession(HttpServletRequest request) {
091        return getSession(request, getRepositoryName(request));
092    }
093
094    public static CoreSession getSession(HttpServletRequest request, String repositoryName) {
095        return getCoreProvider(request).getSession(request, repositoryName);
096    }
097
098    public static SessionRef getSessionRef(HttpServletRequest request) {
099        return getSessionRef(request, getRepositoryName(request));
100    }
101
102    public static SessionRef getSessionRef(HttpServletRequest request, String repositoryName) {
103        return getCoreProvider(request).getSessionRef(request, repositoryName);
104    }
105
106}