]> git.argeo.org Git - gpl/argeo-slc.git/blob - RepoElem.java
6d07d1c672725063c1a49aae3ea313576b8d3d0a
[gpl/argeo-slc.git] / RepoElem.java
1 package org.argeo.slc.client.ui.dist.model;
2
3 import java.security.AccessControlException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.jcr.Credentials;
8 import javax.jcr.Node;
9 import javax.jcr.Property;
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.RepositoryFactory;
13 import javax.jcr.Session;
14 import javax.jcr.nodetype.NodeType;
15
16 import org.argeo.jcr.ArgeoJcrUtils;
17 import org.argeo.jcr.ArgeoNames;
18 import org.argeo.jcr.JcrUtils;
19 import org.argeo.slc.SlcException;
20 import org.argeo.slc.repo.RepoConstants;
21 import org.argeo.slc.repo.RepoUtils;
22 import org.argeo.util.security.Keyring;
23
24 /**
25 * Abstract a repository. Might be persisted by a node in the current user home
26 * Node or just an URI and a label if user is anonymous
27 */
28 public class RepoElem extends DistParentElem {
29 // private final static Log log = LogFactory.getLog(RepoElem.class);
30 private Repository repository;
31 private Credentials credentials;
32 private RepositoryFactory repositoryFactory;
33 private Keyring keyring;
34
35 // Defines current repo
36 private Node repoNode = null;
37 private String label;
38 private String uri;
39
40 /**
41 * Creates a RepoElement for an authenticated user. repofactory and keyring
42 * are used to enable lazy init
43 *
44 */
45 public RepoElem(Node repoNode, RepositoryFactory repoFactory,
46 Keyring keyring) {
47 this.repoNode = repoNode;
48 this.repositoryFactory = repoFactory;
49 this.keyring = keyring;
50 try {
51 // initialize this repo informations
52 setInHome(RepoConstants.DEFAULT_JAVA_REPOSITORY_ALIAS
53 .equals(repoNode.getName()));
54 if (!inHome())
55 setReadOnly(!repoNode.hasNode(ArgeoNames.ARGEO_PASSWORD));
56 uri = JcrUtils.get(repoNode, ArgeoNames.ARGEO_URI);
57 label = repoNode.isNodeType(NodeType.MIX_TITLE) ? repoNode
58 .getProperty(Property.JCR_TITLE).getString() : repoNode
59 .getName();
60 } catch (RepositoryException e) {
61 throw new SlcException("Unable to " + "initialize repo element", e);
62 }
63 }
64
65 /**
66 * Creates a RepoElement for anonymous user. repofactory is used to enable
67 * lazy init
68 *
69 */
70 public RepoElem(RepositoryFactory repoFactory, String uri, String label) {
71 this.repositoryFactory = repoFactory;
72 this.uri = uri;
73 this.label = label;
74 }
75
76 /** Lazily connects to repository */
77 protected void connect() {
78 if (repository != null)
79 return;
80 if (repoNode == null)
81 // Anonymous
82 repository = ArgeoJcrUtils.getRepositoryByUri(repositoryFactory,
83 uri);
84 else {
85 repository = RepoUtils.getRepository(repositoryFactory, keyring,
86 repoNode);
87 credentials = RepoUtils.getRepositoryCredentials(keyring, repoNode);
88 }
89 }
90
91 public String getLabel() {
92 return label;
93 }
94
95 public String getUri() {
96 return uri;
97 }
98
99 public String toString() {
100 return repoNode != null ? repoNode.toString() : label;
101 }
102
103 public Object[] getChildren() {
104 connect();
105 Session session = null;
106 try {
107 session = repository.login(credentials);
108 String[] workspaceNames = session.getWorkspace()
109 .getAccessibleWorkspaceNames();
110 Map<String, GroupElem> children = new HashMap<String, GroupElem>();
111
112 buildWksp: for (String workspaceName : workspaceNames) {
113 // Add a supplementary check to hide workspace that are not
114 // public to anonymous user
115
116 if (repoNode == null) {
117 Session tmpSession = null;
118 try {
119 tmpSession = repository.login(workspaceName);
120 Boolean res = true;
121 try {
122 tmpSession.checkPermission("/", "read");
123 } catch (AccessControlException e) {
124 res = false;
125 }
126 if (!res)
127 continue buildWksp;
128 } catch (RepositoryException e) {
129 throw new SlcException(
130 "Cannot list workspaces for anonymous user", e);
131 } finally {
132 JcrUtils.logoutQuietly(tmpSession);
133 }
134 }
135
136 // filter technical workspaces
137 // FIXME: rely on a more robust rule than just wksp name
138 if (workspaceName.lastIndexOf('-') > 0) {
139 String prefix = workspaceName.substring(0,
140 workspaceName.lastIndexOf('-'));
141 if (!children.containsKey(prefix)) {
142 children.put(prefix, new GroupElem(RepoElem.this,
143 prefix));
144 }
145 }
146 }
147 return children.values().toArray();
148 } catch (RepositoryException e) {
149 throw new SlcException("Cannot list workspaces for " + repoNode, e);
150 } finally {
151 JcrUtils.logoutQuietly(session);
152 }
153 }
154
155 public Repository getRepository() {
156 connect();
157 return repository;
158 }
159
160 public Credentials getCredentials() {
161 return credentials;
162 }
163
164 public String getDescription() {
165 String desc = label;
166 if (repoNode != null)
167 desc = label + " (" + uri + ")";
168 return desc;
169 }
170
171 /** Might return null in case of an anonymous user */
172 public Node getRepoNode() {
173 return repoNode;
174 }
175 }