| 1 | |
package org.webslinger.servlet.webxml; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Collection; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.HashSet; |
| 8 | |
import java.util.Iterator; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.Set; |
| 12 | |
import java.util.TreeSet; |
| 13 | |
import java.util.logging.Level; |
| 14 | |
import java.util.logging.Logger; |
| 15 | |
import javax.servlet.Filter; |
| 16 | |
import javax.servlet.Servlet; |
| 17 | |
import javax.xml.parsers.DocumentBuilder; |
| 18 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 19 | |
import javax.xml.parsers.ParserConfigurationException; |
| 20 | |
|
| 21 | |
import org.apache.commons.vfs.FileObject; |
| 22 | |
|
| 23 | |
import org.w3c.dom.Element; |
| 24 | |
import org.w3c.dom.Document; |
| 25 | |
import org.w3c.dom.Node; |
| 26 | |
import org.w3c.dom.Text; |
| 27 | |
|
| 28 | |
import org.webslinger.util.MimeTypeLookup; |
| 29 | |
import org.webslinger.util.MimeTypes; |
| 30 | |
import org.webslinger.util.MimeTypes.MimeTypeEntry; |
| 31 | |
import org.webslinger.xml.XmlUtil; |
| 32 | |
|
| 33 | |
import org.xml.sax.SAXException; |
| 34 | |
|
| 35 | |
public class WebXml extends Parameterized implements MimeTypeLookup { |
| 36 | 1 | private static final Logger logger = Logger.getLogger(WebXml.class.getName()); |
| 37 | 1 | public static final DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 38 | |
|
| 39 | |
protected String icon; |
| 40 | |
protected String displayName; |
| 41 | |
protected String description; |
| 42 | |
protected boolean distributable; |
| 43 | 26 | protected ConfiguredFilter[] filters = new ConfiguredFilter[0]; |
| 44 | 26 | protected String[] listeners = new String[0]; |
| 45 | 26 | protected ConfiguredServlet[] servlets = new ConfiguredServlet[0]; |
| 46 | |
protected int sessionTimeout; |
| 47 | 26 | protected final List<String> welcomeFiles = new ArrayList<String>(); |
| 48 | 26 | protected FilterMapping[] filterMappings = new FilterMapping[0]; |
| 49 | |
protected final MimeTypes mimeTypes; |
| 50 | |
|
| 51 | 0 | public WebXml() throws IOException { |
| 52 | 0 | mimeTypes = new MimeTypes(); |
| 53 | 0 | mimeTypes.load(getClass().getClassLoader().getResources("org/webslinger/mime.types")); |
| 54 | 0 | } |
| 55 | |
|
| 56 | 26 | public WebXml(MimeTypes mimeTypes) throws IOException { |
| 57 | 26 | this.mimeTypes = mimeTypes; |
| 58 | 26 | } |
| 59 | |
|
| 60 | |
public WebXml(Document doc) throws IOException { |
| 61 | 0 | this(doc.getDocumentElement()); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
public WebXml(MimeTypes mimeTypes, Document doc) throws IOException { |
| 65 | 21 | this(mimeTypes, doc.getDocumentElement()); |
| 66 | 21 | } |
| 67 | |
|
| 68 | |
public WebXml(Element element) throws IOException { |
| 69 | 0 | this(); |
| 70 | 0 | parse(element); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public WebXml(MimeTypes mimeTypes, Element element) throws IOException { |
| 74 | 21 | this(mimeTypes); |
| 75 | 21 | parse(element); |
| 76 | 21 | } |
| 77 | |
|
| 78 | |
public WebXml(FileObject file) throws IOException, ParserConfigurationException, SAXException { |
| 79 | 0 | this(); |
| 80 | 0 | parse(file); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
public WebXml(MimeTypes mimeTypes, FileObject file) throws IOException, ParserConfigurationException, SAXException { |
| 84 | 0 | this(mimeTypes); |
| 85 | 0 | parse(file); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
public String getMimeType(String extension) { |
| 89 | 785 | return mimeTypes.getMimeType(extension); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public String[] getExtensions(String mimeType) { |
| 93 | 0 | return mimeTypes.getExtensions(mimeType); |
| 94 | |
} |
| 95 | |
|
| 96 | |
public String getPrimaryExtension(String mimeType) { |
| 97 | 0 | return mimeTypes.getPrimaryExtension(mimeType); |
| 98 | |
} |
| 99 | |
|
| 100 | |
public String getIcon() { |
| 101 | 0 | return icon; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public String getDisplayName() { |
| 105 | 0 | return displayName; |
| 106 | |
} |
| 107 | |
|
| 108 | |
public String getDescription() { |
| 109 | 0 | return description; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public boolean getDistributable() { |
| 113 | 0 | return distributable; |
| 114 | |
} |
| 115 | |
|
| 116 | |
public ConfiguredFilter[] getFilters() { |
| 117 | 26 | return filters; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public FilterMapping[] getFilterMappings() { |
| 121 | 1 | return filterMappings; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public String[] getListeners() { |
| 125 | 0 | return listeners; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public ConfiguredServlet[] getServlets() { |
| 129 | 26 | return servlets; |
| 130 | |
} |
| 131 | |
|
| 132 | |
public int getSessionTimeout() { |
| 133 | 0 | return sessionTimeout; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public void parse(FileObject file) throws IOException, ParserConfigurationException, SAXException { |
| 185 | 0 | if (file.exists()) { |
| 186 | 0 | DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder(); |
| 187 | 0 | Document doc = domBuilder.parse(file.getContent().getInputStream(), file.getName().toString()); |
| 188 | 0 | parse(doc.getDocumentElement()); |
| 189 | |
} |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
public void parse(Element element) { |
| 193 | 21 | TreeSet<ConfiguredFilter> filters = new TreeSet<ConfiguredFilter>(); |
| 194 | 21 | TreeSet<ConfiguredServlet> servlets = new TreeSet<ConfiguredServlet>(); |
| 195 | 21 | TreeSet<String> listeners = new TreeSet<String>(); |
| 196 | 21 | List<FilterMapping> filterMappings = new ArrayList<FilterMapping>(); |
| 197 | 21 | Node node = element.getFirstChild(); |
| 198 | 130 | while (node != null) { |
| 199 | 109 | if (node instanceof Element) { |
| 200 | 44 | String nodeName = node.getNodeName(); |
| 201 | 44 | if ("icon".equals(nodeName)) { |
| 202 | 0 | icon = XmlUtil.getNodeSubText(node); |
| 203 | 44 | } else if ("display-name".equals(nodeName)) { |
| 204 | 0 | displayName = XmlUtil.getNodeSubText(node); |
| 205 | 44 | } else if ("description".equals(nodeName)) { |
| 206 | 0 | description = XmlUtil.getNodeSubText(node); |
| 207 | 44 | } else if ("distributable".equals(nodeName)) { |
| 208 | 0 | distributable = true; |
| 209 | 44 | } else if ("context-param".equals(nodeName)) { |
| 210 | 2 | parseParameter(node); |
| 211 | 42 | } else if ("filter".equals(nodeName)) { |
| 212 | 0 | ConfiguredFilter configuredFilter = new ConfiguredFilter((Element) node); |
| 213 | 0 | filters.remove(configuredFilter); |
| 214 | 0 | filters.add(configuredFilter); |
| 215 | 0 | } else if ("filter-mapping".equals(nodeName)) { |
| 216 | 0 | filterMappings.add(new FilterMapping((Element) node)); |
| 217 | 42 | } else if ("listener".equals(nodeName)) { |
| 218 | 0 | parseListener(listeners, node); |
| 219 | 42 | } else if ("servlet".equals(nodeName)) { |
| 220 | 21 | ConfiguredServlet configuredServlet = new ConfiguredServlet((Element) node); |
| 221 | 21 | servlets.remove(configuredServlet); |
| 222 | 21 | servlets.add(configuredServlet); |
| 223 | 21 | } else if ("session-config".equals(nodeName)) { |
| 224 | 0 | parseSessionConfig(node); |
| 225 | 21 | } else if ("mime-mapping".equals(nodeName)) { |
| 226 | 0 | parseMimeMapping(node); |
| 227 | 21 | } else if ("welcome-file-list".equals(nodeName)) { |
| 228 | 0 | parseWelcomeFileList(node); |
| 229 | |
} |
| 230 | |
} |
| 231 | 109 | node = node.getNextSibling(); |
| 232 | |
} |
| 233 | 21 | this.filters = filters.toArray(new ConfiguredFilter[filters.size()]); |
| 234 | 21 | this.listeners = listeners.toArray(new String[listeners.size()]); |
| 235 | 21 | this.servlets = servlets.toArray(new ConfiguredServlet[servlets.size()]); |
| 236 | 21 | this.filterMappings = filterMappings.toArray(new FilterMapping[filterMappings.size()]); |
| 237 | 21 | } |
| 238 | |
|
| 239 | |
public void init(ClassLoader loader) { |
| 240 | 26 | verifyConfiguredClasses("servlet", servlets, Servlet.class, loader); |
| 241 | 26 | verifyConfiguredClasses("filter", filters, Filter.class, loader); |
| 242 | 26 | } |
| 243 | |
|
| 244 | |
protected void parseListener(Collection<String> listeners, Node node) { |
| 245 | 0 | node = node.getFirstChild(); |
| 246 | 0 | String listenerClassName = null; |
| 247 | 0 | while (node != null) { |
| 248 | 0 | if (node instanceof Element) { |
| 249 | 0 | String nodeName = node.getNodeName(); |
| 250 | 0 | if ("listener-class".equals(nodeName)) { |
| 251 | 0 | listenerClassName = XmlUtil.getNodeSubText(node); |
| 252 | |
} |
| 253 | |
} |
| 254 | 0 | node = node.getNextSibling(); |
| 255 | |
} |
| 256 | 0 | if (listenerClassName != null) listeners.add(listenerClassName); |
| 257 | 0 | } |
| 258 | |
|
| 259 | |
protected void parseSessionConfig(Node node) { |
| 260 | 0 | node = node.getFirstChild(); |
| 261 | 0 | while (node != null) { |
| 262 | 0 | if (node instanceof Element) { |
| 263 | 0 | String nodeName = node.getNodeName(); |
| 264 | 0 | if ("session-timeout".equals(nodeName)) { |
| 265 | 0 | sessionTimeout = Integer.parseInt(XmlUtil.getNodeSubText(node)); |
| 266 | |
} |
| 267 | |
} |
| 268 | 0 | node = node.getNextSibling(); |
| 269 | |
} |
| 270 | 0 | } |
| 271 | |
|
| 272 | |
protected void parseMimeMapping(Node node) { |
| 273 | 0 | node = node.getFirstChild(); |
| 274 | 0 | String extension = null; |
| 275 | 0 | String mimeType = null; |
| 276 | 0 | while (node != null) { |
| 277 | 0 | if (node instanceof Element) { |
| 278 | 0 | String nodeName = node.getNodeName(); |
| 279 | 0 | if ("extension".equals(nodeName)) { |
| 280 | 0 | extension = XmlUtil.getNodeSubText(node); |
| 281 | 0 | } else if ("mime-type".equals(nodeName)) { |
| 282 | 0 | mimeType = XmlUtil.getNodeSubText(node); |
| 283 | |
} |
| 284 | |
} |
| 285 | 0 | node = node.getNextSibling(); |
| 286 | |
} |
| 287 | 0 | MimeTypeEntry entry = mimeTypes.addMimeType(mimeType); |
| 288 | 0 | entry.addExtension(extension); |
| 289 | 0 | entry.setPrimaryExtension(extension); |
| 290 | 0 | } |
| 291 | |
|
| 292 | |
protected void parseWelcomeFileList(Node node) { |
| 293 | 0 | node = node.getFirstChild(); |
| 294 | 0 | while (node != null) { |
| 295 | 0 | if (node instanceof Element) { |
| 296 | 0 | String nodeName = node.getNodeName(); |
| 297 | 0 | if ("welcome-file".equals(nodeName)) { |
| 298 | 0 | welcomeFiles.add(XmlUtil.getNodeSubText(node)); |
| 299 | |
} |
| 300 | |
} |
| 301 | 0 | node = node.getNextSibling(); |
| 302 | |
} |
| 303 | 0 | } |
| 304 | |
|
| 305 | |
protected <T> void verifyConfiguredClasses(String label, Configured[] configuredItems, Class<T> implClass, ClassLoader loader) { |
| 306 | 73 | for (Configured configured: configuredItems) { |
| 307 | |
try { |
| 308 | 21 | Class<?> clz = Class.forName(configured.getClassName(), true, loader); |
| 309 | 21 | if (!implClass.isAssignableFrom(clz)) { |
| 310 | 0 | logger.log(Level.SEVERE, "Not a " + label + ": " + clz); |
| 311 | |
} |
| 312 | 0 | } catch (ClassNotFoundException e) { |
| 313 | 0 | logger.log(Level.SEVERE, "Could not find " + configured.getClassName() + " for servlet " + configured.getName(), e); |
| 314 | 21 | } |
| 315 | |
} |
| 316 | 52 | } |
| 317 | |
} |