Search in sources :

Example 6 with DocuImage

use of digilib.image.DocuImage in project digilib by robcast.

the class ScalerNoThread method processRequest.

/**
 * Service this request using the response.
 *
 * @param request
 * @param response
 * @throws ServletException
 */
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    if (dlConfig == null) {
        logger.error("ERROR: No Configuration!");
        throw new ServletException("NO VALID digilib CONFIGURATION!");
    }
    accountlog.debug("request: " + request.getQueryString());
    logger.debug("request: " + request.getQueryString());
    long startTime = System.currentTimeMillis();
    // parse request
    DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig);
    // type of error reporting
    ErrMsg errMsgType = ErrMsg.IMAGE;
    if (dlRequest.hasOption(DigilibOption.errtxt)) {
        errMsgType = ErrMsg.TEXT;
    } else if (dlRequest.hasOption(DigilibOption.errcode)) {
        errMsgType = ErrMsg.CODE;
    }
    try {
        // extract the job information
        ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
        /*
             * check if we can fast-track without scaling
             */
        ImageInput fileToLoad = (ImageInput) jobTicket.getInput();
        // check permissions
        if (useAuthorization) {
            // is the current request/user authorized?
            if (!authzOp.isAuthorized(dlRequest)) {
                // send deny answer and abort
                throw new AuthOpException();
            }
        }
        // if requested, send image as a file
        if (sendFileAllowed && jobTicket.getSendAsFile()) {
            String mt = null;
            if (jobTicket.hasOption(DigilibOption.rawfile)) {
                mt = "application/octet-stream";
            }
            logger.debug("Sending RAW File as is.");
            ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger);
            logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
            return;
        }
        // it
        if (!jobTicket.isTransformRequired()) {
            logger.debug("Sending File as is.");
            ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger);
            logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
            return;
        }
        // create job
        ImageWorker job = new ImageWorker(dlConfig, jobTicket);
        // get result immediately
        DocuImage img = job.call();
        // forced destination image type
        String mt = null;
        if (jobTicket.hasOption(DigilibOption.jpg)) {
            mt = "image/jpeg";
        } else if (jobTicket.hasOption(DigilibOption.png)) {
            mt = "image/png";
        }
        // send image
        ServletOps.sendImage(img, mt, response, logger);
        logger.debug("Job Processing Time: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (ImageOpException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.IMAGE, null, response);
    } catch (IOException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.FILE, null, response);
    } catch (AuthOpException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.AUTH, null, response);
    }
}
Also used : ServletException(javax.servlet.ServletException) ImageOpException(digilib.image.ImageOpException) DigilibServletRequest(digilib.conf.DigilibServletRequest) ImageJobDescription(digilib.image.ImageJobDescription) ImageInput(digilib.io.ImageInput) AuthOpException(digilib.auth.AuthOpException) ImageWorker(digilib.image.ImageWorker) DocuImage(digilib.image.DocuImage) IOException(java.io.IOException)

Example 7 with DocuImage

use of digilib.image.DocuImage in project digilib by robcast.

the class AsyncServletWorker method run.

/**
 * runs the ImageWorker and writes the image to the ServletResponse.
 */
