1 20 21 package org.jivesoftware.whack.container; 22 23 import org.dom4j.Document; 24 import org.dom4j.Element; 25 import org.dom4j.io.SAXReader; 26 import org.xmpp.component.Component; 27 import org.xmpp.component.ComponentManager; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.io.*; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.concurrent.ConcurrentHashMap ; 40 41 59 public class ComponentServlet extends HttpServlet { 60 61 private static Map <String , HttpServlet > servlets; 62 private static File componentDirectory; 63 private static ServletConfig servletConfig; 64 65 private static ComponentManager manager; 66 67 static { 68 servlets = new ConcurrentHashMap <String , HttpServlet >(); 69 componentDirectory = new File(ServerContainer.getInstance().getHomeDirectory(), "components"); 70 manager = ServerContainer.getInstance().getManager(); 71 } 72 73 public void init(ServletConfig config) throws ServletException { 74 super.init(config); 75 servletConfig = config; 76 } 77 78 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 79 String pathInfo = request.getPathInfo(); 80 if (pathInfo == null) { 81 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 82 return; 83 } 84 else { 85 try { 86 if (pathInfo.endsWith(".jsp")) { 88 handleJSP(pathInfo, request, response); 89 return; 90 } 91 else if (pathInfo.endsWith(".gif") || pathInfo.endsWith(".png")) { 93 handleImage(pathInfo, response); 94 return; 95 } 96 else if (servlets.containsKey(pathInfo.substring(1).toLowerCase())) { 98 handleServlet(pathInfo, request, response); 99 } 100 else { 102 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 103 return; 104 } 105 } 106 catch (Exception e) { 107 manager.getLog().error(e); 108 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 109 return; 110 } 111 } 112 } 113 114 122 public static void registerServlets(ComponentFinder finder, Component component, File webXML) { 123 if (!webXML.exists()) { 124 manager.getLog().error("Could not register component servlets, file " + webXML.getAbsolutePath() + 125 " does not exist."); 126 return; 127 } 128 String pluginName = webXML.getParentFile().getParentFile().getName(); 131 try { 132 SAXReader saxReader = new SAXReader(false); 135 saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", 136 false); 137 Document doc = saxReader.read(webXML); 138 List classes = doc.selectNodes("//servlet"); 140 Map <String , Class > classMap = new HashMap <String , Class >(); 141 for (int i = 0; i < classes.size(); i++) { 142 Element servletElement = (Element)classes.get(i); 143 String name = servletElement.element("servlet-name").getTextTrim(); 144 String className = servletElement.element("servlet-class").getTextTrim(); 145 classMap.put(name, finder.loadClass(className, component)); 146 } 147 List names = doc.selectNodes("//servlet-mapping"); 149 for (int i = 0; i < names.size(); i++) { 150 Element nameElement = (Element)names.get(i); 151 String name = nameElement.element("servlet-name").getTextTrim(); 152 String url = nameElement.element("url-pattern").getTextTrim(); 153 Class servletClass = classMap.get(name); 155 Object instance = servletClass.newInstance(); 156 if (instance instanceof HttpServlet ) { 157 ((HttpServlet )instance).init(servletConfig); 159 servlets.put(pluginName + url, (HttpServlet )instance); 160 } 161 else { 162 manager.getLog().warn("Could not load " + (pluginName + url) + ": not a servlet."); 163 } 164 } 165 } 166 catch (Throwable e) { 167 manager.getLog().error(e); 168 } 169 } 170 171 177 public static void unregisterServlets(File webXML) { 178 if (!webXML.exists()) { 179 manager.getLog().error("Could not unregister component servlets, file " + webXML.getAbsolutePath() + 180 " does not exist."); 181 return; 182 } 183 String pluginName = webXML.getParentFile().getParentFile().getName(); 186 try { 187 SAXReader saxReader = new SAXReader(false); 188 saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", 189 false); 190 Document doc = saxReader.read(webXML); 191 List names = doc.selectNodes("//servlet-mapping"); 193 for (int i = 0; i < names.size(); i++) { 194 Element nameElement = (Element)names.get(i); 195 String url = nameElement.element("url-pattern").getTextTrim(); 196 HttpServlet servlet = servlets.get(pluginName + url); 198 servlet.destroy(); 199 servlets.remove(pluginName + url); 200 servlet = null; 201 } 202 } 203 catch (Throwable e) { 204 manager.getLog().error(e); 205 } 206 } 207 208 220 private void handleJSP(String pathInfo, HttpServletRequest request, 221 HttpServletResponse response) throws ServletException , IOException { 222 String jspURL = pathInfo.substring(1); 224 HttpServlet servlet = servlets.get(jspURL); 225 if (servlet != null) { 226 servlet.service(request, response); 227 return; 228 } 229 else { 230 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 231 return; 232 } 233 } 234 235 246 private void handleServlet(String pathInfo, HttpServletRequest request, 247 HttpServletResponse response) throws ServletException , IOException { 248 String jspURL = pathInfo.substring(1); 250 HttpServlet servlet = servlets.get(jspURL); 251 if (servlet != null) { 252 servlet.service(request, response); 253 return; 254 } 255 else { 256 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 257 return; 258 } 259 } 260 261 268 private void handleImage(String pathInfo, HttpServletResponse response) throws IOException { 269 String [] parts = pathInfo.split("/"); 270 if (parts.length != 4) { 272 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 273 return; 274 } 275 File image = new File(componentDirectory, parts[1] + File.separator + "web" + 276 File.separator + "images" + File.separator + parts[3]); 277 if (!image.exists()) { 278 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 279 return; 280 } 281 else { 282 String contentType = "image/gif"; 284 if (pathInfo.endsWith(".png")) { 285 contentType = "image/png"; 286 } 287 response.setHeader("Content-disposition", "filename=\"" + image + "\";"); 288 response.setContentType(contentType); 289 InputStream in = null; 291 ServletOutputStream out = null; 292 try { 293 in = new BufferedInputStream(new FileInputStream(image)); 294 out = response.getOutputStream(); 295 296 response.setContentLength((int)image.length()); 298 299 byte[] buf = new byte[1024]; 301 int len; 302 while ((len = in.read(buf)) != -1) { 303 out.write(buf, 0, len); 304 } 305 } 306 finally { 307 try { 308 in.close(); 309 } 310 catch (Exception ignored) { 311 } 312 try { 313 out.close(); 314 } 315 catch (Exception ignored) { 316 } 317 } 318 } 319 } 320 } 321 | Popular Tags |