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.module.googlesitemap.config; |
35 |
|
|
36 |
|
import info.magnolia.cms.beans.config.RegexpVirtualURIMapping; |
37 |
|
import info.magnolia.cms.util.ObservationUtil; |
38 |
|
import info.magnolia.context.SystemContext; |
39 |
|
import info.magnolia.jcr.predicate.AbstractPredicate; |
40 |
|
import info.magnolia.jcr.util.NodeUtil; |
41 |
|
import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration; |
42 |
|
import info.magnolia.module.googlesitemap.SiteMapNodeTypes; |
43 |
|
|
44 |
|
import java.util.HashMap; |
45 |
|
import java.util.Iterator; |
46 |
|
import java.util.Map; |
47 |
|
|
48 |
|
import javax.inject.Inject; |
49 |
|
import javax.jcr.Node; |
50 |
|
import javax.jcr.RepositoryException; |
51 |
|
import javax.jcr.observation.EventIterator; |
52 |
|
import javax.jcr.observation.EventListener; |
53 |
|
|
54 |
|
import org.slf4j.Logger; |
55 |
|
import org.slf4j.LoggerFactory; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
|
|
| 93.9% |
Uncovered Elements: 2 (33) |
Complexity: 10 |
Complexity Density: 0.45 |
|
61 |
|
public class SiteMapVirtualUriMapping extends RegexpVirtualURIMapping { |
62 |
|
|
63 |
|
public static final String XML_EXTENSION = ".xml"; |
64 |
|
|
65 |
|
private Logger log = LoggerFactory.getLogger(getClass()); |
66 |
|
|
67 |
|
private SystemContext context; |
68 |
|
|
69 |
|
private Map<String, String> siteMapUrlMappings = new HashMap<String, String>(); |
70 |
|
|
71 |
|
private String prefix; |
72 |
|
|
73 |
|
private AbstractPredicate<Node> siteMapNodePredicate = new AbstractPredicate<Node>() { |
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
74 |
18 |
@Override... |
75 |
|
public boolean evaluateTyped(Node node) { |
76 |
18 |
try { |
77 |
18 |
return NodeUtil.isNodeType(node, SiteMapNodeTypes.SiteMap.NAME); |
78 |
|
} catch (RepositoryException e) { |
79 |
0 |
return false; |
80 |
|
} |
81 |
|
} |
82 |
|
}; |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
84 |
2 |
@Inject... |
85 |
|
public SiteMapVirtualUriMapping(SystemContext context) { |
86 |
2 |
this.context = context; |
87 |
2 |
ObservationUtil.registerChangeListener(GoogleSiteMapConfiguration.WORKSPACE, "/", true, new EventListener() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
4 |
@Override... |
89 |
|
public void onEvent(EventIterator events) { |
90 |
4 |
reloadSiteMapNames(); |
91 |
|
} |
92 |
|
}); |
93 |
2 |
reloadSiteMapNames(); |
94 |
|
} |
95 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
96 |
6 |
private void reloadSiteMapNames() {... |
97 |
6 |
try { |
98 |
6 |
siteMapUrlMappings.clear(); |
99 |
6 |
Node root = context.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE).getRootNode(); |
100 |
6 |
final Iterator<Node> it = NodeUtil.collectAllChildren(root, siteMapNodePredicate).iterator(); |
101 |
18 |
while (it.hasNext()) { |
102 |
12 |
Node node = it.next(); |
103 |
12 |
if (SiteMapNodeTypes.SiteMap.getUrl(node) != null) { |
104 |
8 |
siteMapUrlMappings.put(SiteMapNodeTypes.SiteMap.getUrl(node), node.getPath() + XML_EXTENSION); |
105 |
|
} |
106 |
|
} |
107 |
|
} catch (RepositoryException e) { |
108 |
0 |
log.warn("Failed to collect sitemap names: ", e.getMessage()); |
109 |
|
} |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
112 |
3 |
@Override... |
113 |
|
public MappingResult mapURI(String uri) { |
114 |
3 |
if (siteMapUrlMappings.containsKey(uri)) { |
115 |
1 |
MappingResult result = new MappingResult(); |
116 |
1 |
result.setLevel(uri.length()); |
117 |
1 |
result.setToURI(prefix + siteMapUrlMappings.get(uri)); |
118 |
1 |
return result; |
119 |
|
} |
120 |
2 |
return null; |
121 |
|
} |
122 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
123 |
|
public void setPrefix(String prefix) {... |
124 |
|
this.prefix = prefix; |
125 |
|
} |
126 |
|
} |