Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 14293 invoked from network); 2 Jun 2007 15:11:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 2 Jun 2007 15:11:42 -0000 Received: (qmail 11335 invoked by uid 500); 2 Jun 2007 15:11:45 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 11308 invoked by uid 500); 2 Jun 2007 15:11:45 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 11298 invoked by uid 99); 2 Jun 2007 15:11:45 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Jun 2007 08:11:45 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Jun 2007 08:11:39 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 4D48F1A981A; Sat, 2 Jun 2007 08:11:19 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r543747 - in /jackrabbit/trunk/jackrabbit-jcr-commons: ./ src/main/java/org/apache/jackrabbit/commons/repository/ src/main/java/org/apache/jackrabbit/commons/servlet/ Date: Sat, 02 Jun 2007 15:11:18 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070602151119.4D48F1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Sat Jun 2 08:11:17 2007 New Revision: 543747 URL: http://svn.apache.org/viewvc?view=rev&rev=543747 Log: JCR-956: Reusable repository access servlets and other general purpose repository classes Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java (with props) Modified: jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml Modified: jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml?view=diff&rev=543747&r1=543746&r2=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml (original) +++ jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml Sat Jun 2 08:11:17 2007 @@ -56,6 +56,11 @@ slf4j-api + javax.servlet + servlet-api + provided + + junit junit test Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,61 @@ +/* + * 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.commons.repository; + +import javax.jcr.Credentials; +import javax.jcr.NoSuchWorkspaceException; +import javax.jcr.Session; + +import org.apache.jackrabbit.commons.AbstractRepository; + +/** + * An empty repository with no descriptors and no workspaces. This class + * can be used as a dummy sentinel in cases where a proper content repository + * is not available. + */ +public class EmptyRepository extends AbstractRepository { + + /** + * Returns null since this repository contains no descriptors. + * + * @return null + */ + public String getDescriptor(String key) { + return null; + } + + /** + * Returns an empty array since this repository contains no descriptors. + * + * @return empty array + */ + public String[] getDescriptorKeys() { + return new String[0]; + } + + /** + * Throws an exception since this repository contains no workspaces. + * + * @return nothing + * @throws NoSuchWorkspaceException always thrown + */ + public Session login(Credentials credentials, String workspace) + throws NoSuchWorkspaceException { + throw new NoSuchWorkspaceException("Empty repository"); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/EmptyRepository.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,40 @@ +/* + * 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.commons.repository; + +import java.util.Hashtable; + +/** + * Proxy for a repository bound in JNDI. The configured repository is + * looked up from JNDI lazily during each method call. Thus the JNDI entry + * does not need to exist when this class is instantiated. The JNDI entry + * can also be replaced with another repository during the lifetime of an + * instance of this class. + */ +public class JNDIRepository extends ProxyRepository { + + /** + * Creates a proxy for a repository in the given JNDI location. + * + * @param environment JNDI environment settings + * @param name JNDI name of the proxied repository + */ + public JNDIRepository(Hashtable environment, String name) { + super(new JNDIRepositoryFactory(environment, name)); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepository.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,80 @@ +/* + * 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.commons.repository; + +import java.util.Hashtable; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +/** + * Factory that looks up a repository from JNDI. + */ +public class JNDIRepositoryFactory implements RepositoryFactory { + + /** + * JNDI environment settings. + */ + private final Hashtable environment; + + /** + * JNDI name of the repository. + */ + private final String name; + + /** + * Creates a factory for looking up a repository from JNDI. + * + * @param environment JNDI environment settings + * @param name JNDI name of the repository + */ + public JNDIRepositoryFactory(Hashtable environment, String name) { + this.environment = environment; + this.name = name; + } + + /** + * Looks up and returns the configured repository. + * + * @return repository instance + * @throws RepositoryException if the repository can not be found + */ + public Repository getRepository() throws RepositoryException { + try { + Object repository = new InitialContext(environment).lookup(name); + if (repository instanceof Repository) { + return (Repository) repository; + } else if (repository == null) { + throw new RepositoryException( + "Repository not found: The JNDI entry " + + name + " is null"); + } else { + throw new RepositoryException( + "Invalid repository: The JNDI entry " + + name + " is an instance of " + + repository.getClass().getName()); + } + } catch (NamingException e) { + throw new RepositoryException( + "Repository not found: The JNDI entry " + name + + " could not be looked up", e); + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/JNDIRepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,91 @@ +/* + * 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.commons.repository; + +import javax.jcr.Credentials; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +import org.apache.jackrabbit.commons.AbstractRepository; + +/** + * Repository that proxies all method calls to another repository. + * The other repository is accessed lazily using a + * {@link RepositoryFactory repository factory}. + */ +public class ProxyRepository extends AbstractRepository { + + /** + * Factory for accessing the proxied repository. + */ + private final RepositoryFactory factory; + + /** + * Creates a proxy for the repository (or repositories) accessible + * through the given factory. + * + * @param factory repository factory + */ + public ProxyRepository(RepositoryFactory factory) { + this.factory = factory; + } + + /** + * Returns the descriptor keys of the proxied repository, or an empty + * array if the proxied repository can not be accessed. + * + * @return descriptor keys (possibly empty) + */ + public String[] getDescriptorKeys() { + try { + return factory.getRepository().getDescriptorKeys(); + } catch (RepositoryException e) { + return new String[0]; + } + } + + /** + * Returns the descriptor with the given key from the proxied repository. + * Returns null if the descriptor does not exist or if the + * proxied repository can not be accessed. + * + * @return descriptor value, or null + */ + public String getDescriptor(String key) { + try { + return factory.getRepository().getDescriptor(key); + } catch (RepositoryException e) { + return e.getMessage(); + } + } + + /** + * Logs in to the proxied repository and returns the resulting session. + *

