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.ui.contentapp;
35
36 import info.magnolia.jcr.RuntimeRepositoryException;
37 import info.magnolia.ui.datasource.jcr.JcrSessionManager;
38 import info.magnolia.ui.framework.datasource.components.ItemResolver;
39
40 import java.util.Optional;
41
42 import javax.inject.Inject;
43 import javax.jcr.Item;
44 import javax.jcr.PathNotFoundException;
45 import javax.jcr.RepositoryException;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51
52
53 public class JcrNodeResolver implements ItemResolver<Item> {
54
55 private static final Logger log = LoggerFactory.getLogger(JcrNodeResolver.class);
56
57 private final JcrSessionManager contentDecorator;
58
59 @Inject
60 public JcrNodeResolver(JcrSessionManager contentDecorator) {
61 this.contentDecorator = contentDecorator;
62 }
63
64 @Override
65 public String getPath(Item node) {
66 try {
67 return node.getPath();
68 } catch (RepositoryException e) {
69 throw new RuntimeRepositoryException(e);
70 }
71 }
72
73 @Override
74 public Optional<Item> getItemByPath(String path) {
75 try {
76 return Optional.of(contentDecorator.getJCRSession().getNode(path + "/"));
77 } catch (PathNotFoundException e) {
78
79 } catch (RepositoryException e) {
80 log.warn("Failed to resolve node by path [{}]", path, e);
81 }
82 return Optional.empty();
83 }
84 }