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
.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
;
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
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
;
35 // Defines current repo
36 private Node repoNode
= null;
41 * Creates a RepoElement for an authenticated user. repofactory and keyring
42 * are used to enable lazy init
45 public RepoElem(Node repoNode
, RepositoryFactory repoFactory
,
47 this.repoNode
= repoNode
;
48 this.repositoryFactory
= repoFactory
;
49 this.keyring
= keyring
;
51 // initialize this repo informations
52 setInHome(RepoConstants
.DEFAULT_JAVA_REPOSITORY_ALIAS
53 .equals(repoNode
.getName()));
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
60 } catch (RepositoryException e
) {
61 throw new SlcException("Unable to " + "initialize repo element", e
);
66 * Creates a RepoElement for anonymous user. repofactory is used to enable
70 public RepoElem(RepositoryFactory repoFactory
, String uri
, String label
) {
71 this.repositoryFactory
= repoFactory
;
76 /** Lazily connects to repository */
77 protected void connect() {
78 if (repository
!= null)
82 repository
= ArgeoJcrUtils
.getRepositoryByUri(repositoryFactory
,
85 repository
= RepoUtils
.getRepository(repositoryFactory
, keyring
,
87 credentials
= RepoUtils
.getRepositoryCredentials(keyring
, repoNode
);
91 public String
getLabel() {
95 public String
getUri() {
99 public String
toString() {
100 return repoNode
!= null ? repoNode
.toString() : label
;
103 public Object
[] getChildren() {
105 Session session
= null;
107 session
= repository
.login(credentials
);
108 String
[] workspaceNames
= session
.getWorkspace()
109 .getAccessibleWorkspaceNames();
110 Map
<String
, GroupElem
> children
= new HashMap
<String
, GroupElem
>();
112 buildWksp
: for (String workspaceName
: workspaceNames
) {
113 // Add a supplementary check to hide workspace that are not
114 // public to anonymous user
116 if (repoNode
== null) {
117 Session tmpSession
= null;
119 tmpSession
= repository
.login(workspaceName
);
122 tmpSession
.checkPermission("/", "read");
123 } catch (AccessControlException e
) {
128 } catch (RepositoryException e
) {
129 throw new SlcException(
130 "Cannot list workspaces for anonymous user", e
);
132 JcrUtils
.logoutQuietly(tmpSession
);
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,
147 return children
.values().toArray();
148 } catch (RepositoryException e
) {
149 throw new SlcException("Cannot list workspaces for " + repoNode
, e
);
151 JcrUtils
.logoutQuietly(session
);
155 public Repository
getRepository() {
160 public Credentials
getCredentials() {
164 public String
getDescription() {
166 if (repoNode
!= null)
167 desc
= label
+ " (" + uri
+ ")";
171 /** Might return null in case of an anonymous user */
172 public Node
getRepoNode() {