]> git.argeo.org Git - lgpl/argeo-commons.git/blob - RemoteRepositoryElem.java
0be69781c6ed8869efff04a01fc5a6d17dc0db59
[lgpl/argeo-commons.git] / RemoteRepositoryElem.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.jcr.ui.explorer.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.ArgeoException;
28 import org.argeo.eclipse.ui.TreeParent;
29 import org.argeo.jcr.ArgeoJcrUtils;
30 import org.argeo.jcr.ArgeoNames;
31 import org.argeo.util.security.Keyring;
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,
47 RepositoryFactory repositoryFactory, String uri, TreeParent parent,
48 Session userSession, Keyring keyring, String remoteNodePath) {
49 super(alias, null, parent);
50 this.repositoryFactory = repositoryFactory;
51 this.uri = uri;
52 this.keyring = keyring;
53 this.userSession = userSession;
54 this.remoteNodePath = remoteNodePath;
55 }
56
57 @Override
58 protected Session repositoryLogin(String workspaceName)
59 throws RepositoryException {
60 Node remoteRepository = userSession.getNode(remoteNodePath);
61 String userID = remoteRepository.getProperty(ArgeoNames.ARGEO_USER_ID)
62 .getString();
63 String pwdPath = remoteRepository.getPath() + '/'
64 + ArgeoNames.ARGEO_PASSWORD;
65 char[] password = keyring.getAsChars(pwdPath);
66
67 try {
68 SimpleCredentials credentials = new SimpleCredentials(userID,
69 password);
70 return getRepository().login(credentials, workspaceName);
71 } finally {
72 Arrays.fill(password, 0, password.length, ' ');
73 }
74 }
75
76 @Override
77 public Repository getRepository() {
78 if (repository == null)
79 repository = ArgeoJcrUtils.getRepositoryByUri(repositoryFactory,
80 uri);
81 return super.getRepository();
82 }
83
84 public void remove() {
85 try {
86 Node remoteNode = userSession.getNode(remoteNodePath);
87 remoteNode.remove();
88 remoteNode.getSession().save();
89 } catch (RepositoryException e) {
90 throw new ArgeoException("Cannot remove " + remoteNodePath, e);
91 }
92 }
93
94 }