Commit 58d4b809 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Javadocs, init servlets, added support for images.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@463 b35dd754-fafc-0310-a699-88a17e54d16e
parent d5fe1848
......@@ -22,15 +22,27 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.File;
import javax.servlet.ServletOutputStream;
import java.io.*;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* The plugin servlet acts as a proxy for web requests (in the admin console)
* to plugins. Since plugins can be dynamically loaded and live in a different place
* than normal Jive Messenger admin console files, it's not possible to have them
* added to the normal Jive Messenger admin console web app directory.<p>
*
* The servlet listens for requests in the form <tt>/plugins/[pluginName]/[JSP File]</tt>
* (e.g. <tt>/plugins/foo/example.jsp</tt>). It also listens for image requests in the
* the form <tt>/plugins/[pluginName]/images/*.png|gif</tt> (e.g.
* <tt>/plugins/foo/images/example.gif</tt>).<p>
*
* JSP files must be compiled and available via the plugin's class loader. The mapping
* between JSP name and servlet class files is defined in [pluginName]/web/web.xml.
* Typically, this file is auto-generated by the JSP compiler when packaging the plugin.
*
* @author Matt Tucker
*/
......@@ -38,9 +50,11 @@ public class PluginServlet extends HttpServlet {
private static Map<String,HttpServlet> servlets;
private static File pluginDirectory;
private static ServletConfig servletConfig;
public void init(ServletConfig config) throws ServletException {
super.init(config);
servletConfig = config;
servlets = new ConcurrentHashMap<String,HttpServlet>();
pluginDirectory = new File(JiveGlobals.getMessengerHome(), "plugins");
}
......@@ -60,7 +74,7 @@ public class PluginServlet extends HttpServlet {
}
// Handle image requests.
else if (pathInfo.endsWith(".gif") || pathInfo.endsWith(".png")) {
handleImage(pathInfo, request, response);
handleImage(pathInfo, response);
}
// Anything else results in a 404.
else {
......@@ -101,15 +115,18 @@ public class PluginServlet extends HttpServlet {
Class servletClass = classMap.get(name);
Object instance = servletClass.newInstance();
if (instance instanceof HttpServlet) {
// Initialize the servlet then add it to the map..
((HttpServlet)instance).init(servletConfig);
servlets.put(pluginName + url, (HttpServlet)instance);
}
else {
Log.warn("Could not load " + (pluginName + url) + ": not a servlet.");
}
}
}
catch (Throwable e) {
e.printStackTrace();
}
//plugin.getClass().getClassLoader().loadClass("");
}
/**
......@@ -118,10 +135,11 @@ public class PluginServlet extends HttpServlet {
* servlet is found, a 404 error is returned.
*
* @param pathInfo the extra path info.
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @param request the request object.
* @param response the response object.
* @throws ServletException if a servlet exception occurs while handling the
* request.
* @throws IOException if an IOException occurs while handling the request.
*/
private void handleJSP(String pathInfo, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
......@@ -139,11 +157,49 @@ public class PluginServlet extends HttpServlet {
}
}
private void handleImage(String pathInfo, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
/**
* Handles a request for an image.
*
* @param pathInfo the extra path info.
* @param response the response object.
* @throws IOException if an IOException occurs while handling the request.
*/
private void handleImage(String pathInfo, HttpServletResponse response) throws IOException
{
File image = new File(pluginDirectory, pathInfo.replaceAll("/", File.separator));
if (!image.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
else {
// Content type will be GIF or PNG.
String contentType = "image/gif";
if (pathInfo.endsWith(".png")) {
contentType = "image/png";
}
response.setHeader("Content-disposition", "filename=\"" + image + "\";");
response.setContentType(contentType);
// Write out the image to the user.
InputStream in = null;
ServletOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(image));
out = response.getOutputStream();
// Set the size of the file.
response.setContentLength((int)image.length());
// Use a 1K buffer.
byte[] buf = new byte[1024];
int len;
while ((len=in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
finally {
try { in.close(); } catch (Exception ignored) {}
try { out.close(); } catch (Exception ignored) {}
}
}
}
}
\ No newline at end of file
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