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