]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/jcr/RemoteJcrRepositoryWrapper.java
Adapt to changes in third parties
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / jcr / RemoteJcrRepositoryWrapper.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.jcr;
17
18 import javax.jcr.Credentials;
19 import javax.jcr.LoginException;
20 import javax.jcr.NoSuchWorkspaceException;
21 import javax.jcr.Repository;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.RepositoryFactory;
24 import javax.jcr.Session;
25 import javax.jcr.SimpleCredentials;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.ArgeoException;
30 import org.argeo.jcr.ArgeoJcrUtils;
31 import org.argeo.jcr.JcrRepositoryWrapper;
32 import org.argeo.security.NodeAuthenticationToken;
33 import org.argeo.security.SystemAuthentication;
34 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
35 import org.springframework.security.core.Authentication;
36 import org.springframework.security.core.context.SecurityContextHolder;
37
38 /**
39 * Wrapper around a remote Jackrabbit repository which allows to simplify
40 * configuration and intercept some actions. It exposes itself as a
41 * {@link Repository}.
42 */
43 public class RemoteJcrRepositoryWrapper extends JcrRepositoryWrapper {
44 private final static Log log = LogFactory
45 .getLog(RemoteJcrRepositoryWrapper.class);
46
47 private String uri = null;
48
49 private RepositoryFactory repositoryFactory;
50
51 // remote
52 private Credentials remoteSystemCredentials = null;
53
54 /**
55 * Empty constructor, {@link #init()} should be called after properties have
56 * been set
57 */
58 public RemoteJcrRepositoryWrapper() {
59 }
60
61 /**
62 * Embedded constructor, calling the {@link #init()} method.
63 *
64 * @param alias
65 * if not null the repository will be published under this alias
66 */
67 public RemoteJcrRepositoryWrapper(RepositoryFactory repositoryFactory,
68 String uri, Credentials remoteSystemCredentials) {
69 this.repositoryFactory = repositoryFactory;
70 this.uri = uri;
71 this.remoteSystemCredentials = remoteSystemCredentials;
72 init();
73 }
74
75 public void init() {
76 Repository repository = createJackrabbitRepository();
77 setRepository(repository);
78 }
79
80 /** Actually creates the new repository. */
81 protected Repository createJackrabbitRepository() {
82 long begin = System.currentTimeMillis();
83 try {
84 if (uri == null || uri.trim().equals(""))
85 throw new ArgeoException("Remote URI not set");
86
87 Repository repository = ArgeoJcrUtils.getRepositoryByUri(
88 repositoryFactory, uri);
89 if (repository == null)
90 throw new ArgeoException("Remote JCR repository " + uri
91 + " not found");
92 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
93 log.info("Created remote JCR repository in " + duration
94 + " s from URI " + uri);
95 // we assume that the data model of the remote repository has
96 // been properly initialized
97 return repository;
98 } catch (Exception e) {
99 throw new ArgeoException("Cannot create remote JCR repository "
100 + uri, e);
101 }
102 }
103
104 /** Shutdown the repository */
105 public void destroy() throws Exception {
106 super.destroy();
107 }
108
109 /** Central login method */
110 public Session login(Credentials credentials, String workspaceName)
111 throws LoginException, NoSuchWorkspaceException,
112 RepositoryException {
113
114 // retrieve credentials for remote
115 if (credentials == null) {
116 Authentication authentication = SecurityContextHolder.getContext()
117 .getAuthentication();
118 if (authentication != null) {
119 if (authentication instanceof UsernamePasswordAuthenticationToken) {
120 UsernamePasswordAuthenticationToken upat = (UsernamePasswordAuthenticationToken) authentication;
121 credentials = new SimpleCredentials(upat.getName(), upat
122 .getCredentials().toString().toCharArray());
123 } else if ((authentication instanceof SystemAuthentication)
124 || (authentication instanceof NodeAuthenticationToken)) {
125 credentials = remoteSystemCredentials;
126 }
127 }
128 }
129
130 return super.login(credentials, workspaceName);
131 }
132
133 public void setUri(String uri) {
134 this.uri = uri;
135 }
136
137 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
138 this.repositoryFactory = repositoryFactory;
139 }
140
141 public void setRemoteSystemCredentials(Credentials remoteSystemCredentials) {
142 this.remoteSystemCredentials = remoteSystemCredentials;
143 }
144
145 }