1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.web.jcr.spi;
25
26 import java.util.ServiceLoader;
27 import java.util.Set;
28 import javax.jcr.Repository;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31 import javax.servlet.ServletContext;
32 import javax.servlet.http.HttpServletRequest;
33 import org.modeshape.jcr.api.RepositoryFactory;
34 import org.modeshape.jcr.api.SecurityContextCredentials;
35 import org.modeshape.web.jcr.ServletSecurityContext;
36
37 /**
38 * Repository provider backed by the ModeShape {@link RepositoryFactory} implementation.
39 * <p>
40 * The provider instantiates a {code JcrEngine} that is configured from the file in the location specified by the servlet context
41 * parameter {@code org.modeshape.web.jcr.rest.CONFIG_FILE}. This location must be accessible by the classloader for this class.
42 * </p>
43 * *
44 * <p>
45 * This class is thread-safe.
46 * </p>
47 *
48 * @see RepositoryProvider
49 * @see Class#getResourceAsStream(String)
50 */
51 public class FactoryRepositoryProvider implements RepositoryProvider {
52
53 public static final String JCR_URL = "org.modeshape.web.jcr.JCR_URL";
54
55 private String jcrUrl;
56
57 public FactoryRepositoryProvider() {
58 }
59
60 public Set<String> getJcrRepositoryNames() {
61 RepositoryFactory factory = factory();
62 if (factory == null) return null;
63
64 return factory.getRepositories(jcrUrl).getRepositoryNames();
65 }
66
67 private Repository getRepository( String repositoryName ) throws RepositoryException {
68 RepositoryFactory factory = factory();
69 if (factory == null) return null;
70
71 return factory.getRepositories(jcrUrl).getRepository(repositoryName);
72 }
73
74 public void startup( ServletContext context ) {
75 this.jcrUrl = context.getInitParameter(JCR_URL);
76 }
77
78 public void shutdown() {
79 factory().shutdown();
80 }
81
82 private final RepositoryFactory factory() {
83
84 for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
85 return factory;
86 }
87
88 throw new IllegalStateException("No RepositoryFactory implementation on the classpath");
89 }
90
91 /**
92 * Returns an active session for the given workspace name in the named repository.
93 *
94 * @param request the servlet request; may not be null or unauthenticated
95 * @param repositoryName the name of the repository in which the session is created
96 * @param workspaceName the name of the workspace to which the session should be connected
97 * @return an active session with the given workspace in the named repository
98 * @throws RepositoryException if any other error occurs
99 */
100 public Session getSession( HttpServletRequest request,
101 String repositoryName,
102 String workspaceName ) throws RepositoryException {
103 assert request != null;
104
105 Repository repository;
106
107 try {
108 repository = getRepository(repositoryName);
109
110 } catch (RepositoryException re) {
111 throw new NoSuchRepositoryException(re.getMessage(), re);
112 }
113
114 // If there's no authenticated user, try an anonymous login
115 if (request.getUserPrincipal() == null) {
116 return repository.login(workspaceName);
117 }
118
119 return repository.login(new SecurityContextCredentials(new ServletSecurityContext(request)), workspaceName);
120
121 }
122 }