View Javadoc
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.cms.ui.jcr.model;
17  
18  import java.util.Arrays;
19  
20  import javax.jcr.Node;
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.argeo.api.NodeUtils;
28  import org.argeo.api.security.Keyring;
29  import org.argeo.cms.ArgeoNames;
30  import org.argeo.eclipse.ui.EclipseUiException;
31  import org.argeo.eclipse.ui.TreeParent;
32  
33  /** Root of a remote repository */
34  public class RemoteRepositoryElem extends RepositoryElem {
35  	private final Keyring keyring;
36  	/**
37  	 * A session of the logged in user on the default workspace of the node
38  	 * repository.
39  	 */
40  	private final Session userSession;
41  	private final String remoteNodePath;
42  
43  	private final RepositoryFactory repositoryFactory;
44  	private final String uri;
45  
46  	public RemoteRepositoryElem(String alias, RepositoryFactory repositoryFactory, String uri, TreeParent parent,
47  			Session userSession, Keyring keyring, String remoteNodePath) {
48  		super(alias, null, parent);
49  		this.repositoryFactory = repositoryFactory;
50  		this.uri = uri;
51  		this.keyring = keyring;
52  		this.userSession = userSession;
53  		this.remoteNodePath = remoteNodePath;
54  	}
55  
56  	@Override
57  	protected Session repositoryLogin(String workspaceName) throws RepositoryException {
58  		Node remoteRepository = userSession.getNode(remoteNodePath);
59  		String userID = remoteRepository.getProperty(ArgeoNames.ARGEO_USER_ID).getString();
60  		if (userID.trim().equals("")) {
61  			return getRepository().login(workspaceName);
62  		} else {
63  			String pwdPath = remoteRepository.getPath() + '/' + ArgeoNames.ARGEO_PASSWORD;
64  			char[] password = keyring.getAsChars(pwdPath);
65  			try {
66  				SimpleCredentials credentials = new SimpleCredentials(userID, password);
67  				return getRepository().login(credentials, workspaceName);
68  			} finally {
69  				Arrays.fill(password, 0, password.length, ' ');
70  			}
71  		}
72  	}
73  
74  	@Override
75  	public Repository getRepository() {
76  		if (repository == null)
77  			repository = NodeUtils.getRepositoryByUri(repositoryFactory, uri);
78  		return super.getRepository();
79  	}
80  
81  	public void remove() {
82  		try {
83  			Node remoteNode = userSession.getNode(remoteNodePath);
84  			remoteNode.remove();
85  			remoteNode.getSession().save();
86  		} catch (RepositoryException e) {
87  			throw new EclipseUiException("Cannot remove " + remoteNodePath, e);
88  		}
89  	}
90  
91  }