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.core.io.registry.context.RenderingContext; 026import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 027import org.nuxeo.ecm.webengine.jaxrs.session.impl.PerRequestCoreProvider; 028 029/** 030 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 031 */ 032public class SessionFactory { 033 034 public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName(); 035 036 private static volatile String defaultRepository = "default"; 037 038 public static void setDefaultRepository(String repoName) { 039 defaultRepository = repoName; 040 } 041 042 /** 043 * Resolves the repository name in the following order: 044 * 045 * <pre> 046 * - "X-NXRepository" request attribute 047 * - "X-NXRepository" request header 048 * - "nxrepository" request parameter 049 * - default 050 * </pre> 051 */ 052 public static String getRepositoryName(HttpServletRequest request) { 053 String v = (String) request.getAttribute(RenderingContext.REPOSITORY_NAME_REQUEST_HEADER); 054 if (v == null) { 055 v = request.getHeader(RenderingContext.REPOSITORY_NAME_REQUEST_HEADER); 056 } 057 if (v == null) { 058 v = request.getParameter(RenderingContext.REPOSITORY_NAME_REQUEST_PARAMETER); 059 } 060 return v != null ? v : defaultRepository; 061 } 062 063 public static CoreSessionProvider<?> getCoreProvider(HttpServletRequest request) { 064 CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY); 065 if (provider == null) { 066 HttpSession s = request.getSession(false); 067 if (s != null) { 068 provider = (CoreSessionProvider<?>) s.getAttribute(SESSION_FACTORY_KEY); 069 } 070 if (provider == null) { 071 provider = new PerRequestCoreProvider(); 072 } 073 request.setAttribute(SESSION_FACTORY_KEY, provider); 074 } 075 return provider; 076 } 077 078 public static void dispose(HttpServletRequest request) { 079 CoreSessionProvider<?> provider = (CoreSessionProvider<?>) request.getAttribute(SESSION_FACTORY_KEY); 080 if (provider != null) { 081 request.removeAttribute(SESSION_FACTORY_KEY); 082 provider.onRequestDone(request); 083 } 084 } 085 086 public static CoreSession getSession() { 087 RequestContext ctx = RequestContext.getActiveContext(); 088 if (ctx == null) { 089 throw new IllegalStateException( 090 "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"); 091 } 092 return getSession(ctx.getRequest()); 093 } 094 095 public static CoreSession getSession(String repositoryName) { 096 RequestContext ctx = RequestContext.getActiveContext(); 097 if (ctx == null) { 098 throw new IllegalStateException( 099 "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"); 100 } 101 return getSession(ctx.getRequest(), repositoryName); 102 } 103 104 public static CoreSession getSession(HttpServletRequest request) { 105 return getSession(request, getRepositoryName(request)); 106 } 107 108 public static CoreSession getSession(HttpServletRequest request, String repositoryName) { 109 return getCoreProvider(request).getSession(request, repositoryName); 110 } 111 112 public static SessionRef getSessionRef(HttpServletRequest request) { 113 return getSessionRef(request, getRepositoryName(request)); 114 } 115 116 public static SessionRef getSessionRef(HttpServletRequest request, String repositoryName) { 117 return getCoreProvider(request).getSessionRef(request, repositoryName); 118 } 119 120}