1 package org
.argeo
.slc
.client
.ui
.dist
.model
;
3 import java
.security
.AccessControlException
;
4 import java
.util
.HashMap
;
7 import javax
.jcr
.Credentials
;
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
;
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
;
27 * Abstract a repository. It might be persisted by a node in the current user home
28 * Node or just an URI and a label if user is anonymous
30 public class RepoElem
extends DistParentElem
{
31 private final static Log log
= LogFactory
.getLog(RepoElem
.class);
33 private Repository repository
;
34 private Credentials credentials
;
35 private RepositoryFactory repositoryFactory
;
36 private Keyring keyring
;
38 // Defines current repo
39 private Node repoNode
= null;
44 * Creates a RepoElement for an authenticated user. repofactory and keyring
45 * are used to enable lazy init
48 public RepoElem(Node repoNode
, RepositoryFactory repoFactory
,
50 this.repoNode
= repoNode
;
51 this.repositoryFactory
= repoFactory
;
52 this.keyring
= keyring
;
54 // initialize this repo informations
55 setInHome(RepoConstants
.DEFAULT_JAVA_REPOSITORY_ALIAS
56 .equals(repoNode
.getName()));
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
63 } catch (RepositoryException e
) {
64 throw new SlcException("Unable to " + "initialize repo element", e
);
69 * Creates a RepoElement for anonymous user. repofactory is used to enable
73 public RepoElem(RepositoryFactory repoFactory
, String uri
, String label
) {
74 this.repositoryFactory
= repoFactory
;
79 /** Lazily connects to repository */
80 protected void connect() {
81 if (repository
!= null)
85 repository
= ArgeoJcrUtils
.getRepositoryByUri(repositoryFactory
,
88 repository
= RepoUtils
.getRepository(repositoryFactory
, keyring
,
90 credentials
= RepoUtils
.getRepositoryCredentials(keyring
, repoNode
);
94 public String
getLabel() {
98 public String
getUri() {
102 public String
toString() {
103 return repoNode
!= null ? repoNode
.toString() : label
;
106 public Object
[] getChildren() {
109 } catch (Exception e
) {
110 log
.error("Cannot connect to " + uri
+ " return no children.", e
);
111 return new Object
[0];
114 Session session
= null;
116 session
= repository
.login(credentials
);
117 String
[] workspaceNames
= session
.getWorkspace()
118 .getAccessibleWorkspaceNames();
119 Map
<String
, WkspGroupElem
> children
= new HashMap
<String
, WkspGroupElem
>();
121 buildWksp
: for (String workspaceName
: workspaceNames
) {
122 // Add a supplementary check to hide workspace that are not
123 // public to anonymous user
125 if (repoNode
== null) {
126 Session tmpSession
= null;
128 tmpSession
= repository
.login(workspaceName
);
131 tmpSession
.checkPermission("/", "read");
132 } catch (AccessControlException e
) {
137 } catch (RepositoryException e
) {
138 throw new SlcException(
139 "Cannot list workspaces for anonymous user", e
);
141 JcrUtils
.logoutQuietly(tmpSession
);
145 // filter technical workspaces
146 // FIXME: rely on a more robust rule than just wksp name
147 if (workspaceName
.lastIndexOf(VERSION_SEP
) > 0) {
148 String prefix
= workspaceName
.substring(0,
149 workspaceName
.lastIndexOf(VERSION_SEP
));
150 if (!children
.containsKey(prefix
)) {
151 children
.put(prefix
, new WkspGroupElem(RepoElem
.this,
156 return children
.values().toArray();
157 } catch (RepositoryException e
) {
158 throw new SlcException("Cannot list workspaces for " + repoNode
, e
);
160 JcrUtils
.logoutQuietly(session
);
164 public Repository
getRepository() {
169 public Credentials
getCredentials() {
173 public String
getDescription() {
175 if (repoNode
!= null)
176 desc
= label
+ " (" + uri
+ ")";
180 /** Might return null in case of an anonymous user */
181 public Node
getRepoNode() {