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.setup;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.module.delta.AbstractRepositoryTask;
38
39 import javax.jcr.Node;
40 import javax.jcr.RepositoryException;
41 import javax.jcr.Session;
42
43 import org.apache.commons.lang3.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52
53
54
55
56
57
58 public class AllowOptionMethodForRequestsTask extends AbstractRepositoryTask {
59
60 private static final Logger log = LoggerFactory.getLogger(AllowOptionMethodForRequestsTask.class);
61
62 private static final String IP_CONFIG_PATH = "/server/IPConfig/allow-all";
63 private static final String OPTIONS_METHOD = "OPTIONS";
64 private static final String METHODS_PROPERTY = "methods";
65
66 public AllowOptionMethodForRequestsTask() {
67 super("Allow OPTIONS method", "Add OPTIONS method into allowed methods of the server configuration.");
68 }
69
70 @Override
71 protected void doExecute(final InstallContext installContext) throws RepositoryException {
72 final Session session = installContext.getConfigJCRSession();
73 if (!session.nodeExists(IP_CONFIG_PATH)) {
74 log.warn("Destination node [{}] does not exist. Task failed to allow OPTIONS method.", IP_CONFIG_PATH);
75 return;
76 }
77 final Node node = session.getNode(IP_CONFIG_PATH);
78 if (!node.hasProperty(METHODS_PROPERTY)) {
79 log.warn("Expected property [methods] to exist. Task failed to allow OPTIONS method.");
80 return;
81 }
82 String methods = node.getProperty(METHODS_PROPERTY).getString();
83 if (!StringUtils.containsIgnoreCase(methods, OPTIONS_METHOD)) {
84 methods = StringUtils.appendIfMissing(methods, ",");
85 final String newValue = methods.concat(OPTIONS_METHOD);
86 node.setProperty(METHODS_PROPERTY, newValue);
87 }
88 }
89 }