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