Mercurial > hg > digilib-old
changeset 548:bc9196347188 digilibPDF
more refactoring and cleanup
author | robcast |
---|---|
date | Mon, 13 Dec 2010 15:35:25 +0100 |
parents | e1094c5ec032 |
children | b86a7b700bc3 |
files | client/digitallibrary/dlInfo-js.jsp client/digitallibrary/dlInfo-xml.jsp client/digitallibrary/server/dlConfig.jsp client/digitallibrary/server/dlRequest.jsp servlet/src/digilib/servlet/DigilibPDFWorker.java servlet/src/digilib/servlet/Initialiser.java servlet/src/digilib/servlet/PDFCache.java servlet/src/digilib/servlet/PDFFileWorker.java servlet/src/digilib/servlet/PDFJobDescription.java servlet/src/digilib/servlet/PDFJobInformation.java servlet/src/digilib/servlet/PDFMaker.java servlet/src/digilib/servlet/PDFStreamWorker.java servlet/src/digilib/servlet/PDFTitlePage.java servlet/src/digilib/servlet/ParameterMap.java |
diffstat | 14 files changed, 220 insertions(+), 194 deletions(-) [+] |
line wrap: on
line diff
--- a/client/digitallibrary/dlInfo-js.jsp Fri Dec 10 09:35:09 2010 +0100 +++ b/client/digitallibrary/dlInfo-js.jsp Mon Dec 13 15:35:25 2010 +0100 @@ -19,7 +19,7 @@ %> // Automatically generated JavaScript snippet with parameters <% - Object[] keys = dlRequest.keySet().toArray(); + Object[] keys = dlRequest.getParams().keySet().toArray(); java.util.Arrays.sort(keys); int l = keys.length; for (int i = 0; i < l; i++) {
--- a/client/digitallibrary/dlInfo-xml.jsp Fri Dec 10 09:35:09 2010 +0100 +++ b/client/digitallibrary/dlInfo-xml.jsp Mon Dec 13 15:35:25 2010 +0100 @@ -23,7 +23,7 @@ %><!-- Automatically generated XML snippet with document parameters --> <document-parameters> <% - Object[] keys = dlRequest.keySet().toArray(); + Object[] keys = dlRequest.getParams.keySet().toArray(); java.util.Arrays.sort(keys); int l = keys.length; for (int i = 0; i < l; i++) {
--- a/client/digitallibrary/server/dlConfig.jsp Fri Dec 10 09:35:09 2010 +0100 +++ b/client/digitallibrary/server/dlConfig.jsp Mon Dec 13 15:35:25 2010 +0100 @@ -42,7 +42,7 @@ <table> <% - Object[] keys = dlConfig.keySet().toArray(); + Object[] keys = dlConfig.getParams().keySet().toArray(); java.util.Arrays.sort(keys); int l = keys.length; for (int i = 0; i < l; i++) {
--- a/client/digitallibrary/server/dlRequest.jsp Fri Dec 10 09:35:09 2010 +0100 +++ b/client/digitallibrary/server/dlRequest.jsp Mon Dec 13 15:35:25 2010 +0100 @@ -93,7 +93,7 @@ <table> <% - Object[] keys = dlRequest.keySet().toArray(); + Object[] keys = dlRequest.getParams().keySet().toArray(); java.util.Arrays.sort(keys); int l = keys.length; for (int i = 0; i < l; i++) {
--- a/servlet/src/digilib/servlet/DigilibPDFWorker.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/DigilibPDFWorker.java Mon Dec 13 15:35:25 2010 +0100 @@ -51,9 +51,9 @@ private File outputfile = null; - private PDFJobInformation job_info = null; + private PDFJobDescription job_info = null; - public DigilibPDFWorker(DigilibConfiguration dlConfig, PDFJobInformation pdfji, File outputfile) { + public DigilibPDFWorker(DigilibConfiguration dlConfig, PDFJobDescription pdfji, File outputfile) { super(); // TODO dlConfig this.dlConfig = dlConfig; @@ -165,7 +165,7 @@ ImageJobDescription iji = job_info.getImageJobInformation(); iji.setValue("pn", pn); // create image worker - DigilibImageWorker1 image_worker = new DigilibImageWorker1(dlConfig, null, iji); + /* FIXME: DigilibImageWorker1 image_worker = new DigilibImageWorker1(dlConfig, null, iji); try { DocuImage img = image_worker.render(); @@ -194,6 +194,7 @@ logger.error(e.getMessage()); e.printStackTrace(); } + */ }
--- a/servlet/src/digilib/servlet/Initialiser.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/Initialiser.java Mon Dec 13 15:35:25 2010 +0100 @@ -46,6 +46,7 @@ * @author casties * */ +@SuppressWarnings("serial") public class Initialiser extends HttpServlet { /** servlet version */ @@ -61,13 +62,13 @@ DigilibConfiguration dlConfig; /** Executor for digilib image jobs */ - DigilibJobCenter imageEx; + DigilibJobCenter<DocuImage> imageEx; /** Executor for PDF jobs */ - DigilibJobCenter pdfEx; + DigilibJobCenter<OutputStream> pdfEx; /** Executor for PDF image jobs */ - DigilibJobCenter pdfImageEx; + DigilibJobCenter<DocuImage> pdfImageEx; /** * Initialisation on first run. @@ -144,7 +145,7 @@ int pmt = dlConfig.getAsInt("pdf-max-waiting-threads"); pdfEx = new DigilibJobCenter<OutputStream>(pnt, pmt, true); dlConfig.setValue("servlet.worker.pdfexecutor", pdfEx); - // digilib worker threads + // PDF image worker threads int pint = dlConfig.getAsInt("pdf-image-worker-threads"); int pimt = dlConfig.getAsInt("pdf-image-max-waiting-threads"); pdfImageEx = new DigilibJobCenter<DocuImage>(pint, pimt, true);
--- a/servlet/src/digilib/servlet/PDFCache.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/PDFCache.java Mon Dec 13 15:35:25 2010 +0100 @@ -3,8 +3,11 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.concurrent.Future; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; @@ -14,6 +17,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import digilib.image.DocuImage; + /** * A class for handling user requests for pdf documents from digilib images. * @@ -33,6 +38,8 @@ private DigilibJobCenter<OutputStream> pdfJobCenter = null; + private DigilibJobCenter<DocuImage> pdfImageJobCenter = null; + private File cache_directory = new File("cache"); private File temp_directory = new File("pdf_temp"); @@ -57,7 +64,7 @@ // TODO ? functionality for the pre-generation of complete books/chapters using default values - public void init(ServletConfig config) throws ServletException{ + public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println("***** Digital Image Library Image PDF-Cache Servlet (version " @@ -88,6 +95,7 @@ } pdfJobCenter = (DigilibJobCenter<OutputStream>) dlConfig.getValue("servlet.worker.pdfexecutor"); + pdfImageJobCenter = (DigilibJobCenter<DocuImage>) dlConfig.getValue("servlet.worker.pdfimageexecutor"); // register this instance globally context.setAttribute(instanceKey, this); @@ -106,14 +114,11 @@ } - - @Override public void processRequest(HttpServletRequest request, HttpServletResponse response) { // evaluate request ( make a PDFJobDeclaration , get the DocumentId) - PDFJobInformation pdfji = new PDFJobInformation(dlConfig); - pdfji.setWithRequest(request); + PDFJobDescription pdfji = new PDFJobDescription(request, dlConfig); String docid = pdfji.getDocumentId(); @@ -126,9 +131,11 @@ int status = getStatus(docid); if (status == STATUS_NONEXISTENT) { + // not there -- start creation createNewPdfDocument(pdfji, docid); notifyUser(status, docid, request, response); } else if (status == STATUS_DONE) { + // pdf created -- send it try { sendFile(docid, getDownloadFilename(pdfji), response); } catch (IOException e) { @@ -169,9 +176,9 @@ jsp = JSP_ERROR; } - RequestDispatcher dispatch = context.getRequestDispatcher(jsp); - try { + // forward to the relevant jsp + RequestDispatcher dispatch = context.getRequestDispatcher(jsp); dispatch.forward(request, response); } catch (ServletException e) { logger.debug(e.getMessage()); @@ -203,11 +210,20 @@ * * @param pdfji * @param filename + * @return + * @throws FileNotFoundException */ - public void createNewPdfDocument(PDFJobInformation pdfji, String filename){ + public Future<OutputStream> createNewPdfDocument(PDFJobDescription pdfji, String filename) throws FileNotFoundException{ // start new worker - PDFMaker pdf_maker = new PDFMaker(context, pdfji,filename); - new Thread(pdf_maker, "PDFMaker").start(); + File of = this.getTempFile(filename); + OutputStream os = new FileOutputStream(of); + PDFStreamWorker job = new PDFStreamWorker(dlConfig, os, pdfji, pdfImageJobCenter); + // start job + Future<OutputStream> jobTicket = pdfJobCenter.submit(job); + // what do we do with the result? + return jobTicket; + /* PDFMaker pdf_maker = new PDFMaker(context, pdfji,filename); + new Thread(pdf_maker, "PDFMaker").start();*/ } @@ -217,7 +233,7 @@ * @param pdfji * @return */ - public String getDownloadFilename(PDFJobInformation pdfji){ + public String getDownloadFilename(PDFJobDescription pdfji){ // filename example: digilib_example_pgs1-3.pdf String filename; filename = "digilib_";
--- a/servlet/src/digilib/servlet/PDFFileWorker.java Fri Dec 10 09:35:09 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -package digilib.servlet; - -import java.io.OutputStream; -import java.util.concurrent.Callable; - -import digilib.image.DocuImage; - -public class PDFFileWorker extends PDFStreamWorker implements Callable<OutputStream> { - - public PDFFileWorker(DigilibConfiguration dlConfig, - OutputStream outputfile, PDFJobInformation job_info, - DigilibJobCenter<DocuImage> imageJobCenter) { - super(dlConfig, outputfile, job_info, imageJobCenter); - // TODO Auto-generated constructor stub - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/servlet/src/digilib/servlet/PDFJobDescription.java Mon Dec 13 15:35:25 2010 +0100 @@ -0,0 +1,150 @@ +package digilib.servlet; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.log4j.Logger; + +import digilib.io.DocuDirectory; +import digilib.io.FileOps; + + +/** + * A container class for storing a set of instruction parameters + * used for content generator classes like MakePDF. + * + * + * @author cmielack, casties + * + */ +public class PDFJobDescription extends ParameterMap { + + String[] parameter_list = {"pgs"}; // all other parameters get passed into an extra ImageJobInformation + // (this should be redesigned later...) + + + ImageJobDescription image_info = null; + DigilibConfiguration dlConfig = null; + NumRange pages = null; + /** gengeral logger for this class */ + protected static Logger logger = Logger.getLogger("digilib.servlet"); + + + /** + * Initialize the PDFJobInformation + * + * @param dlcfg + * The DigilibConfiguration. + */ + public PDFJobDescription(DigilibConfiguration dlcfg) { + super(30); + + // page numbers + newParameter("pgs", "", null, 's'); + dlConfig = dlcfg; + + } + + /** + * Initialize the PDFJobInformation with a request. + * + * @param dlcfg The DigilibConfiguration. + * @param request + */ + public PDFJobDescription(HttpServletRequest request, DigilibConfiguration dlcfg) { + super(30); + dlConfig = dlcfg; + this.setWithRequest(request); + } + + + /** + * Read in the request object. + * + * @param request + */ + public void setWithRequest(HttpServletRequest request) { + image_info = new ImageJobDescription(dlConfig); + // FIXME: image_info.setWithRequest(request); + + for (String param : parameter_list){ + if (request.getParameterMap().containsKey(param)){ + setValueFromString(param, request.getParameter(param)); + } + } + // process parameters + try { + pages = new NumRange(getAsString("pgs")); + DocuDirectory dir = image_info.getFileDirectory(); + int dirsize = dir.size(FileOps.CLASS_IMAGE); + pages.setMaxnum(dirsize); + } catch (Exception e) { + logger.warn("Problem with parsing page numbers: "+e.toString()); + } + } + + + /** + * Generate the filename of the pdf to be created. + * + * @return + */ + public String getDocumentId(){ + String id; + + // TODO use complete request information for id generation + + if(this.image_info!=null){ + String fn = image_info.getAsString("fn"); + String dh = image_info.getAsString("dh"); + String pgs = getAsString("pgs"); + + id = "fn=" + fn + "&dh=" + dh + "&pgs=" + pgs + ".pdf"; + // make safe to use as filename by urlencoding + try { + id = URLEncoder.encode(id, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + else { + id = null; + } + + return id; + } + + + public ImageJobDescription getImageJobInformation(){ + ImageJobDescription new_image_info = new ImageJobDescription(image_info, dlConfig); + return new_image_info; + } + + + public NumRange getPages() { + return pages; + } + + + /** + * Check parameters for validity. + * Returns true if no errors are found. + * + * @return + */ + public boolean checkValidity(){ + if (pages != null) { + return true; + } + return false; + } + + public DigilibConfiguration getDlConfig(){ + return dlConfig; + } + +} \ No newline at end of file
--- a/servlet/src/digilib/servlet/PDFJobInformation.java Fri Dec 10 09:35:09 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -package digilib.servlet; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.log4j.Logger; - -import digilib.io.DocuDirectory; -import digilib.io.FileOps; - - -/** - * A container class for storing a set of instruction parameters - * used for content generator classes like MakePDF. - * - * - * @author cmielack - * - */ -@SuppressWarnings("serial") -public class PDFJobInformation extends ParameterMap { - - String[] parameter_list = {"pgs"}; // all other parameters get passed into an extra ImageJobInformation - // (this should be redesigned later...) - - - ImageJobDescription image_info = null; - DigilibConfiguration dlConfig = null; - NumRange pages = null; - /** gengeral logger for this class */ - protected static Logger logger = Logger.getLogger("digilib.servlet"); - - - /** - * Initialize the PDFJobInformation - * - * @param dlcfg - * The DigilibConfiguration. - */ - public PDFJobInformation(DigilibConfiguration dlcfg) { - super(30); - - // page numbers - newParameter("pgs", "", null, 's'); - dlConfig = dlcfg; - - } - - - /** - * Read in the request object. - * - * @param request - */ - public void setWithRequest(HttpServletRequest request) { - image_info = new ImageJobDescription(dlConfig); - // FIXME: image_info.setWithRequest(request); - - for (String param : parameter_list){ - if (request.getParameterMap().containsKey(param)){ - setValueFromString(param, request.getParameter(param)); - } - } - // process parameters - try { - pages = new NumRange(getAsString("pgs")); - DocuDirectory dir = image_info.getFileDirectory(); - int dirsize = dir.size(FileOps.CLASS_IMAGE); - pages.setMaxnum(dirsize); - } catch (Exception e) { - logger.warn("Problem with parsing page numbers: "+e.toString()); - } - } - - - /** - * Generate the filename of the pdf to be created. - * - * @return - */ - public String getDocumentId(){ - String id; - - // TODO use complete request information for id generation - - if(this.image_info!=null){ - String fn = image_info.getAsString("fn"); - String dh = image_info.getAsString("dh"); - String pgs = getAsString("pgs"); - - id = "fn=" + fn + "&dh=" + dh + "&pgs=" + pgs + ".pdf"; - // make safe to use as filename by urlencoding - try { - id = URLEncoder.encode(id, "UTF-8"); - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - else { - id = null; - } - - return id; - } - - - public ImageJobDescription getImageJobInformation(){ - ImageJobDescription new_image_info = new ImageJobDescription(image_info, dlConfig); - return new_image_info; - } - - - public NumRange getPages() { - return pages; - } - - - /** - * Check parameters for validity. - * Returns true if no errors are found. - * - * @return - */ - public boolean checkValidity(){ - if (pages != null) { - return true; - } - return false; - } - - public DigilibConfiguration getDlConfig(){ - return dlConfig; - } - -} \ No newline at end of file
--- a/servlet/src/digilib/servlet/PDFMaker.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/PDFMaker.java Mon Dec 13 15:35:25 2010 +0100 @@ -22,7 +22,7 @@ public static String version = "0.1"; - private PDFJobInformation job_info = null; + private PDFJobDescription job_info = null; private String filename = null; private DigilibConfiguration dlConfig = null; private ServletContext context = null; @@ -32,7 +32,7 @@ - public PDFMaker(ServletContext context, PDFJobInformation pdfji, String filename){ + public PDFMaker(ServletContext context, PDFJobDescription pdfji, String filename){ this.job_info = pdfji; this.filename = filename; this.dlConfig = pdfji.getDlConfig();
--- a/servlet/src/digilib/servlet/PDFStreamWorker.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/PDFStreamWorker.java Mon Dec 13 15:35:25 2010 +0100 @@ -18,17 +18,17 @@ public class PDFStreamWorker implements Callable<OutputStream> { - private static Logger logger = Logger.getLogger(PDFStreamWorker.class); + protected static Logger logger = Logger.getLogger(PDFStreamWorker.class); - private DigilibConfiguration dlConfig = null; + protected DigilibConfiguration dlConfig = null; - private Document doc = null; + protected Document doc = null; - private OutputStream outstream = null; + protected OutputStream outstream = null; - private PDFJobInformation job_info = null; + protected PDFJobDescription job_info = null; - private DigilibJobCenter<DocuImage> imageJobCenter = null; + protected DigilibJobCenter<DocuImage> imageJobCenter = null; /** * @param dlConfig @@ -36,7 +36,7 @@ * @param job_info */ public PDFStreamWorker(DigilibConfiguration dlConfig, OutputStream outputfile, - PDFJobInformation job_info, + PDFJobDescription job_info, DigilibJobCenter<DocuImage> imageJobCenter) { super(); this.dlConfig = dlConfig; @@ -45,7 +45,6 @@ this.imageJobCenter = imageJobCenter; } - @Override public OutputStream call() throws Exception { outstream = renderPDF(); return outstream; @@ -77,7 +76,6 @@ + (System.currentTimeMillis() - start_time) + "ms)"); start_time = System.currentTimeMillis(); - // Integer[] pgs = job_info.getPageNrs();//get_pgs(); NumRange pgs = job_info.getPages(); for (int p : pgs) { @@ -139,16 +137,16 @@ throws InterruptedException, ExecutionException, IOException, DocumentException { // create image worker - ImageWorker image_worker = new ImageWorker(dlConfig, iji); + ImageWorker job = new ImageWorker(dlConfig, iji); // submit - Future<DocuImage> jobTicket = imageJobCenter.submit(image_worker); + Future<DocuImage> jobTicket = imageJobCenter.submit(job); // wait for result DocuImage img = jobTicket.get(); // scale the image Image pdfimg = Image.getInstance(img.getAwtImage(), null); float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder(); float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder(); - // TODO: do we really want to scale this again? + // TODO: do we really scale this again? pdfimg.scaleToFit(docW, docH); // add to PDF doc.add(pdfimg);
--- a/servlet/src/digilib/servlet/PDFTitlePage.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/PDFTitlePage.java Mon Dec 13 15:35:25 2010 +0100 @@ -23,7 +23,7 @@ */ public class PDFTitlePage { - private PDFJobInformation job_info = null; + private PDFJobDescription job_info = null; private DigilibInfoReader info_reader= null; private DocuDirCache dirCache = null; protected static Logger logger = Logger.getLogger("digilib.servlet"); @@ -33,7 +33,7 @@ * Initialize a TitlePage * @param pdfji */ - public PDFTitlePage(PDFJobInformation pdfji){ + public PDFTitlePage(PDFJobDescription pdfji){ job_info = pdfji; dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache");
--- a/servlet/src/digilib/servlet/ParameterMap.java Fri Dec 10 09:35:09 2010 +0100 +++ b/servlet/src/digilib/servlet/ParameterMap.java Mon Dec 13 15:35:25 2010 +0100 @@ -270,4 +270,20 @@ public boolean hasOption(String opt) { return options.hasOption(opt); } + + public HashMap<String, Parameter> getParams() { + return params; + } + + public void setParams(HashMap<String, Parameter> params) { + this.params = params; + } + + public OptionsSet getOptions() { + return options; + } + + public void setOptions(OptionsSet options) { + this.options = options; + } }