1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core;
35
36 import info.magnolia.cms.core.search.QueryManager;
37 import info.magnolia.cms.core.search.QueryManagerImpl;
38 import info.magnolia.cms.security.AccessDeniedException;
39 import info.magnolia.cms.security.AccessManager;
40 import info.magnolia.cms.security.PermissionUtil;
41 import info.magnolia.jcr.RuntimeRepositoryException;
42 import info.magnolia.jcr.util.NodeUtil;
43 import info.magnolia.jcr.util.SessionUtil;
44 import info.magnolia.jcr.wrapper.JCRPropertiesFilteringNodeWrapper;
45
46 import java.io.ObjectStreamField;
47 import java.io.Serializable;
48 import java.util.Collection;
49
50 import javax.jcr.ItemNotFoundException;
51 import javax.jcr.Node;
52 import javax.jcr.PathNotFoundException;
53 import javax.jcr.RepositoryException;
54 import javax.jcr.Session;
55 import javax.jcr.Workspace;
56
57 import org.apache.commons.lang.StringUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61
62
63
64
65
66
67
68
69
70 @Deprecated
71 public class DefaultHierarchyManager implements HierarchyManager, Serializable {
72
73 private static final long serialVersionUID = 223L;
74
75
76
77
78
79 private static final ObjectStreamField[] serialPersistentFields = {
80 new ObjectStreamField("userId", String.class),
81 new ObjectStreamField("repositoryName", String.class),
82 new ObjectStreamField("workspaceName", String.class),
83 };
84
85 private static final Logger log = LoggerFactory.getLogger(DefaultHierarchyManager.class);
86
87 private Session jcrSession;
88
89 public DefaultHierarchyManager(Session jcrSession) {
90 this.jcrSession = jcrSession;
91 }
92
93 public DefaultHierarchyManager(String userId, Session jcrSession, AccessManager ignoredAccessManager) throws RepositoryException {
94 this(userId, jcrSession);
95 }
96
97 public DefaultHierarchyManager(String ignoredUserId, Session jcrSession) throws RepositoryException {
98 this(jcrSession);
99 }
100
101
102
103
104 public DefaultHierarchyManager(Session jcrSession, String ignored) {
105 this.jcrSession = jcrSession;
106 }
107
108
109
110
111
112 protected void setAccessManager(AccessManager accessManager) {
113
114 throw new UnsupportedOperationException("Custom access managers are no longer supported. Use Repository level security checks instead.");
115 }
116
117
118
119
120
121 @Override
122 public AccessManager getAccessManager() {
123
124 throw new UnsupportedOperationException("Custom access managers are no longer supported. Use Repository level security checks instead.");
125 }
126
127
128
129
130
131 protected void setQueryManager(QueryManager queryManager) {
132 throw new UnsupportedOperationException("This Operation is no longer available.");
133 }
134
135 @Override
136 public QueryManager getQueryManager() {
137 try {
138 return new QueryManagerImpl(getJcrSession().getWorkspace().getQueryManager(), this);
139 } catch (RepositoryException e) {
140 throw new RuntimeRepositoryException(e);
141 }
142 }
143
144 private Node getRootNode() throws RepositoryException {
145 return jcrSession.getRootNode();
146 }
147
148 private String getWorkspaceName() {
149 return jcrSession.getWorkspace().getName();
150 }
151
152 protected Session getJcrSession() {
153 return this.jcrSession;
154 }
155
156 protected void setJcrSession(Session jcrSession) {
157 this.jcrSession = jcrSession;
158 }
159
160
161
162
163
164
165
166
167
168
169
170 @Override
171 public Content createContent(String path, String label, String contentType) throws PathNotFoundException,
172 RepositoryException, AccessDeniedException {
173 Content content = wrapAsContent(this.getRootNode(), this.getNodePath(path, label), contentType);
174 setMetaData(content.getMetaData());
175 return content;
176 }
177
178 protected Content wrapAsContent(Node rootNode, String path, String contentType) throws PathNotFoundException, RepositoryException, AccessDeniedException {
179 return new DefaultContent(rootNode, path, contentType);
180 }
181
182 private String getNodePath(String parent, String label) {
183 if (StringUtils.isEmpty(parent) || (parent.equals("/"))) {
184 return label;
185 }
186 if (!parent.endsWith("/")) {
187 parent = parent + "/";
188 }
189 return getNodePath(parent + label);
190 }
191
192 private String getNodePath(String path) {
193 if (path != null && path.startsWith("/")) {
194 return path.replaceFirst("/", StringUtils.EMPTY);
195 }
196 return path;
197 }
198
199
200
201
202
203 protected void setMetaData(MetaData md) throws RepositoryException, AccessDeniedException {
204 md.setCreationDate();
205 md.setModificationDate();
206 md.setAuthorId(this.jcrSession.getUserID());
207 }
208
209
210
211
212
213
214
215
216 @Override
217 public Content getContent(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
218 if (path.equals("/")) {
219 return this.getRoot();
220 }
221 try {
222 return wrapAsContent(this.getRootNode(), getNodePath(path));
223 } catch (ItemNotFoundException e) {
224 this.getJcrSession().refresh(true);
225 return wrapAsContent(this.getRootNode(), getNodePath(path));
226 }
227 }
228
229 public Content wrapAsContent(Node rootNode, String path) throws AccessDeniedException, PathNotFoundException, RepositoryException {
230 return new DefaultContent(rootNode, path);
231 }
232
233
234
235
236
237
238
239
240
241 @Override
242 public Content getContent(String path, boolean create, ItemType type) throws AccessDeniedException,
243 RepositoryException {
244 Content node;
245 try {
246 node = getContent(path);
247 }
248 catch (PathNotFoundException e) {
249 if (create) {
250 node = this.createContent(StringUtils.substringBeforeLast(path, "/"), StringUtils.substringAfterLast(
251 path,
252 "/"), type.toString());
253 }
254 else {
255 throw e;
256 }
257 }
258 return node;
259 }
260
261
262
263
264
265
266
267
268
269 @Override
270 public NodeData getNodeData(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
271 if (StringUtils.isEmpty(path)) {
272 return null;
273 }
274 final String nodePath = StringUtils.substringBeforeLast(path, "/");
275 final String nodeDataName = StringUtils.substringAfterLast(path, "/");
276 return getContent(nodePath).getNodeData(nodeDataName);
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290 @Deprecated
291 public Content getPage(String path, String templateName) throws PathNotFoundException, RepositoryException,
292 AccessDeniedException {
293 Content page = getContent(path);
294 if (page.getTemplate().equals(templateName)) {
295 return page;
296 }
297 Content pageToBeFound = null;
298 try {
299 if (page.hasChildren()) {
300 Collection<Content> children = page.getChildren(ItemType.CONTENT.getSystemName());
301 for (Content child : children) {
302 if (child.getTemplate().equals(templateName)) {
303 return child;
304 }
305 if (child.hasChildren()) {
306 pageToBeFound = getPage(child.getHandle(), templateName);
307 }
308 if (pageToBeFound != null) {
309 return pageToBeFound;
310 }
311 }
312 }
313 }
314 catch (Exception e) {
315 log.error("Failed to get - " + path + " : " + e.getMessage(), e);
316 }
317 return pageToBeFound;
318 }
319
320
321
322
323
324
325
326
327 @Override
328 public void delete(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
329 ItemType type = null;
330 if (this.isNodeData(path)) {
331 this.getNodeData(makeRelative(path)).delete();
332 }
333 else {
334 Node aNode = this.getRootNode().getNode(makeRelative(path));
335 aNode = NodeUtil.deepUnwrap(aNode, JCRPropertiesFilteringNodeWrapper.class);
336 if (aNode.hasProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE)) {
337 type = new ItemType(aNode.getProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE).getString());
338 }
339 type = new ItemType(aNode.getProperty(ItemType.JCR_PRIMARY_TYPE).getString());
340 aNode.remove();
341 }
342 }
343
344 private String makeRelative(String path) {
345 return StringUtils.stripStart(path, "/");
346 }
347
348
349
350
351 @Override
352 public Content getRoot() throws RepositoryException, AccessDeniedException {
353 return (new DefaultContent(this.getRootNode()));
354 }
355
356
357
358
359
360
361
362 @Override
363 public boolean isExist(String path) {
364 if (!PermissionUtil.isGranted(jcrSession, path, Session.ACTION_READ)) {
365 return false;
366 }
367 try {
368 this.getJcrSession().refresh(true);
369 return this.getJcrSession().itemExists(path);
370 } catch (RepositoryException re) {
371
372 if (re.getCause().getClass().getName().equals("org.apache.jackrabbit.spi.commons.conversion.MalformedPathException")) {
373
374 log.debug("{} is not valid jcr path.", path);
375 } else {
376 log.error("Exception caught", re);
377 }
378 return false;
379 }
380 }
381
382 @Override
383 public boolean isGranted(String path, long oldPermissions) {
384 return PermissionUtil.isGranted(jcrSession, path, oldPermissions);
385 }
386
387
388
389
390
391
392 @Override
393 public boolean isNodeData(String path) throws AccessDeniedException {
394 boolean result = false;
395 String nodePath = getNodePath(path);
396 if (StringUtils.isEmpty(nodePath)) {
397 return false;
398 }
399 try {
400 result = this.getRootNode().hasProperty(nodePath);
401 if (!result) {
402
403 result = this.getRootNode().hasProperty(nodePath + "/" + ItemType.JCR_DATA);
404 }
405 }
406 catch (RepositoryException e) {
407
408 }
409 return result;
410 }
411
412
413
414
415
416
417 @Override
418 public Content getContentByUUID(String uuid) throws ItemNotFoundException, RepositoryException,
419 AccessDeniedException {
420 try {
421 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
422 } catch (ItemNotFoundException e) {
423
424 this.getJcrSession().refresh(true);
425 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
426 }
427 }
428
429 protected Content wrapAsContent(Node node) {
430 return new DefaultContent(node);
431 }
432
433
434
435
436 @Override
437 public Workspace getWorkspace() {
438 return getJcrSession().getWorkspace();
439 }
440
441
442
443
444
445
446
447
448 @Override
449 public void moveTo(String source, String destination) throws PathNotFoundException, RepositoryException,
450 AccessDeniedException {
451 this.getWorkspace().move(source, destination);
452 }
453
454
455
456
457
458
459
460
461 @Override
462 public void copyTo(String source, String destination) throws PathNotFoundException, RepositoryException,
463 AccessDeniedException {
464 this.getWorkspace().copy(source, destination);
465 }
466
467
468
469
470
471 @Override
472 public void save() throws RepositoryException {
473 try {
474 this.getJcrSession().save();
475 }
476 catch (RepositoryException re) {
477
478 log.error(re.getMessage(), re);
479 throw re;
480 }
481 }
482
483
484
485
486 @Override
487 public boolean hasPendingChanges() throws RepositoryException {
488 return this.getJcrSession().hasPendingChanges();
489 }
490
491
492
493
494
495
496
497 @Override
498 public void refresh(boolean keepChanges) throws RepositoryException {
499 this.getJcrSession().refresh(keepChanges);
500 }
501
502 @Override
503 public String getName() {
504 return getWorkspaceName();
505 }
506
507 @Override
508 public int hashCode() {
509 final int prime = 31;
510 int result = 1;
511 result = prime * result + ((jcrSession == null) ? 0 : jcrSession.hashCode());
512 return result;
513 }
514
515 @Override
516 public boolean equals(Object obj) {
517 if (this == obj) {
518 return true;
519 }
520 if (obj == null) {
521 return false;
522 }
523 if (getClass() != obj.getClass()) {
524 return false;
525 }
526 DefaultHierarchyManager other = (DefaultHierarchyManager) obj;
527 if (jcrSession == null) {
528 if (other.jcrSession != null) {
529 return false;
530 }
531 } else if (!SessionUtil.hasSameUnderlyingSession(jcrSession, other.jcrSession)) {
532 return false;
533 }
534 return true;
535 }
536 }