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 public Object transform(Object input) {
116 final Element servletMapping = (Element) input;
117 return servletMapping.getText();
118 }
119 });
120 }
121
122 public boolean isFilterRegistered(String filterClass) {
123 return getFilterElement(filterClass) != null;
124 }
125
126
127
128
129 public boolean areFilterDispatchersConfiguredProperly(String filterClass, List mandatoryDispatchers, List optionalDispatchers) {
130 return checkFilterDispatchersConfiguration(filterClass, mandatoryDispatchers, optionalDispatchers) >= 0;
131 }
132
133
134
135
136
137
138
139 public int checkFilterDispatchersConfiguration(String filterClass, List mandatoryDispatchers, List optionalDispatchers) {
140 final Element filterEl = getFilterElement(filterClass);
141 if (filterEl != null) {
142 final String filterName = filterEl.getTextNormalize();
143 final String filterMappingXPathExpr = "/webxml:web-app/webxml:filter-mapping[webxml:filter-name='" + filterName + "']/webxml:dispatcher";
144 final List dispatchersEl = getElementsFromXPath(filterMappingXPathExpr);
145 final List registeredDispatchers = new ArrayList();
146 final Iterator it = dispatchersEl.iterator();
147 while (it.hasNext()) {
148 final Element dispatcherEl = (Element) it.next();
149 registeredDispatchers.add(dispatcherEl.getTextNormalize());
150 }
151 registeredDispatchers.removeAll(optionalDispatchers);
152 if (CollectionUtils.isEqualCollection(mandatoryDispatchers, registeredDispatchers)) {
153 return 1;
154 } else if (CollectionUtils.isSubCollection(mandatoryDispatchers, registeredDispatchers)) {
155 return 0;
156 } else {
157 return -1;
158 }
159
160 }
161 return 1;
162 }
163
164 public boolean isListenerRegistered(String deprecatedListenerClass) {
165 final String xpathExpr = "/webxml:web-app/webxml:listener[webxml:listener-class='" + deprecatedListenerClass + "']";
166 return xpathMatches(xpathExpr);
167 }
168
169 private Element getFilterElement(String filterClass) {
170 final String filterXPathExpr = "/webxml:web-app/webxml:filter[webxml:filter-class='" + filterClass + "']/webxml:filter-name";
171
172 return getElementFromXPath(filterXPathExpr);
173 }
174
175 private List getElementsFromXPath(String xpathExpr) {
176 try {
177 final XPath xpath = XPath.newInstance(xpathExpr);
178
179 xpath.addNamespace("webxml", doc.getRootElement().getNamespace().getURI());
180 return xpath.selectNodes(doc);
181 } catch (JDOMException e) {
182 throw new RuntimeException(e);
183 }
184 }
185
186 private Element getElementFromXPath(String xpathExpr) {
187 final List list = getElementsFromXPath(xpathExpr);
188 return (Element) (list.size() > 0 ? list.get(0) : null);
189 }
190
191 private boolean xpathMatches(String xpathExpr) {
192 return getElementsFromXPath(xpathExpr).size() > 0;
193 }
194 }