@Override
public void run() {
    try {
        /*
             * render the image
             */
        DocuImage img = imageWorker.call();
        if (completed) {
            logger.debug("AsyncServletWorker already completed (after scaling)!");
            return;
        }
        /*
             * set forced destination image type
             */
        String mt = null;
        if (jobinfo.hasOption(DigilibOption.jpg)) {
            mt = "image/jpeg";
        } else if (jobinfo.hasOption(DigilibOption.png)) {
            mt = "image/png";
        }
        /*
             *  send the image
             */
        HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
        ServletOps.sendImage(img, mt, response, logger);
        logger.debug("Job done in: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (ImageOpException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        Scaler.digilibError(errMsgType, Error.IMAGE, null, (HttpServletResponse) asyncContext.getResponse());
    } catch (IOException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        Scaler.digilibError(errMsgType, Error.FILE, null, (HttpServletResponse) asyncContext.getResponse());
    } catch (ServletException e) {
        logger.error("Servlet error: ", e);
    } catch (Exception e) {
        logger.error("Other error: ", e);
    } finally {
        if (completed) {
            logger.debug("AsyncServletWorker already completed (finally)!");
        } else {
            // submit response
            logger.debug("context complete.");
            completed = true;
            asyncContext.complete();
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) ImageOpException(digilib.image.ImageOpException) DocuImage(digilib.image.DocuImage) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ImageOpException(digilib.image.ImageOpException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 8 with DocuImage

use of digilib.image.DocuImage in project digilib by robcast.

the class PDFServletConfiguration method contextDestroyed.

/* (non-Javadoc)
     * @see digilib.conf.DigilibServletConfiguration#contextDestroyed(javax.servlet.ServletContextEvent)
     */
@Override
public void contextDestroyed(ServletContextEvent sce) {
    super.contextDestroyed(sce);
    ServletContext context = sce.getServletContext();
    DigilibServletConfiguration config = PDFServletConfiguration.getCurrentConfig(context);
    @SuppressWarnings("unchecked") DigilibJobCenter<DocuImage> pdfExecutor = (DigilibJobCenter<DocuImage>) config.getValue(PDF_EXECUTOR_KEY);
    if (pdfExecutor != null) {
        // shut down pdf thread pool
        List<Runnable> rj = pdfExecutor.shutdownNow();
        int nrj = rj.size();
        if (nrj > 0) {
            logger.error("Still running threads when shutting down PDF job queue: " + nrj);
        }
    }
    @SuppressWarnings("unchecked") DigilibJobCenter<DocuImage> pdfImageExecutor = (DigilibJobCenter<DocuImage>) config.getValue(PDF_IMAGEEXECUTOR_KEY);
    if (pdfImageExecutor != null) {
        // shut down pdf image thread pool
        List<Runnable> rj = pdfImageExecutor.shutdownNow();
        int nrj = rj.size();
        if (nrj > 0) {
            logger.error("Still running threads when shutting down PDF-image job queue: " + nrj);
        }
    }
}
Also used : ServletContext(javax.servlet.ServletContext) DocuImage(digilib.image.DocuImage) DigilibJobCenter(digilib.util.DigilibJobCenter)

Example 9 with DocuImage

use of digilib.image.DocuImage in project digilib by robcast.

the class PDFServletConfiguration method configurePdfServlet.

/**
 * @param config
 * @param context
 */
protected void configurePdfServlet(ServletContext context) {
    PDFServletConfiguration config = this;
    // PDF worker threads
    int pnt = config.getAsInt("pdf-worker-threads");
    int pmt = config.getAsInt("pdf-max-waiting-threads");
    DigilibJobCenter<OutputStream> pdfExecutor = new DigilibJobCenter<OutputStream>(pnt, pmt, false, "servlet.worker.pdfexecutor");
    config.setValue(PDF_EXECUTOR_KEY, pdfExecutor);
    // PDF image worker threads
    int pint = config.getAsInt("pdf-image-worker-threads");
    int pimt = config.getAsInt("pdf-image-max-waiting-threads");
    DigilibJobCenter<DocuImage> pdfImageExecutor = new DigilibJobCenter<DocuImage>(pint, pimt, false, "servlet.worker.pdfimageexecutor");
    config.setValue(PDF_IMAGEEXECUTOR_KEY, pdfImageExecutor);
    /*
         * set up temporary directories
         */
    String temp_fn = config.getAsString("pdf-temp-dir");
    File temp_directory = new File(temp_fn);
    if (!temp_directory.exists()) {
        // try to create
        temp_directory.mkdirs();
    } else {
        // rid the temporary directory of possible incomplete document files
        FileOps.emptyDirectory(temp_directory);
    }
    config.setValue(PDF_WORKDIR_KEY, temp_directory);
    String cache_fn = config.getAsString("pdf-cache-dir");
    File cache_directory = new File(cache_fn);
    if (!cache_directory.exists()) {
        // try to create
        cache_directory.mkdirs();
    }
    config.setValue(PDF_CACHEDIR_KEY, cache_directory);
    /*
         * set as the PDF servlets main config
         */
    context.setAttribute(PDF_SERVLET_CONFIG_KEY, this);
}
Also used : OutputStream(java.io.OutputStream) DocuImage(digilib.image.DocuImage) DigilibJobCenter(digilib.util.DigilibJobCenter) File(java.io.File)

Example 10 with DocuImage

use of digilib.image.DocuImage in project digilib by robcast.

the class DigilibConfiguration method configure.

/**
 * Configure digilib.
 *
 * Sets up Factories and Singletons using the configuration.
 */
@SuppressWarnings("unchecked")
public void configure() {
    DigilibConfiguration config = this;
    setupLogger();
    /*
         * initialise static DocuImage class instance
         */
    try {
        Class<DocuImage> docuImageClass = (Class<DocuImage>) Class.forName(config.getAsString("docuimage-class"));
        DocuImageFactory.setDocuImageClass(docuImageClass);
        // DocuImage class instance
        DocuImage di = DocuImageFactory.getInstance();
        config.newParameter("servlet.docuimage.class", docuImageClass, null, 's');
        config.newParameter("servlet.docuimage.version", di.getVersion(), null, 's');
        logger.debug("DocuImage (" + docuImageClass + ") " + di.getVersion());
        // set hacks on instance
        try {
            docuImageClass.newInstance().setHacks(config.getAsString("docuimage-hacks"));
        } catch (InstantiationException | IllegalAccessException e) {
            logger.error("Error creating instance of DocuImage class!");
        }
        // log supported formats
        StringBuilder fmts = new StringBuilder();
        Iterator<String> dlfs = di.getSupportedFormats();
        for (String f = dlfs.next(); dlfs.hasNext(); f = dlfs.next()) {
            fmts.append(f);
            fmts.append(", ");
        }
        logger.info("DocuImage supported image formats: " + fmts);
    } catch (ClassNotFoundException e) {
        logger.error("Error setting DocuImage class!");
    }
    // disk cache for image toolkit
    boolean dc = getAsBoolean("img-diskcache-allowed");
    // TODO: methods for all toolkits?
    ImageIO.setUseCache(dc);
}
Also used : DocuImage(digilib.image.DocuImage)

Aggregations

DocuImage (digilib.image.DocuImage)11 IOException (java.io.IOException)5 DigilibJobCenter (digilib.util.DigilibJobCenter)4 ServletContext (javax.servlet.ServletContext)4 ServletException (javax.servlet.ServletException)4 ImageOpException (digilib.image.ImageOpException)3 File (java.io.File)3 AuthOpException (digilib.auth.AuthOpException)2 ImageWorker (digilib.image.ImageWorker)2 Image (com.itextpdf.text.Image)1 AuthnOps (digilib.auth.AuthnOps)1 AuthzOps (digilib.auth.AuthzOps)1 DigilibServletRequest (digilib.conf.DigilibServletRequest)1 ImageJobDescription (digilib.image.ImageJobDescription)1 AliasingDocuDirCache (digilib.io.AliasingDocuDirCache)1 DocuDirCache (digilib.io.DocuDirCache)1 DocuDirectory (digilib.io.DocuDirectory)1 FileClass (digilib.io.FileOps.FileClass)1 ImageInput (digilib.io.ImageInput)1 DirMeta (digilib.meta.DirMeta)1