+ * Note that the {@link Session#getRepository()} method of the resulting + * session will return the proxied repository, not this repository proxy! + * + * @throws RepositoryException if the proxied repository can not be + * accessed, or if the login in the proxied + * repository fails + */ + public Session login(Credentials credentials, String workspace) + throws RepositoryException { + return factory.getRepository().login(credentials, workspace); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/ProxyRepository.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,35 @@ +/* + * 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.commons.repository; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; + +/** + * Factory interface for JCR content repositories. + */ +public interface RepositoryFactory { + + /** + * Returns a content repository. + * + * @return content repository + * @throws RepositoryException if a repository is not available + */ + Repository getRepository() throws RepositoryException; + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/repository/RepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,183 @@ +/* + * 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.commons.servlet; + +import java.io.IOException; +import java.util.Properties; + +import javax.jcr.Repository; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Abstract base class for servlets that make a repository available in + * the servlet context. This class handles the initialization and cleanup + * tasks of setting up and clearing the configured repository attribute, + * while a subclass only needs to implement the abstract + * {@link #getRepository()} method to actually make the repository available. + *

+ * The default name of the repository attribute is + * "javax.jcr.Repository", but it can be changed by specifying + * an init parameter with the same name: + *

+ * <servlet>
+ *   <init-param>
+ *     <param-name>javax.jcr.Repository</param-name>
+ *     <param-value>my.repository.attribute</param-value>
+ *     <description>
+ *       This init parameter causes the repository to be looked up from
+ *       the "my.repository.attribute" attribute instead of the default
+ *       "javax.jcr.Repository".
+ *     </description>
+ *   </init-param>
+ * </servlet>
+ * 
+ *

+ * A repository servlet can also be mapped to the URL space. See the + * {@link #doGet(HttpServletRequest, HttpServletResponse)} method for + * the details of the default behavior. + */ +public abstract class AbstractRepositoryServlet extends HttpServlet { + + /** + * Retrieves a repository and binds it in servlet context attribute. + * + * @throws ServletException if the repository can not be retrieved + */ + public void init() throws ServletException { + getServletContext().setAttribute(getAttributeName(), getRepository()); + } + + /** + * Removes the repository attribute from the servlet context. + */ + public void destroy() { + getServletContext().removeAttribute(getAttributeName()); + } + + /** + * Returns the repository instance to be bound in the servlet context. + * This method is invoked once during servlet initialization and is the + * only method that a subclass needs to implement. + *

+ * Note that the returned repository can be (and often is) a proxy + * instance that accesses the actual repository only on demand. This + * allows late binding of the repository even though this method gets + * called already during servlet initialization. + * + * @return repository instance + * @throws ServletException if the repository is not available + */ + protected abstract Repository getRepository() throws ServletException; + + /** + * Returns the name of the repository attribute. The default + * implementation returns "javax.jcr.Repository" or + * the value of the "javax.jcr.Repository" init parameter. + *

+ * A subclass can override this method to customize the attribute name, + * but for consistency it is generally better not to do that. + * + * @return name of the repository attribute + */ + protected String getAttributeName() { + String name = Repository.class.getName(); + return getInitParameter(name, name); + } + + /** + * Utility method that returns the named init parameter or the given + * default value if the parameter does not exist. + * + * @param name name of the init parameter + * @param def default value + * @return value of the init parameter, or the default value + */ + protected String getInitParameter(String name, String def) { + String value = getInitParameter(name); + if (value == null) { + value = def; + } + return value; + } + + /** + * Outputs the repository descriptors either as a collection of properties + * (see {@link Properties#store(java.io.OutputStream, String)} or + * individually addressable text/plain resources based on the request URI. + *

+ * A typical mapping for a repository servlet would be: + *

+     * <servlet-mapping>
+     *   <servlet-name>Repository</servlet-name>
+     *   <url-pattern>/repository/*</url-pattern>
+     * </servlet-mapping>
+     * 
+ *

+ * This mapping would allow clients to retrieve all repository descriptors + * from http://server/context/repository/ and to address + * individual descriptors by key with URIs like + * http://server/context/repository/key. + * For example, the name of the repository vendor could be retrieved from + * http://server/context/repository/jcr.repository.vendor. + * Likewise, a 404 (not found) response from + * http://server/context/repository/level.2.supported would + * indicate that the repository does not support Level 2 features. + *

+ * Note that mapping a repository servlet to the URL space is optional, + * as the main purpose of the servlet is to make a repository available + * in the servlet context, not to expose repository information to web + * clients. + * + * @param request HTTP request + * @param response HTTP response + * @throws IOException on IO errors + */ + protected void doGet( + HttpServletRequest request, HttpServletResponse response) + throws IOException { + String name = getAttributeName(); + Repository repository = (Repository) + getServletContext().getAttribute(name); + + String info = request.getPathInfo(); + if (info == null || info.equals("/")) { + Properties descriptors = new Properties(); + String[] keys = repository.getDescriptorKeys(); + for (int i = 0; i < keys.length; i++) { + descriptors.setProperty( + keys[i], repository.getDescriptor(keys[i])); + } + response.setContentType("text/plain; charset=ISO-8859-1"); + descriptors.store(response.getOutputStream(), name); + } else { + String key = info.substring(1); // skip the leading "/" + String descriptor = repository.getDescriptor(key); + if (descriptor != null) { + response.setContentType("text/plain; charset=UTF-8"); + response.getWriter().write(descriptor); + } else { + response.sendError( + HttpServletResponse.SC_NOT_FOUND, + "Repository descriptor " + key + " not found"); + } + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/AbstractRepositoryServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,78 @@ +/* + * 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.commons.servlet; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.servlet.ServletContext; + +import org.apache.jackrabbit.commons.repository.RepositoryFactory; + +/** + * Factory that returns a repository from a servlet context attribute. + */ +public class ContextRepositoryFactory implements RepositoryFactory { + + /** + * Servlet context. + */ + private final ServletContext context; + + /** + * Name of the servlet context attribute. + */ + private final String name; + + /** + * Creates a factory that looks up a repository form the configured + * servlet context attribute. + * + * @param context servlet context + * @param name name of the servlet context attribute + */ + public ContextRepositoryFactory( + ServletContext context, String name) { + this.context = context; + this.name = name; + } + + /** + * Returns the repository from the configured servlet context attribute. + * + * @return repository instance + * @throws RepositoryException if the servlet context attribute does not + * exist, or if it does not contain a + * repository instance + */ + public Repository getRepository() throws RepositoryException { + Object repository = context.getAttribute(name); + if (repository instanceof Repository) { + return (Repository) repository; + } else if (repository != null) { + throw new RepositoryException( + "Invalid repository: Attribute " + name + + " in servet context " + context.getServletContextName() + + " is an instance of " + repository.getClass().getName()); + } else { + throw new RepositoryException( + "Repository not found: Attribute " + name + + " does not exist in servet context " + + context.getServletContextName()); + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,92 @@ +/* + * 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.commons.servlet; + +import javax.jcr.Repository; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.apache.jackrabbit.commons.repository.ProxyRepository; +import org.apache.jackrabbit.commons.repository.RepositoryFactory; + +/** + * Servlet that makes a repository from one servlet context attribute + * available as another servlet context attribute. The source context + * can be different from the context of this servlet. + *

+ * The supported initialization parameters of this servlet are: + *

+ *
javax.jcr.Repository
+ *
+ * Name of the target servlet context attribute. + * The default value is "javax.jcr.Repository". + *
+ *
path
+ *
+ * Context path of the source servlet context. The source context + * defaults to the context of this servlet if this parameter is not set. + *
+ *
name
+ *
+ * Name of the source servlet context attribute. The default value + * is "javax.jcr.Repository". The name of the source attribute + * can be the same as the name of target attribute only if the source + * context is different from the context of this servlet. + *
+ *
+ *

+ * This servlet can also be mapped to the URL space. See + * {@link AbstractRepositoryServlet} for the details. + */ +public class ContextRepositoryServlet extends AbstractRepositoryServlet { + + /** + * Serial version UID. + */ + private static final long serialVersionUID = 6222606878557491477L; + + /** + * Creates and returns a repository proxy for accessing a repository + * in the configured servlet context attribute. + * + * @return repository proxy + */ + protected Repository getRepository() throws ServletException { + String path = getInitParameter("path"); + String name = getInitParameter("name", Repository.class.getName()); + + ServletContext context = getServletContext(); + if (path != null && context.equals(context.getContext(path))) { + path = null; + } + + if (path == null && name.equals(getAttributeName())) { + throw new ServletException( + "Invalid configuration: Can not duplicate attribute " + + name + " of servlet " + getServletName()); + } + + RepositoryFactory factory; + if (context != null) { + factory = new CrossContextRepositoryFactory(context, path, name); + } else { + factory = new ContextRepositoryFactory(context, name); + } + return new ProxyRepository(factory); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ContextRepositoryServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,84 @@ +/* + * 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.commons.servlet; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.servlet.ServletContext; + +import org.apache.jackrabbit.commons.repository.RepositoryFactory; + +/** + * Factory that returns a repository from an attribute in an identified + * servlet context. + */ +public class CrossContextRepositoryFactory implements RepositoryFactory { + + /** + * Base servlet context that is used to look up the other context. + */ + private final ServletContext context; + + /** + * Context path of the other servlet context that contains the + * repository attribute. + */ + private final String path; + + /** + * Name of the repository attribute in the identified servlet context. + */ + private final String name; + + /** + * Creates a factory that looks up a repository from the configured + * attribute of the configured servlet context. + * + * @param context base servlet context + * @param path context path of the servlet context that contains the + * repository attribute + * @param name name of the repository attribute + */ + public CrossContextRepositoryFactory( + ServletContext context, String path, String name) { + this.context = context; + this.path = path; + this.name = name; + } + + /** + * Returns the repository from the configured attribute of the configured + * servlet context. + * + * @return repository instance + * @throws RepositoryException if the servlet context or the repository + * attribute does not exist, or if the + * attribute does not contain a repository + * instance + */ + public Repository getRepository() throws RepositoryException { + ServletContext context = this.context.getContext(path); + if (context == null) { + throw new RepositoryException( + "Repository not found: Servlet context " + path + + " does not exist or is not accessible from context " + + this.context.getServletContextName()); + } + return new ContextRepositoryFactory(context, name).getRepository(); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/CrossContextRepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,81 @@ +/* + * 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.commons.servlet; + +import java.util.Enumeration; +import java.util.Hashtable; + +import javax.jcr.Repository; +import javax.naming.InitialContext; + +import org.apache.jackrabbit.commons.repository.JNDIRepository; + +/** + * Servlet that makes a repository from JNDI available as an attribute + * in the servlet context. + *

+ * The supported initialization parameters of this servlet are: + *

+ *
javax.jcr.Repository
+ *
+ * Name of the servlet context attribute to put the repository in. + * The default value is "javax.jcr.Repository". + *
+ *
location
+ *
+ * Location of the repository in the JNDI directory. + * The default value is "javax/jcr/Repository". + *
+ *
*
+ *
+ * All other init parameters are used as the JNDI environment when + * instantiating {@link InitialContext} for looking up the repository. + *
+ *
+ *

+ * This servlet can also be mapped to the URL space. See + * {@link AbstractRepositoryServlet} for the details. + */ +public class JNDIRepositoryServlet extends AbstractRepositoryServlet { + + /** + * Serial version UID. + */ + private static final long serialVersionUID = 8952525573562952560L; + + /** + * Creates and returns a JNDI repository proxy based on the configured + * init parameters. + * + * @return JNDI repository proxy + */ + protected Repository getRepository() { + String location = Repository.class.getName().replace('.', '/'); + Hashtable environment = new Hashtable(); + Enumeration names = getInitParameterNames(); + while (names.hasMoreElements()) { + String name = (String) names.nextElement(); + if (name.equals("location")) { + location = getInitParameter(name); + } else if (!name.equals(Repository.class.getName())) { + environment.put(name, getInitParameter(name)); + } + } + return new JNDIRepository(environment, location); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/JNDIRepositoryServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,68 @@ +/* + * 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.commons.servlet; + +import javax.servlet.GenericServlet; + +import org.apache.jackrabbit.commons.repository.ProxyRepository; + +/** + * Proxy for a repository bound in servlet context. The configured repository + * attribute is looked up from the servlet context during each method call. + * Thus the repository does not need to exist when this class is instantiated. + * The repository can also be replaced with another repository during the + * lifetime of an instance of this class. + *

+ * A typical way to use this class would be: + *

+ * public class MyServlet extends HttpServlet {
+ *
+ *     private final Repository repository = new ServletRepository(this);
+ *
+ *     protected void doGet(
+ *             HttpServletRequest request, HttpServletResponse response)
+ *             throws ServletException, IOException {
+ *          try {
+ *              Session session = repository.login();
+ *              try {
+ *                  ...;
+ *              } finally {
+ *                  session.logout();
+ *              }
+ *          } catch (RepositoryException e) {
+ *              throw new ServletException(e);
+ *          }
+ *      }
+ *
+ * }
+ * 
+ * + * @see ServletRepositoryFactory + */ +public class ServletRepository extends ProxyRepository { + + /** + * Creates a proxy for a repository found in the context of the given + * servlet. + * + * @param servlet servlet + */ + public ServletRepository(GenericServlet servlet) { + super(new ServletRepositoryFactory(servlet)); + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepository.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java?view=auto&rev=543747 ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java Sat Jun 2 08:11:17 2007 @@ -0,0 +1,93 @@ +/* + * 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.commons.servlet; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.servlet.GenericServlet; +import javax.servlet.ServletContext; + +import org.apache.jackrabbit.commons.repository.RepositoryFactory; + +/** + * Factory that looks up a repository from the context of a given servlet. + *

+ * The default name of the repository attribute is + * "javax.jcr.Repository", but it can be changed by specifying + * an init parameter with the same name: + *

+ * <servlet>
+ *   <init-param>
+ *     <param-name>javax.jcr.Repository</param-name>
+ *     <param-value>my.repository.attribute</param-value>
+ *     <description>
+ *       This init parameter causes the repository to be looked up from
+ *       the "my.repository.attribute" attribute instead of the default
+ *       "javax.jcr.Repository".
+ *     </description>
+ *   </init-param>
+ * </servlet>
+ * 
+ */ +public class ServletRepositoryFactory implements RepositoryFactory { + + /** + * The servlet whose context contains the repository. + */ + private final GenericServlet servlet; + + /** + * Creates a factory for looking up a repository from the context of + * the given servlet. + * + * @param servlet servlet whose context contains the repository + */ + public ServletRepositoryFactory(GenericServlet servlet) { + this.servlet = servlet; + } + + /** + * Looks up and returns a repository bound in the configured servlet + * context attribute. + * + * @return repository from servlet context + * @throws RepositoryException if the repository is not available + */ + public Repository getRepository() throws RepositoryException { + String name = servlet.getInitParameter(Repository.class.getName()); + if (name == null) { + name = Repository.class.getName(); + } + + ServletContext context = servlet.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()); + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/servlet/ServletRepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native