2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.jcr
;
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
;
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
;
39 * Wrapper around a remote Jackrabbit repository which allows to simplify
40 * configuration and intercept some actions. It exposes itself as a
43 public class RemoteJcrRepositoryWrapper
extends JcrRepositoryWrapper
{
44 private final static Log log
= LogFactory
45 .getLog(RemoteJcrRepositoryWrapper
.class);
47 private String uri
= null;
49 private RepositoryFactory repositoryFactory
;
52 private Credentials remoteSystemCredentials
= null;
55 * Empty constructor, {@link #init()} should be called after properties have
58 public RemoteJcrRepositoryWrapper() {
62 * Embedded constructor, calling the {@link #init()} method.
65 * if not null the repository will be published under this alias
67 public RemoteJcrRepositoryWrapper(RepositoryFactory repositoryFactory
,
68 String uri
, Credentials remoteSystemCredentials
) {
69 this.repositoryFactory
= repositoryFactory
;
71 this.remoteSystemCredentials
= remoteSystemCredentials
;
76 Repository repository
= createJackrabbitRepository();
77 setRepository(repository
);
80 /** Actually creates the new repository. */
81 protected Repository
createJackrabbitRepository() {
82 long begin
= System
.currentTimeMillis();
84 if (uri
== null || uri
.trim().equals(""))
85 throw new ArgeoException("Remote URI not set");
87 Repository repository
= ArgeoJcrUtils
.getRepositoryByUri(
88 repositoryFactory
, uri
);
89 if (repository
== null)
90 throw new ArgeoException("Remote JCR repository " + uri
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
98 } catch (Exception e
) {
99 throw new ArgeoException("Cannot create remote JCR repository "
104 /** Shutdown the repository */
105 public void destroy() throws Exception
{
109 /** Central login method */
110 public Session
login(Credentials credentials
, String workspaceName
)
111 throws LoginException
, NoSuchWorkspaceException
,
112 RepositoryException
{
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
;
130 return super.login(credentials
, workspaceName
);
133 public void setUri(String uri
) {
137 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
138 this.repositoryFactory
= repositoryFactory
;
141 public void setRemoteSystemCredentials(Credentials remoteSystemCredentials
) {
142 this.remoteSystemCredentials
= remoteSystemCredentials
;