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 md.setTitle(StringUtils.EMPTY);
208 }
209
210
211
212
213
214
215
216
217 @Override
218 public Content getContent(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
219 if (path.equals("/")) {
220 return this.getRoot();
221 }
222 try {
223 return wrapAsContent(this.getRootNode(), getNodePath(path));
224 } catch (ItemNotFoundException e) {
225 this.getJcrSession().refresh(true);
226 return wrapAsContent(this.getRootNode(), getNodePath(path));
227 }
228 }
229
230 public Content wrapAsContent(Node rootNode, String path) throws AccessDeniedException, PathNotFoundException, RepositoryException {
231 return new DefaultContent(rootNode, path);
232 }
233
234
235
236
237
238
239
240
241
242 @Override
243 public Content getContent(String path, boolean create, ItemType type) throws AccessDeniedException,
244 RepositoryException {
245 Content node;
246 try {
247 node = getContent(path);
248 }
249 catch (PathNotFoundException e) {
250 if (create) {
251 node = this.createContent(StringUtils.substringBeforeLast(path, "/"), StringUtils.substringAfterLast(
252 path,
253 "/"), type.toString());
254 }
255 else {
256 throw e;
257 }
258 }
259 return node;
260 }
261
262
263
264
265
266
267
268
269
270 @Override
271 public NodeData getNodeData(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
272 if (StringUtils.isEmpty(path)) {
273 return null;
274 }
275 final String nodePath = StringUtils.substringBeforeLast(path, "/");
276 final String nodeDataName = StringUtils.substringAfterLast(path, "/");
277 return getContent(nodePath).getNodeData(nodeDataName);
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291 @Deprecated
292 public Content getPage(String path, String templateName) throws PathNotFoundException, RepositoryException,
293 AccessDeniedException {
294 Content page = getContent(path);
295 if (page.getTemplate().equals(templateName)) {
296 return page;
297 }
298 Content pageToBeFound = null;
299 try {
300 if (page.hasChildren()) {
301 Collection<Content> children = page.getChildren(ItemType.CONTENT.getSystemName());
302 for (Content child : children) {
303 if (child.getTemplate().equals(templateName)) {
304 return child;
305 }
306 if (child.hasChildren()) {
307 pageToBeFound = getPage(child.getHandle(), templateName);
308 }
309 if (pageToBeFound != null) {
310 return pageToBeFound;
311 }
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
327
328 @Override
329 public void delete(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
330 ItemType type = null;
331 if (this.isNodeData(path)) {
332 this.getNodeData(makeRelative(path)).delete();
333 }
334 else {
335 Node aNode = this.getRootNode().getNode(makeRelative(path));
336 aNode = NodeUtil.deepUnwrap(aNode, JCRPropertiesFilteringNodeWrapper.class);
337 if (aNode.hasProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE)) {
338 type = new ItemType(aNode.getProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE).getString());
339 }
340 type = new ItemType(aNode.getProperty(ItemType.JCR_PRIMARY_TYPE).getString());
341 aNode.remove();
342 }
343 }
344
345 private String makeRelative(String path) {
346 return StringUtils.stripStart(path, "/");
347 }
348
349
350
351
352 @Override
353 public Content getRoot() throws RepositoryException, AccessDeniedException {
354 return (new DefaultContent(this.getRootNode()));
355 }
356
357
358
359
360
361
362
363 @Override
364 public boolean isExist(String path) {
365 if (!PermissionUtil.isGranted(jcrSession, path, Session.ACTION_READ)) {
366 return false;
367 }
368 try {
369 this.getJcrSession().refresh(true);
370 return this.getJcrSession().itemExists(path);
371 } catch (RepositoryException re) {
372
373 if (re.getCause() != null && re.getCause().getClass().getName().equals("org.apache.jackrabbit.spi.commons.conversion.MalformedPathException")) {
374
375 log.debug("{} is not valid jcr path.", path);
376 } else {
377 log.error("Exception caught", re);
378 }
379 return false;
380 }
381 }
382
383 @Override
384 public boolean isGranted(String path, long oldPermissions) {
385 return PermissionUtil.isGranted(jcrSession, path, oldPermissions);
386 }
387
388
389
390
391
392
393 @Override
394 public boolean isNodeData(String path) throws AccessDeniedException {
395 boolean result = false;
396 String nodePath = getNodePath(path);
397 if (StringUtils.isEmpty(nodePath)) {
398 return false;
399 }
400 try {
401 result = this.getRootNode().hasProperty(nodePath);
402 if (!result) {
403
404 result = this.getRootNode().hasProperty(nodePath + "/" + ItemType.JCR_DATA);
405 }
406 }
407 catch (RepositoryException e) {
408
409 }
410 return result;
411 }
412
413
414
415
416
417
418 @Override
419 public Content getContentByUUID(String uuid) throws ItemNotFoundException, RepositoryException,
420 AccessDeniedException {
421 try {
422 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
423 } catch (ItemNotFoundException e) {
424
425 this.getJcrSession().refresh(true);
426 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
427 }
428 }
429
430 protected Content wrapAsContent(Node node) {
431 return new DefaultContent(node);
432 }
433
434
435
436
437 @Override
438 public Workspace getWorkspace() {
439 return getJcrSession().getWorkspace();
440 }
441
442
443
444
445
446
447
448
449 @Override
450 public void moveTo(String source, String destination) throws PathNotFoundException, RepositoryException,
451 AccessDeniedException {
452 this.getWorkspace().move(source, destination);
453 }
454
455
456
457
458
459
460
461
462 @Override
463 public void copyTo(String source, String destination) throws PathNotFoundException, RepositoryException,
464 AccessDeniedException {
465 this.getWorkspace().copy(source, destination);
466 }
467
468
469
470
471
472 @Override
473 public void save() throws RepositoryException {
474 try {
475 this.getJcrSession().save();
476 }
477 catch (RepositoryException re) {
478
479 log.error(re.getMessage(), re);
480 throw re;
481 }
482 }
483
484
485
486
487 @Override
488 public boolean hasPendingChanges() throws RepositoryException {
489 return this.getJcrSession().hasPendingChanges();
490 }
491
492
493
494
495
496
497
498 @Override
499 public void refresh(boolean keepChanges) throws RepositoryException {
500 this.getJcrSession().refresh(keepChanges);
501 }
502
503 @Override
504 public String getName() {
505 return getWorkspaceName();
506 }
507
508 @Override
509 public int hashCode() {
510 final int prime = 31;
511 int result = 1;
512 result = prime * result + ((jcrSession == null) ? 0 : jcrSession.hashCode());
513 return result;
514 }
515
516 @Override
517 public boolean equals(Object obj) {
518 if (this == obj) {
519 return true;
520 }
521 if (obj == null) {
522 return false;
523 }
524 if (getClass() != obj.getClass()) {
525 return false;
526 }
527 DefaultHierarchyManager other = (DefaultHierarchyManager) obj;
528 if (jcrSession == null) {
529 if (other.jcrSession != null) {
530 return false;
531 }
532 } else if (!SessionUtil.hasSameUnderlyingSession(jcrSession, other.jcrSession)) {
533 return false;
534 }
535 return true;
536 }
537 }