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.util;
35
36 import info.magnolia.cms.core.Path;
37 import org.apache.commons.collections.CollectionUtils;
38 import org.apache.commons.collections.Transformer;
39 import org.jdom.Document;
40 import org.jdom.Element;
41 import org.jdom.JDOMException;
42 import org.jdom.input.SAXBuilder;
43 import org.jdom.xpath.XPath;
44
45 import java.io.File;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Iterator;
51 import java.util.List;
52
53
54
55
56
57
58
59
60 public class WebXmlUtil {
61 private final Document doc;
62
63 public WebXmlUtil() {
64 final File source = new File(Path.getAppRootDir() + "/WEB-INF/web.xml");
65 if (!source.exists()) {
66 throw new IllegalStateException("Failed to locate web.xml : " + source.getAbsolutePath());
67 }
68 final SAXBuilder builder = new SAXBuilder();
69 try {
70 doc = builder.build(source);
71 } catch (JDOMException e) {
72 throw new RuntimeException(e);
73 } catch (IOException e) {
74 throw new RuntimeException(e);
75 }
76 }
77
78
79
80
81 public WebXmlUtil(InputStream inputStream) {
82 final SAXBuilder builder = new SAXBuilder();
83 try {
84 doc = builder.build(inputStream);
85 } catch (JDOMException e) {
86 throw new RuntimeException(e);
87 } catch (IOException e) {
88 throw new RuntimeException(e);
89 }
90 }
91
92 public boolean isServletOrMappingRegistered(String servletName) {
93 return isServletRegistered(servletName) || isServletMappingRegistered(servletName);
94 }
95
96 public boolean isServletRegistered(String servletName) {
97 return xpathMatches("/webxml:web-app/webxml:servlet[webxml:servlet-name='" + servletName + "']");
98 }
99
100 public boolean isServletMappingRegistered(String servletName) {
101 return xpathMatches("/webxml:web-app/webxml:servlet-mapping[webxml:servlet-name='" + servletName + "']");
102 }
103
104 public boolean isServletMappingRegistered(String servletName, String urlPattern) {
105 final String xpathExpr = "/webxml:web-app/webxml:servlet-mapping[webxml:servlet-name='"
106 + servletName + "' and webxml:url-pattern='" + urlPattern + "']";
107 return xpathMatches(xpathExpr);
108 }
109
110 public Collection getServletMappings(String servletName) {
111 final String servletMappingXPathExpr = "/webxml:web-app/webxml:servlet-mapping[webxml:servlet-name='" + servletName + "']/webxml:url-pattern";
112 final List servletMappings = getElementsFromXPath(servletMappingXPathExpr);
113
114 return CollectionUtils.collect(servletMappings, new Transformer() {
115 @Override
116 public Object transform(Object input) {
117 final Element servletMapping = (Element) input;
118 return servletMapping.getText();
119 }
120 });
121 }
122
123 public boolean isFilterRegistered(String filterClass) {
124 return getFilterElement(filterClass) != null;
125 }
126
127
128
129
130
131
132
133 public int checkFilterDispatchersConfiguration(String filterClass, List mandatoryDispatchers, List optionalDispatchers) {
134 final Element filterEl = getFilterElement(filterClass);
135 if (filterEl != null) {
136 final String filterName = filterEl.getTextNormalize();
137 final String filterMappingXPathExpr = "/webxml:web-app/webxml:filter-mapping[webxml:filter-name='" + filterName + "']/webxml:dispatcher";
138 final List dispatchersEl = getElementsFromXPath(filterMappingXPathExpr);
139 final List registeredDispatchers = new ArrayList();
140 final Iterator it = dispatchersEl.iterator();
141 while (it.hasNext()) {
142 final Element dispatcherEl = (Element) it.next();
143 registeredDispatchers.add(dispatcherEl.getTextNormalize());
144 }
145 registeredDispatchers.removeAll(optionalDispatchers);
146 if (CollectionUtils.isEqualCollection(mandatoryDispatchers, registeredDispatchers)) {
147 return 1;
148 } else if (CollectionUtils.isSubCollection(mandatoryDispatchers, registeredDispatchers)) {
149 return 0;
150 } else {
151 return -1;
152 }
153
154 }
155 return 1;
156 }
157
158 public boolean isListenerRegistered(String deprecatedListenerClass) {
159 final String xpathExpr = "/webxml:web-app/webxml:listener[webxml:listener-class='" + deprecatedListenerClass + "']";
160 return xpathMatches(xpathExpr);
161 }
162
163 private Element getFilterElement(String filterClass) {
164 final String filterXPathExpr = "/webxml:web-app/webxml:filter[webxml:filter-class='" + filterClass + "']/webxml:filter-name";
165
166 return getElementFromXPath(filterXPathExpr);
167 }
168
169 private List getElementsFromXPath(String xpathExpr) {
170 try {
171 final XPath xpath = XPath.newInstance(xpathExpr);
172
173 xpath.addNamespace("webxml", doc.getRootElement().getNamespace().getURI());
174 return xpath.selectNodes(doc);
175 } catch (JDOMException e) {
176 throw new RuntimeException(e);
177 }
178 }
179
180 private Element getElementFromXPath(String xpathExpr) {
181 final List list = getElementsFromXPath(xpathExpr);
182 return (Element) (list.size() > 0 ? list.get(0) : null);
183 }
184
185 private boolean xpathMatches(String xpathExpr) {
186 return getElementsFromXPath(xpathExpr).size() > 0;
187 }
188 }