Commit 7d406039 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Plugins now support GenericServlet instead of just HttpServlet (JM-459).

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@3067 b35dd754-fafc-0310-a699-88a17e54d16e
parent 0198974c
/** /**
* $RCSfile$
* $Revision$ * $Revision$
* $Date$ * $Date$
* *
* Copyright (C) 2004 Jive Software. All rights reserved. * Copyright (C) 2004-2005 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
...@@ -24,9 +23,10 @@ import org.xml.sax.SAXException; ...@@ -24,9 +23,10 @@ import org.xml.sax.SAXException;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet; import javax.servlet.GenericServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
...@@ -61,12 +61,12 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -61,12 +61,12 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class PluginServlet extends HttpServlet { public class PluginServlet extends HttpServlet {
private static Map<String, HttpServlet> servlets; private static Map<String, GenericServlet> servlets;
private static PluginManager pluginManager; private static PluginManager pluginManager;
private static ServletConfig servletConfig; private static ServletConfig servletConfig;
static { static {
servlets = new ConcurrentHashMap<String, HttpServlet>(); servlets = new ConcurrentHashMap<String, GenericServlet>();
} }
public void init(ServletConfig config) throws ServletException { public void init(ServletConfig config) throws ServletException {
...@@ -75,11 +75,11 @@ public class PluginServlet extends HttpServlet { ...@@ -75,11 +75,11 @@ public class PluginServlet extends HttpServlet {
} }
public void service(HttpServletRequest request, HttpServletResponse response) public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException
{
String pathInfo = request.getPathInfo(); String pathInfo = request.getPathInfo();
if (pathInfo == null) { if (pathInfo == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
} }
else { else {
try { try {
...@@ -89,25 +89,19 @@ public class PluginServlet extends HttpServlet { ...@@ -89,25 +89,19 @@ public class PluginServlet extends HttpServlet {
return; return;
} }
handleJSP(pathInfo, request, response); handleJSP(pathInfo, request, response);
return;
} }
// Handle servlet requests. // Handle servlet requests.
else if (getServlet(pathInfo) != null) { else if (getServlet(pathInfo) != null) {
handleServlet(pathInfo, request, response); handleServlet(pathInfo, request, response);
} }
// Handle image requests. // Handle image/other requests.
else { else {
handleOtherRequest(pathInfo, response); handleOtherRequest(pathInfo, response);
return;
} }
// Anything else results in a 404.
} }
catch (Exception e) { catch (Exception e) {
Log.error(e); Log.error(e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
} }
} }
} }
...@@ -160,10 +154,10 @@ public class PluginServlet extends HttpServlet { ...@@ -160,10 +154,10 @@ public class PluginServlet extends HttpServlet {
// Register the servlet for the URL. // Register the servlet for the URL.
Class servletClass = classMap.get(name); Class servletClass = classMap.get(name);
Object instance = servletClass.newInstance(); Object instance = servletClass.newInstance();
if (instance instanceof HttpServlet) { if (instance instanceof GenericServlet) {
// Initialize the servlet then add it to the map.. // Initialize the servlet then add it to the map..
((HttpServlet)instance).init(servletConfig); ((GenericServlet)instance).init(servletConfig);
servlets.put(pluginName + url, (HttpServlet)instance); servlets.put(pluginName + url, (GenericServlet)instance);
} }
else { else {
Log.warn("Could not load " + (pluginName + url) + ": not a servlet."); Log.warn("Could not load " + (pluginName + url) + ": not a servlet.");
...@@ -201,7 +195,7 @@ public class PluginServlet extends HttpServlet { ...@@ -201,7 +195,7 @@ public class PluginServlet extends HttpServlet {
Element nameElement = (Element)names.get(i); Element nameElement = (Element)names.get(i);
String url = nameElement.element("url-pattern").getTextTrim(); String url = nameElement.element("url-pattern").getTextTrim();
// Destroy the servlet than remove from servlets map. // Destroy the servlet than remove from servlets map.
HttpServlet servlet = servlets.get(pluginName + url); GenericServlet servlet = servlets.get(pluginName + url);
servlet.destroy(); servlet.destroy();
servlets.remove(pluginName + url); servlets.remove(pluginName + url);
servlet = null; servlet = null;
...@@ -228,15 +222,12 @@ public class PluginServlet extends HttpServlet { ...@@ -228,15 +222,12 @@ public class PluginServlet extends HttpServlet {
// Strip the starting "/" from the path to find the JSP URL. // Strip the starting "/" from the path to find the JSP URL.
String jspURL = pathInfo.substring(1); String jspURL = pathInfo.substring(1);
GenericServlet servlet = servlets.get(jspURL);
HttpServlet servlet = servlets.get(jspURL);
if (servlet != null) { if (servlet != null) {
servlet.service(request, response); servlet.service(request, response);
return;
} }
else { else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
} }
} }
...@@ -253,14 +244,12 @@ public class PluginServlet extends HttpServlet { ...@@ -253,14 +244,12 @@ public class PluginServlet extends HttpServlet {
private void handleServlet(String pathInfo, HttpServletRequest request, private void handleServlet(String pathInfo, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { HttpServletResponse response) throws ServletException, IOException {
// Strip the starting "/" from the path to find the JSP URL. // Strip the starting "/" from the path to find the JSP URL.
HttpServlet servlet = getServlet(pathInfo); GenericServlet servlet = getServlet(pathInfo);
if (servlet != null) { if (servlet != null) {
servlet.service(request, response); servlet.service(request, response);
return;
} }
else { else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
} }
} }
...@@ -270,10 +259,10 @@ public class PluginServlet extends HttpServlet { ...@@ -270,10 +259,10 @@ public class PluginServlet extends HttpServlet {
* @param pathInfo the pathinfo to map to the servlet. * @param pathInfo the pathinfo to map to the servlet.
* @return the mapped servlet, or null if no servlet was found. * @return the mapped servlet, or null if no servlet was found.
*/ */
private HttpServlet getServlet(String pathInfo) { private GenericServlet getServlet(String pathInfo) {
pathInfo = pathInfo.substring(1).toLowerCase(); pathInfo = pathInfo.substring(1).toLowerCase();
HttpServlet servlet = servlets.get(pathInfo); GenericServlet servlet = servlets.get(pathInfo);
if (servlet == null) { if (servlet == null) {
for (String key : servlets.keySet()) { for (String key : servlets.keySet()) {
int index = key.indexOf("/*"); int index = key.indexOf("/*");
...@@ -359,11 +348,13 @@ public class PluginServlet extends HttpServlet { ...@@ -359,11 +348,13 @@ public class PluginServlet extends HttpServlet {
in.close(); in.close();
} }
catch (Exception ignored) { catch (Exception ignored) {
// Ignore.
} }
try { try {
out.close(); out.close();
} }
catch (Exception ignored) { catch (Exception ignored) {
// Ignore.
} }
} }
} }
...@@ -480,7 +471,7 @@ public class PluginServlet extends HttpServlet { ...@@ -480,7 +471,7 @@ public class PluginServlet extends HttpServlet {
URL url = (URL)aCol; URL url = (URL)aCol;
File file = new File(url.getFile()); File file = new File(url.getFile());
classpath.append(file.getAbsolutePath().toString()).append(";"); classpath.append(file.getAbsolutePath()).append(";");
} }
// Load all jars from lib // Load all jars from lib
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment