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.cache.setup;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.module.delta.AbstractRepositoryTask;
38 import info.magnolia.module.delta.TaskExecutionException;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import javax.jcr.Node;
42 import javax.jcr.PathNotFoundException;
43 import javax.jcr.RepositoryException;
44 import javax.jcr.Session;
45
46
47
48
49 class RemoveVirtualUriMappingTask extends AbstractRepositoryTask {
50 private static final String VIRTUAL_URI_PATH = "/modules/cache/virtualURIMapping/tildeCacheRessources";
51
52 RemoveVirtualUriMappingTask() {
53 super("Remove unnecessary virtual URI mapping", "Removes the now-unnecessary virtual URI mapping for resources ~far~future~cache.");
54 }
55
56 @Override
57 protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
58 final Session session = ctx.getJCRSession(RepositoryConstants.CONFIG);
59 try {
60 final Node n = session.getNode(VIRTUAL_URI_PATH);
61 final Node parent = n.getParent();
62 if (checkProperty(n, "class", "info.magnolia.cms.beans.config.RegexpVirtualURIMapping") &&
63 checkProperty(n, "fromURI", "(\\.*)\\.(\\d{4}-(?:\\d{2}-){5}\\d{3})\\.cache\\.(css|js)") &&
64 checkProperty(n, "toURI", "$1~$2~cache.$3")) {
65 n.remove();
66 if (!parent.hasNodes()) {
67 parent.remove();
68 }
69 } else {
70 ctx.warn("Virtual URI mapping found at " + VIRTUAL_URI_PATH + " with different values than expected. Make sure you rename or remove it appropriately.");
71 }
72 } catch (PathNotFoundException e) {
73 ctx.warn("Virtual URI mapping not found at " + VIRTUAL_URI_PATH + ". If you modified or moved it, make sure you remove it appropriately.");
74 }
75 }
76
77 private boolean checkProperty(Node n, String name, String expectedValue) throws RepositoryException {
78 return n.getProperty(name).getString().equals(expectedValue);
79 }
80 }