X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=security%2Fruntime%2Forg.argeo.security.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fjcr%2FRemoteJcrRepositoryWrapper.java;fp=security%2Fruntime%2Forg.argeo.security.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fjcr%2FRemoteJcrRepositoryWrapper.java;h=b8272356e6e1dfd07dbcaea0797522b5055bf22d;hb=3baeb167c9a92d1f915904bb956632c0aff65b2b;hp=0000000000000000000000000000000000000000;hpb=7abea79fee4787580e26cfb1f01a845767cce504;p=lgpl%2Fargeo-commons.git diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/RemoteJcrRepositoryWrapper.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/RemoteJcrRepositoryWrapper.java new file mode 100644 index 000000000..b8272356e --- /dev/null +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/RemoteJcrRepositoryWrapper.java @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed 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.argeo.security.jcr; + +import javax.jcr.Credentials; +import javax.jcr.LoginException; +import javax.jcr.NoSuchWorkspaceException; +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.RepositoryFactory; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.ArgeoException; +import org.argeo.jcr.ArgeoJcrUtils; +import org.argeo.jcr.JcrRepositoryWrapper; +import org.argeo.security.NodeAuthenticationToken; +import org.argeo.security.SystemAuthentication; +import org.springframework.security.Authentication; +import org.springframework.security.context.SecurityContextHolder; +import org.springframework.security.providers.UsernamePasswordAuthenticationToken; + +/** + * Wrapper around a remote Jackrabbit repository which allows to simplify + * configuration and intercept some actions. It exposes itself as a + * {@link Repository}. + */ +public class RemoteJcrRepositoryWrapper extends JcrRepositoryWrapper { + private final static Log log = LogFactory + .getLog(RemoteJcrRepositoryWrapper.class); + + private String uri = null; + + private RepositoryFactory repositoryFactory; + + // remote + private Credentials remoteSystemCredentials = null; + + /** + * Empty constructor, {@link #init()} should be called after properties have + * been set + */ + public RemoteJcrRepositoryWrapper() { + } + + /** + * Embedded constructor, calling the {@link #init()} method. + * + * @param alias + * if not null the repository will be published under this alias + */ + public RemoteJcrRepositoryWrapper(RepositoryFactory repositoryFactory, + String uri, Credentials remoteSystemCredentials) { + this.repositoryFactory = repositoryFactory; + this.uri = uri; + this.remoteSystemCredentials = remoteSystemCredentials; + init(); + } + + public void init() { + Repository repository = createJackrabbitRepository(); + setRepository(repository); + } + + /** Actually creates the new repository. */ + protected Repository createJackrabbitRepository() { + long begin = System.currentTimeMillis(); + try { + if (uri == null || uri.trim().equals("")) + throw new ArgeoException("Remote URI not set"); + + Repository repository = ArgeoJcrUtils.getRepositoryByUri( + repositoryFactory, uri); + if (repository == null) + throw new ArgeoException("Remote JCR repository " + uri + + " not found"); + double duration = ((double) (System.currentTimeMillis() - begin)) / 1000; + log.info("Created remote JCR repository in " + duration + + " s from URI " + uri); + // we assume that the data model of the remote repository has + // been properly initialized + return repository; + } catch (Exception e) { + throw new ArgeoException("Cannot create remote JCR repository " + + uri, e); + } + } + + /** Shutdown the repository */ + public void destroy() throws Exception { + super.destroy(); + } + + /** Central login method */ + public Session login(Credentials credentials, String workspaceName) + throws LoginException, NoSuchWorkspaceException, + RepositoryException { + + // retrieve credentials for remote + if (credentials == null) { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + if (authentication != null) { + if (authentication instanceof UsernamePasswordAuthenticationToken) { + UsernamePasswordAuthenticationToken upat = (UsernamePasswordAuthenticationToken) authentication; + credentials = new SimpleCredentials(upat.getName(), upat + .getCredentials().toString().toCharArray()); + } else if ((authentication instanceof SystemAuthentication) + || (authentication instanceof NodeAuthenticationToken)) { + credentials = remoteSystemCredentials; + } + } + } + + return super.login(credentials, workspaceName); + } + + public void setUri(String uri) { + this.uri = uri; + } + + public void setRepositoryFactory(RepositoryFactory repositoryFactory) { + this.repositoryFactory = repositoryFactory; + } + + public void setRemoteSystemCredentials(Credentials remoteSystemCredentials) { + this.remoteSystemCredentials = remoteSystemCredentials; + } + +}