Mercurial > hg > digilib-old
diff servlet/src/digilib/servlet/ServletOps.java @ 290:5d0c0da080ec gen2 scaleable_1
digilib servlet version 2 ("scaleable digilib")
- first stab at using thread pools to limit resource use
- using Dug Leas util.concurrent
- doesn't mix with tomcat :-(
author | robcast |
---|---|
date | Thu, 21 Oct 2004 20:53:37 +0200 |
parents | 0ff3ede32060 |
children | 1cec876f2788 |
line wrap: on
line diff
--- a/servlet/src/digilib/servlet/ServletOps.java Mon Oct 18 15:40:54 2004 +0200 +++ b/servlet/src/digilib/servlet/ServletOps.java Thu Oct 21 20:53:37 2004 +0200 @@ -1,156 +1,217 @@ -/* ServletOps -- Servlet utility class - - Digital Image Library servlet components - - Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2 of the License, or (at your - option) any later version. - - Please read license.txt for the full details. A copy of the GPL - may be found at http://www.gnu.org/copyleft/lgpl.html - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -*/ +/* + * ServletOps -- Servlet utility class + * + * Digital Image Library servlet components + * + * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * Please read license.txt for the full details. A copy of the GPL may be found + * at http://www.gnu.org/copyleft/lgpl.html + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + * + */ package digilib.servlet; -import javax.servlet.*; -import javax.servlet.http.*; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.StringTokenizer; -import digilib.*; -import digilib.io.*; +import javax.servlet.ServletConfig; +import javax.servlet.http.HttpServletResponse; +import org.apache.log4j.Logger; + +import EDU.oswego.cs.dl.util.concurrent.Executor; +import digilib.io.FileOpException; +import digilib.io.FileOps; public class ServletOps { - private Utils util = null; - private Hashtable confTable = null; - - public ServletOps() { - util = new Utils(); - } + private static Logger logger = Logger.getLogger("servlet.op"); - public ServletOps(Utils u) { - util = u; - } + /** + * convert a string with a list of pathnames into an array of strings using + * the system's path seperator string + */ + public static String[] getPathArray(String paths) { + // split list into directories + StringTokenizer dirs = new StringTokenizer(paths, + java.io.File.pathSeparator); + int n = dirs.countTokens(); + if (n < 1) { + return null; + } + // add directories into array + String[] pathArray = new String[n]; + for (int i = 0; i < n; i++) { + pathArray[i] = dirs.nextToken(); + } + return pathArray; + } - public ServletOps(Utils u, ServletConfig sc) throws ServletException { - util = u; - setConfig(sc); - } - - public void setUtils(Utils u) { - util = u; - } + /** + * get a real File for a config File. + * + * If the File is not absolute the path is appended to the WEB-INF directory + * of the web-app. + * + * @param file + * @param sc + * @return + */ + public static File getConfigFile(File f, ServletConfig sc) { + // is the filename absolute? + if (!f.isAbsolute()) { + // relative path -> use getRealPath to resolve in WEB-INF + String fn = sc.getServletContext().getRealPath( + "WEB-INF/" + f.getPath()); + f = new File(fn); + } + return f; + } - /** - * read parameter list from the XML file in init parameter "config-file" - */ - public void setConfig(ServletConfig c) throws ServletException { - // reset parameter table - confTable = null; - if (c == null) { - return; - } - // get config file name - String fn = c.getInitParameter("config-file"); - if (fn == null) { - util.dprintln(4, "setConfig: no param config-file"); - return; - } - File f = new File(fn); - // setup config file list reader - XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); - try { - confTable = lilo.loadURL(f.toURL().toString()); - } catch (Exception e) { - util.dprintln(4, "setConfig: unable to read file "+fn); - throw new ServletException(e); - } - } + /** + * get a real file name for a config file pathname. + * + * If filename starts with "/" its treated as absolute else the path is + * appended to the WEB-INF directory of the web-app. + * + * @param filename + * @param sc + * @return + */ + public static String getConfigFile(String filename, ServletConfig sc) { + File f = new File(filename); + // is the filename absolute? + if (!f.isAbsolute()) { + // relative path -> use getRealPath to resolve in WEB-INF + filename = sc.getServletContext() + .getRealPath("WEB-INF/" + filename); + } + return filename; + } - /** - * print a servlet response and exit - */ - public static void htmlMessage(String s, HttpServletResponse response) throws IOException { - response.setContentType("text/html; charset=iso-8859-1"); - PrintWriter out = response.getWriter(); - out.println("<html>"); - out.println("<head><title>Scaler</title></head>"); - out.println("<body>"); - out.println("<p>"+s+"</p>"); - out.println("</body></html>"); - } + /** + * print a servlet response and exit + */ + public static void htmlMessage(String msg, HttpServletResponse response) + throws IOException { + htmlMessage("Scaler", msg, response); + } + + /** + * print a servlet response and exit + */ + public static void htmlMessage(String title, String msg, + HttpServletResponse response) throws IOException { + response.setContentType("text/html; charset=iso-8859-1"); + PrintWriter out = response.getWriter(); + out.println("<html>"); + out.println("<head><title>" + title + "</title></head>"); + out.println("<body>"); + out.println("<p>" + msg + "</p>"); + out.println("</body></html>"); + } - /** - * get a parameter from request and return it if set, otherwise return default - */ - public int tryToGetParam(String s, int i, HttpServletRequest r) { - try { - i = Integer.parseInt(r.getParameter(s)); - } catch(Exception e) { - util.dprintln(4, "trytoGetParam(int) failed on param "+s); - //e.printStackTrace(); - } - return i; - } - public float tryToGetParam(String s, float f, HttpServletRequest r) { - try { - f = Float.parseFloat(r.getParameter(s)); - } catch(Exception e) { - util.dprintln(4, "trytoGetParam(float) failed on param "+s); - //e.printStackTrace(); - } - return f; - } - public String tryToGetParam(String s, String x, HttpServletRequest r) { - if (r.getParameter(s) != null) { - x = r.getParameter(s); - } else { - util.dprintln(4, "trytoGetParam(string) failed on param "+s); - } - return x; - } - + /** + * Transfers an image file as-is with the mime type mt. + * + * The local file is copied to the <code>OutputStream</code> of the + * <code>ServletResponse</code>. If mt is null then the mime-type is + * auto-detected with mimeForFile. + * + * @param mt + * mime-type of the file. + * @param f + * Image file to be sent. + * @param res + * ServletResponse where the image file will be sent. + * @throws FileOpException + * Exception is thrown for a IOException. + */ + public static void sendFileImmediately(File f, String mt, + HttpServletResponse response) throws FileOpException { + logger.debug("sendRawFile(" + mt + ", " + f + ")"); + if (mt == null) { + // auto-detect mime-type + mt = FileOps.mimeForFile(f); + if (mt == null) { + throw new FileOpException("Unknown file type."); + } + } + response.setContentType(mt); + // open file + try { + if (mt.equals("application/octet-stream")) { + response.addHeader("Content-Disposition", + "attachment; filename=\"" + f.getName() + "\""); + } + FileInputStream inFile = new FileInputStream(f); + OutputStream outStream = response.getOutputStream(); + byte dataBuffer[] = new byte[4096]; + int len; + while ((len = inFile.read(dataBuffer)) != -1) { + // copy out file + outStream.write(dataBuffer, 0, len); + } + inFile.close(); + response.flushBuffer(); + } catch (IOException e) { + throw new FileOpException("Unable to send file."); + } + } - /** - * get an init parameter from config and return it if set, otherwise return default - */ - public int tryToGetInitParam(String s, int i) { - //System.out.println("trytogetInitParam("+s+", "+i+")"); - try { - //System.out.println("trytogetInitParam: "+(String)confTable.get(s)); - i = Integer.parseInt((String)confTable.get(s)); - } catch(Exception e) { - util.dprintln(4, "trytogetInitParam(int) failed on param "+s); - //e.printStackTrace(); - } - return i; - } - public float tryToGetInitParam(String s, float f) { - try { - f = Float.parseFloat((String)confTable.get(s)); - } catch(Exception e) { - util.dprintln(4, "trytoGetInitParam(float) failed on param "+s); - //e.printStackTrace(); - } - return f; - } - public String tryToGetInitParam(String s, String x) { - if ((confTable != null)&&((String)confTable.get(s) != null)) { - x = (String)confTable.get(s); - } else { - util.dprintln(4, "trytoGetInitParam(string) failed on param "+s); - } - return x; - } + /** + * Transfers an image file as-is with the mime type mt using a work queue. + * + * The local file is copied to the <code>OutputStream</code> of the + * <code>ServletResponse</code>. If mt is null then the mime-type is + * auto-detected with mimeForFile. + * + * @param mt + * mime-type of the file. + * @param f + * Image file to be sent. + * @param res + * ServletResponse where the image file will be sent. + * @throws FileOpException + * Exception is thrown for a IOException. + */ + public static void sendFile(File f, String mimetype, + HttpServletResponse response, Executor workQueue) + throws FileOpException { + // we're cheating + sendFileImmediately(f, mimetype, response); + /* + // create worker + DigilibSender job = new DigilibSender(f, null, response); + try { + logger.debug("queue size: " + + ((DigilibManager) workQueue).getQueueSize()); + workQueue.execute(job); + logger.debug("job sent!"); + synchronized (job) { + while (job.isBusy()) { + job.wait(); + } + } + } catch (InterruptedException e) { + throw new FileOpException("INTERRUPTED: Unable to send file. " + e); + } + */ -} + } + +} \ No newline at end of file