jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r762780 - in /jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet: FilterRepositoryFactory.java ServletRepository.java
Date Wed, 08 Apr 2009 04:10:59 GMT
Author: jukka
Date: Tue Apr  7 14:01:02 2009
New Revision: 762780

URL: http://svn.apache.org/viewvc?rev=762780&view=rev
Log:
JCR-2043: Login filters in jackrabbit-servlet

Make it easier to use the ServletRepository class also in Filter classes.

Added:
    jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/FilterRepositoryFactory.java
Modified:
    jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/ServletRepository.java

Added: jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/FilterRepositoryFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/FilterRepositoryFactory.java?rev=762780&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/FilterRepositoryFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/FilterRepositoryFactory.java
Tue Apr  7 14:01:02 2009
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.servlet;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+
+import org.apache.jackrabbit.commons.repository.RepositoryFactory;
+
+/**
+ * Factory that looks up a repository from the context of a given filter.
+ * <p>
+ * The default name of the repository attribute is
+ * "<code>javax.jcr.Repository</code>", but it can be changed by specifying
+ * an init parameter with the same name:
+ * <pre>
+ * &lt;filter&gt;
+ *   &lt;init-param&gt;
+ *     &lt;param-name&gt;javax.jcr.Repository&lt;/param-name&gt;
+ *     &lt;param-value&gt;my.repository.attribute&lt;/param-value&gt;
+ *     &lt;description&gt;
+ *       This init parameter causes the repository to be looked up from
+ *       the "my.repository.attribute" attribute instead of the default
+ *       "javax.jcr.Repository".
+ *     &lt;/description&gt;
+ *   &lt;/init-param&gt;
+ * &lt;/filter&gt;
+ * </pre>
+ *
+ * @since Apache Jackrabbit 1.6
+ */
+public class FilterRepositoryFactory implements RepositoryFactory {
+
+    /**
+     * Configuration of the filter whose context contains the repository.
+     */
+    private final FilterConfig config;
+
+    /**
+     * Creates a factory for looking up a repository from the context
+     * associated with the given filter configuration.
+     *
+     * @param config filter configuration
+     */
+    public FilterRepositoryFactory(FilterConfig config) {
+        this.config = config;
+    }
+
+    /**
+     * Looks up and returns a repository bound in the servlet context of
+     * the given filter.
+     *
+     * @return repository from servlet context
+     * @throws RepositoryException if the repository is not available
+     */
+    public Repository getRepository() throws RepositoryException {
+        String name = config.getInitParameter(Repository.class.getName());
+        if (name == null) {
+            name = Repository.class.getName();
+        }
+
+        ServletContext context = config.getServletContext();
+        Object repository = context.getAttribute(name);
+        if (repository instanceof Repository) {
+            return (Repository) repository;
+        } else if (repository != null) {
+            throw new RepositoryException(
+                    "Invalid repository: Attribute " + name
+                    + " in servlet context " + context.getServletContextName()
+                    + " is an instance of " + repository.getClass().getName());
+        } else {
+            throw new RepositoryException(
+                    "Repository not found: Attribute " + name
+                    + " does not exist in servlet context "
+                    + context.getServletContextName());
+        }
+    }
+
+}

Modified: jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/ServletRepository.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/ServletRepository.java?rev=762780&r1=762779&r2=762780&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/ServletRepository.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/ServletRepository.java
Tue Apr  7 14:01:02 2009
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.servlet;
 
+import javax.servlet.FilterConfig;
 import javax.servlet.GenericServlet;
+import javax.servlet.ServletException;
 
 import org.apache.jackrabbit.commons.repository.ProxyRepository;
 
@@ -50,9 +52,26 @@
  *
  * }
  * </pre>
+ * <p>
+ * Starting with version 1.6 this class can also be used by a servlet filter:
+ * <pre>
+ * public class MyFilter implements Filter {
+ *
+ *     private Repository repository;
+ *
+ *     public void init(FilterConfig config) {
+ *         repository = new ServletRepository(config);
+ *     }
+ *
+ *     // ...
+ *
+ * }
+ * </pre>
+ 
  *
  * @since 1.4
  * @see ServletRepositoryFactory
+ * @see FilterRepositoryFactory
  */
 public class ServletRepository extends ProxyRepository {
 
@@ -66,4 +85,15 @@
         super(new ServletRepositoryFactory(servlet));
     }
 
+    /**
+     * Creates a proxy for a repository found in the servlet context
+     * associated with the given filter configuration.
+     *
+     * @since Apache Jackrabbit 1.6
+     * @param config filter configuration
+     */
+    public ServletRepository(FilterConfig config) {
+        super(new FilterRepositoryFactory(config));
+    }
+
 }



Mime
View raw message