From 1349f13dd9a034fe6fd7ab2f0ba520204138af0a Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 8 Nov 2012 14:17:22 +0000 Subject: [PATCH] Optimize remoting git-svn-id: https://svn.argeo.org/commons/trunk@5745 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- demo/log4j.properties | 4 +- .../WEB-INF/security-filters.xml | 154 ++++++++++-------- .../WEB-INF/web.xml | 28 +++- .../remote/ScopedSessionProvider.java | 70 ++++---- .../mvc/MultipleRepositoryHandlerMapping.java | 9 +- 5 files changed, 161 insertions(+), 104 deletions(-) diff --git a/demo/log4j.properties b/demo/log4j.properties index 537803d19..75b2a8bb7 100644 --- a/demo/log4j.properties +++ b/demo/log4j.properties @@ -6,8 +6,8 @@ log4j.logger.org.argeo.jackrabbit.remote.ExtendedDispatcherServlet=WARN log4j.logger.org.argeo.server.webextender.TomcatDeployer=WARN log4j.logger.org.springframework.security=DEBUG -log4j.logger.org.apache.commons.exec=DEBUG -log4j.logger.org.apache.jackrabbit.server.jcr=DEBUG +#log4j.logger.org.apache.commons.exec=DEBUG +#log4j.logger.org.apache.jackrabbit.server.jcr=DEBUG log4j.logger.org.apache.catalina=INFO log4j.logger.org.apache.coyote=INFO diff --git a/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/security-filters.xml b/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/security-filters.xml index 0195436f4..578997a1b 100644 --- a/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/security-filters.xml +++ b/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/security-filters.xml @@ -6,22 +6,54 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> - + - - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -29,25 +61,25 @@ - + - - - - PATTERN_TYPE_APACHE_ANT - /**=ROLE_USER,ROLE_ADMIN - - - - - - - PATTERN_TYPE_APACHE_ANT - /**=IS_AUTHENTICATED_ANONYMOUSLY - - - + + + + + + + + + + + + + + + + @@ -68,26 +100,15 @@ - - - - - - - - - - - - + + + + + + + + + - @@ -124,25 +144,25 @@ - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/web.xml b/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/web.xml index 04cf5eb2d..a1876b306 100644 --- a/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/web.xml +++ b/server/modules/org.argeo.jackrabbit.webapp/WEB-INF/web.xml @@ -78,12 +78,34 @@ - springSecurityFilterChain + filterChain.davex org.springframework.web.filter.DelegatingFilterProxy + + filterChain.davex + /jcr/* + + + + filterChain.private + org.springframework.web.filter.DelegatingFilterProxy + + + filterChain.private + /files/* + + + filterChain.public + org.springframework.web.filter.DelegatingFilterProxy + - springSecurityFilterChain - /* + filterChain.public + /pub/* + + filterChain.public + /public/* + + diff --git a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/remote/ScopedSessionProvider.java b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/remote/ScopedSessionProvider.java index 125aff8a3..e321bfac3 100644 --- a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/remote/ScopedSessionProvider.java +++ b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/remote/ScopedSessionProvider.java @@ -16,7 +16,6 @@ package org.argeo.jackrabbit.remote; import java.io.Serializable; -import java.util.List; import javax.jcr.LoginException; import javax.jcr.Repository; @@ -30,7 +29,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.jackrabbit.server.SessionProvider; import org.argeo.ArgeoException; +import org.argeo.jcr.ArgeoJcrConstants; import org.argeo.jcr.JcrUtils; +import org.springframework.security.Authentication; import org.springframework.security.context.SecurityContextHolder; /** @@ -48,17 +49,22 @@ public class ScopedSessionProvider implements SessionProvider, Serializable { private transient String currentWorkspaceName = null; private transient String currentJcrUser = null; + // private transient String anonymousUserId = "anonymous"; + public Session getSession(HttpServletRequest request, Repository rep, String workspace) throws LoginException, ServletException, RepositoryException { - String springUser = SecurityContextHolder.getContext() - .getAuthentication().getName(); + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + if (authentication == null) + throw new ArgeoException( + "Request not authenticated by Spring Security"); + String springUser = authentication.getName(); // HTTP - String pathInfo = request.getPathInfo(); - List tokens = JcrUtils.tokenize(pathInfo); - String httpRepository = tokens.get(0); + String requestJcrRepository = (String) request + .getAttribute(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS); // HTTP session if (httpSession != null @@ -68,43 +74,46 @@ public class ScopedSessionProvider implements SessionProvider, Serializable { if (httpSession == null) httpSession = request.getSession(); + // Initializes current values if (currentRepositoryName == null) - currentRepositoryName = httpRepository; + currentRepositoryName = requestJcrRepository; if (currentWorkspaceName == null) currentWorkspaceName = workspace; if (currentJcrUser == null) currentJcrUser = springUser; + // logout if there was a change in session coordinates if (jcrSession != null) - if (!currentRepositoryName.equals(httpRepository)) { + if (!currentRepositoryName.equals(requestJcrRepository)) { if (log.isDebugEnabled()) - log.debug(getHttpSessionId() + " Changed from repository " - + currentRepositoryName + " to " + httpRepository - + ", logging out."); + log.debug(getHttpSessionId() + " Changed from repository '" + + currentRepositoryName + "' to '" + + requestJcrRepository + + "', logging out cached JCR session."); logout(); } else if (!currentWorkspaceName.equals(workspace)) { if (log.isDebugEnabled()) - log.debug(getHttpSessionId() + " Changed from workspace " - + currentWorkspaceName + " to " + workspace - + ", logging out."); + log.debug(getHttpSessionId() + " Changed from workspace '" + + currentWorkspaceName + "' to '" + workspace + + "', logging out cached JCR session."); logout(); } else if (!currentJcrUser.equals(springUser)) { if (log.isDebugEnabled()) - log.debug(getHttpSessionId() + " Changed from user " - + currentJcrUser + " to " + springUser - + ", logging out."); + log.debug(getHttpSessionId() + " Changed from user '" + + currentJcrUser + "' to '" + springUser + + "', logging out cached JCR session."); logout(); } - // JCR session + // login if needed if (jcrSession == null) try { Session session = login(rep, workspace); if (!session.getUserID().equals(springUser)) - throw new ArgeoException("HTTP user '" + springUser - + "' not in line with JCR user '" + throw new ArgeoException("Spring Security user '" + + springUser + "' not in line with JCR user '" + session.getUserID() + "'"); - currentRepositoryName = httpRepository; + currentRepositoryName = requestJcrRepository; // do not use workspace variable which may be null currentWorkspaceName = session.getWorkspace().getName(); currentJcrUser = session.getUserID(); @@ -115,8 +124,9 @@ public class ScopedSessionProvider implements SessionProvider, Serializable { throw new ArgeoException("Cannot open session to workspace " + workspace, e); } - else - return jcrSession; + + // returns cached session + return jcrSession; } protected Session login(Repository repository, String workspace) @@ -131,8 +141,8 @@ public class ScopedSessionProvider implements SessionProvider, Serializable { } public void releaseSession(Session session) { - if (log.isDebugEnabled()) - log.debug(getHttpSessionId() + " Releasing JCR session " + session); + if (log.isTraceEnabled()) + log.trace(getHttpSessionId() + " Releasing JCR session " + session); } protected void logout() { @@ -149,9 +159,11 @@ public class ScopedSessionProvider implements SessionProvider, Serializable { public void destroy() { logout(); - if (log.isDebugEnabled()) - log.debug(getHttpSessionId() - + " Cleaned up provider for web session "); + if (getHttpSessionId() != null) + if (log.isDebugEnabled()) + log.debug(getHttpSessionId() + + " Cleaned up provider for web session "); httpSession = null; } -} \ No newline at end of file + +} diff --git a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/MultipleRepositoryHandlerMapping.java b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/MultipleRepositoryHandlerMapping.java index 4f0649b2f..917ad94f7 100644 --- a/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/MultipleRepositoryHandlerMapping.java +++ b/server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/MultipleRepositoryHandlerMapping.java @@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; +import org.argeo.jcr.ArgeoJcrConstants; import org.argeo.jcr.JcrUtils; import org.argeo.jcr.RepositoryRegister; import org.springframework.beans.BeansException; @@ -77,13 +78,15 @@ public abstract class MultipleRepositoryHandlerMapping implements if ((tokens.size() == 1 || tokens.size() == 2) && request.getMethod().equals(MKCOL)) return null; - String repositoryName = extractRepositoryName(tokens); - String pathPrefix = request.getServletPath() + '/' + repositoryName; + String repositoryAlias = extractRepositoryName(tokens); + request.setAttribute(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, + repositoryAlias); + String pathPrefix = request.getServletPath() + '/' + repositoryAlias; String beanName = pathPrefix; if (!applicationContext.containsBean(beanName)) { Repository repository = repositoryRegister.getRepositories().get( - repositoryName); + repositoryAlias); HttpServlet servlet = createServlet(repository, pathPrefix); applicationContext.getBeanFactory().registerSingleton(beanName, servlet); -- 2.30.2