1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.api;
17
18 import java.security.PrivilegedAction;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.jcr.NoSuchWorkspaceException;
23 import javax.jcr.Node;
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.RepositoryFactory;
27 import javax.jcr.Session;
28 import javax.naming.InvalidNameException;
29 import javax.naming.ldap.LdapName;
30 import javax.security.auth.AuthPermission;
31 import javax.security.auth.Subject;
32 import javax.security.auth.login.LoginContext;
33 import javax.security.auth.login.LoginException;
34
35
36 public class NodeUtils {
37
38
39
40
41
42 public static Repository getRepositoryByAlias(RepositoryFactory repositoryFactory, String alias) {
43 try {
44 Map<String, String> parameters = new HashMap<String, String>();
45 parameters.put(NodeConstants.CN, alias);
46 return repositoryFactory.getRepository(parameters);
47 } catch (RepositoryException e) {
48 throw new RuntimeException("Unexpected exception when trying to retrieve repository with alias " + alias,
49 e);
50 }
51 }
52
53
54
55
56
57
58 public static Repository getRepositoryByUri(RepositoryFactory repositoryFactory, String uri) {
59 return getRepositoryByUri(repositoryFactory, uri, null);
60 }
61
62
63
64
65
66
67 public static Repository getRepositoryByUri(RepositoryFactory repositoryFactory, String uri, String alias) {
68 try {
69 Map<String, String> parameters = new HashMap<String, String>();
70 parameters.put(NodeConstants.LABELED_URI, uri);
71 if (alias != null)
72 parameters.put(NodeConstants.CN, alias);
73 return repositoryFactory.getRepository(parameters);
74 } catch (RepositoryException e) {
75 throw new RuntimeException("Unexpected exception when trying to retrieve repository with uri " + uri, e);
76 }
77 }
78
79
80
81
82
83
84
85
86
87 public static Node getUserHome(Session session, String username) {
88
89
90
91
92
93
94
95
96
97
98
99
100 try {
101 checkUserWorkspace(session, username);
102 String homePath = getHomePath(username);
103 if (session.itemExists(homePath))
104 return session.getNode(homePath);
105
106 homePath = "/home/" + username;
107 if (session.itemExists(homePath))
108 return session.getNode(homePath);
109 return null;
110 } catch (RepositoryException e) {
111 throw new RuntimeException("Cannot find home for user " + username, e);
112 }
113 }
114
115 private static String getHomePath(String username) {
116 LdapName dn;
117 try {
118 dn = new LdapName(username);
119 } catch (InvalidNameException e) {
120 throw new IllegalArgumentException("Invalid name " + username, e);
121 }
122 String userId = dn.getRdn(dn.size() - 1).getValue().toString();
123 return '/' + userId;
124 }
125
126 private static void checkUserWorkspace(Session session, String username) {
127 String workspaceName = session.getWorkspace().getName();
128 if (!NodeConstants.HOME_WORKSPACE.equals(workspaceName))
129 throw new IllegalArgumentException(workspaceName + " is not the home workspace for user " + username);
130 }
131
132
133
134
135
136
137
138
139
140 public static Node getGroupHome(Session session, String groupname) {
141
142
143
144
145
146
147
148
149
150
151
152
153 try {
154 checkGroupWorkspace(session, groupname);
155 String homePath = getGroupPath(groupname);
156 if (session.itemExists(homePath))
157 return session.getNode(homePath);
158
159 homePath = "/groups/" + groupname;
160 if (session.itemExists(homePath))
161 return session.getNode(homePath);
162 return null;
163 } catch (RepositoryException e) {
164 throw new RuntimeException("Cannot find home for group " + groupname, e);
165 }
166
167 }
168
169 private static String getGroupPath(String groupname) {
170 String cn;
171 try {
172 LdapName dn = new LdapName(groupname);
173 cn = dn.getRdn(dn.size() - 1).getValue().toString();
174 } catch (InvalidNameException e) {
175 cn = groupname;
176 }
177 return '/' + cn;
178 }
179
180 private static void checkGroupWorkspace(Session session, String groupname) {
181 String workspaceName = session.getWorkspace().getName();
182 if (!NodeConstants.SRV_WORKSPACE.equals(workspaceName))
183 throw new IllegalArgumentException(workspaceName + " is not the group workspace for group " + groupname);
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public static Node getUserHome(Session session) {
213 String userID = session.getUserID();
214 return getUserHome(session, userID);
215 }
216
217
218
219
220
221 public static String getDataPath(String cn, Node node) throws RepositoryException {
222 assert node != null;
223 StringBuilder buf = new StringBuilder(NodeConstants.PATH_DATA);
224 return buf.append('/').append(cn).append('/').append(node.getSession().getWorkspace().getName())
225 .append(node.getPath()).toString();
226 }
227
228
229
230
231
232
233
234 public static Session openDataAdminSession(Repository repository, String workspaceName) {
235 ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
236 LoginContext loginContext;
237 try {
238 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
239 loginContext.login();
240 } catch (LoginException e1) {
241 throw new RuntimeException("Could not login as data admin", e1);
242 } finally {
243 Thread.currentThread().setContextClassLoader(currentCl);
244 }
245 return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
246
247 @Override
248 public Session run() {
249 try {
250 return repository.login(workspaceName);
251 } catch (NoSuchWorkspaceException e) {
252 throw new IllegalArgumentException("No workspace " + workspaceName + " available", e);
253 } catch (RepositoryException e) {
254 throw new RuntimeException("Cannot open data admin session", e);
255 }
256 }
257
258 });
259 }
260
261
262 private NodeUtils() {
263 }
264
265 }