Search in sources :

Example 6 with DocuDirectory

use of digilib.io.DocuDirectory in project digilib by robcast.

the class DocumentBean method setRequest.

/**
 * Sets the current DigilibRequest. Also completes information in the request.
 *
 * @param dlRequest
 *            The dlRequest to set.
 */
public void setRequest(DigilibServletRequest dlRequest) throws Exception {
    this.dlRequest = dlRequest;
    if (dirCache == null) {
        return;
    }
    String fn = dlRequest.getFilePath();
    // get information about the file
    ImageSet fileset = (ImageSet) dirCache.getFile(fn, dlRequest.getAsInt("pn"));
    if (fileset == null) {
        return;
    }
    // add file name
    dlRequest.setValue("img.fn", fileset);
    // add dpi
    dlRequest.setValue("img.dpix", new Double(fileset.getResX()));
    dlRequest.setValue("img.dpiy", new Double(fileset.getResY()));
    // get number of pages in directory
    DocuDirectory dd = dirCache.getDirectory(fn);
    if (dd != null) {
        // add pt
        dlRequest.setValue("pt", dd.size());
    }
    // get original pixel size
    ImageInput origfile = fileset.getBiggest();
    // check image for size (TODO: just if mo=hires?)
    ImageSize pixsize = origfile.getSize();
    if (pixsize != null) {
        // add pixel size
        dlRequest.setValue("img.pix_x", new Integer(pixsize.getWidth()));
        dlRequest.setValue("img.pix_y", new Integer(pixsize.getHeight()));
    }
}
Also used : ImageInput(digilib.io.ImageInput) ImageSize(digilib.util.ImageSize) DocuDirectory(digilib.io.DocuDirectory) ImageSet(digilib.io.ImageSet)

Example 7 with DocuDirectory

use of digilib.io.DocuDirectory in project digilib by robcast.

the class Manifester method getLastModified.

/**
 * Returns modification time relevant to the request for caching.
 *
 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.HttpServletRequest)
 */
public long getLastModified(HttpServletRequest request) {
    accountlog.debug("GetLastModified from " + request.getRemoteAddr() + " for " + request.getQueryString());
    long mtime = -1;
    try {
        // create new digilib request
        DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig, EnumSet.of(ParsingOption.omitIiifImageApi));
        // get list of IIIF parameters
        @SuppressWarnings("unchecked") List<String> iiifParams = (List<String>) dlRequest.getValue("request.iiif.elements");
        // get identifier (first parameter)
        String identifier = iiifParams.get(0);
        // decode identifier to file path
        dlRequest.setValueFromString("fn", dlRequest.decodeIiifIdentifier(identifier));
        DocuDirectory dd = dirCache.getDirectory(dlRequest.getFilePath());
        if (dd != null) {
            // return rounded modification date of directory
            mtime = dd.getDirMTime() / 1000 * 1000;
        }
    } catch (Exception e) {
        logger.error("error in getLastModified: " + e.getMessage());
    }
    logger.debug("  returns " + mtime);
    return mtime;
}
Also used : DigilibServletRequest(digilib.conf.DigilibServletRequest) DocuDirectory(digilib.io.DocuDirectory) List(java.util.List) ImageOpException(digilib.image.ImageOpException) ServletException(javax.servlet.ServletException) AuthOpException(digilib.auth.AuthOpException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 8 with DocuDirectory

use of digilib.io.DocuDirectory in project digilib by robcast.

the class Manifester method processRequest.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
    try {
        // create DigilibRequest from ServletRequest, omit IIIF Image API parsing
        DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig, EnumSet.of(ParsingOption.omitIiifImageApi));
        // get list of IIIF parameters
        @SuppressWarnings("unchecked") List<String> iiifParams = (List<String>) dlRequest.getValue("request.iiif.elements");
        if (iiifParams == null) {
            logger.error("Invalid IIIF request.");
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid IIIF request.");
            return;
        }
        // get identifier (first parameter)
        // allow empty identifier for image root dir
        String identifier = "";
        if (iiifParams.size() > 0) {
            identifier = iiifParams.get(0);
        }
        // decode identifier to file path
        dlRequest.setValueFromString("fn", dlRequest.decodeIiifIdentifier(identifier));
        // get directory path
        String dlFn = dlRequest.getFilePath();
        // get information about the directory
        DocuDirectory dlDir = dirCache.getDirectory(dlFn);
        if (dlDir == null) {
            logger.error("Directory for manifest not found: " + dlFn);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // check for existing manifest file
        File mfFile = new File(dlDir.getDir(), "manifest.json");
        // check for image files
        if ((dlDir.size() == 0) && !mfFile.canRead()) {
            logger.debug("Directory has no files: " + dlFn);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        ManifestParams params = new ManifestParams();
        /*
			 * set CORS header ACAO "*" for info response as per IIIF spec
			 */
        if (corsForInfoRequests) {
            String origin = request.getHeader("Origin");
            if (origin != null) {
                response.setHeader("Access-Control-Allow-Origin", "*");
            }
        }
        /*
             * check permissions
             */
        if (useAuthorization) {
            // is the current request/user authorized?
            if (!authzOp.isAuthorized(dlRequest)) {
                // send deny answer and abort
                throw new AuthOpException("Access denied!");
            }
        }
        // use JSON-LD content type only when asked
        String accept = request.getHeader("Accept");
        if (accept != null && accept.contains("application/ld+json")) {
            response.setContentType("application/ld+json");
        } else {
            response.setContentType("application/json");
        }
        if (mfFile.canRead()) {
            // send manifest file
            ServletOps.sendFile(mfFile, "", "", response);
            return;
        }
        // check for manifest-meta.json file with additional metadata
        File mfMetaFile = new File(dlDir.getDir(), "manifest-meta.json");
        if (mfMetaFile.canRead()) {
            params.mfMetaFile = mfMetaFile;
        }
        /*
			 * configure base URLs for manifest
			 */
        params.imgApiUrl = dlConfig.getAsString("iiif-image-base-url");
        String manifestBaseUrl = dlConfig.getAsString("iiif-manifest-base-url");
        if ("".equals(params.imgApiUrl) || "".equals(manifestBaseUrl)) {
            // try to figure out base URLs
            String servletBaseUrl = dlConfig.getAsString("webapp-base-url");
            if ("".equals(servletBaseUrl)) {
                String url = request.getRequestURL().toString();
                // get base URL for web application by last occurrence of Servlet path
                int srvPathLen = url.lastIndexOf(request.getServletPath());
                servletBaseUrl = url.substring(0, srvPathLen);
            }
            // manifest base URL
            manifestBaseUrl = servletBaseUrl + request.getServletPath() + "/" + dlConfig.getAsString("iiif-prefix");
            // Image API base URL
            params.imgApiUrl = servletBaseUrl + "/" + this.scalerServletPath + "/" + dlConfig.getAsString("iiif-prefix");
        }
        // full manifest URL with identifier
        params.manifestUrl = manifestBaseUrl + "/" + identifier;
        params.identifier = identifier;
        params.docuDir = dlDir;
        /*
			 * start json representation
			 */
        ServletOutputStream out = response.getOutputStream();
        JsonGenerator manifest = Json.createGenerator(out).writeStartObject();
        /*
			 * manifest metadata
			 */
        writeManifestMeta(manifest, dlFn, params);
        /*
			 * sequences
			 */
        writeSequences(manifest, params);
        // manifest
        manifest.writeEnd();
        manifest.close();
    } catch (IOException e) {
        logger.error("ERROR sending manifest: ", e);
    } catch (ImageOpException e) {
        logger.error("ERROR sending manifest: ", e);
    } catch (AuthOpException e) {
        logger.debug("Permission denied.");
        try {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        } catch (IOException e1) {
            logger.error("Error sending error: ", e);
        }
    }
}
Also used : ImageOpException(digilib.image.ImageOpException) DigilibServletRequest(digilib.conf.DigilibServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) AuthOpException(digilib.auth.AuthOpException) DocuDirectory(digilib.io.DocuDirectory) JsonGenerator(javax.json.stream.JsonGenerator) List(java.util.List) File(java.io.File)

Example 9 with DocuDirectory

use of digilib.io.DocuDirectory in project digilib by robcast.

the class PDFRequest method setWithRequest.

/**
 * Read the request object.
 *
 * @param request
 * @throws ImageOpException
 * @throws IOException
 */
public void setWithRequest(HttpServletRequest request) throws IOException, ImageOpException {
    // read matching request parameters for the parameters in this map
    for (String k : params.keySet()) {
        if (request.getParameterMap().containsKey(k)) {
            setValueFromString(k, request.getParameter(k));
        }
    }
    // process parameters
    pages = new NumRange(getAsString("pgs"));
    ImageJobDescription ij = ImageJobDescription.getInstance(this, dlConfig);
    DocuDirectory dir = ij.getFileDirectory();
    int dirsize = dir.size();
    pages.setMaxnum(dirsize);
}
Also used : NumRange(digilib.util.NumRange) ImageJobDescription(digilib.image.ImageJobDescription) DocuDirectory(digilib.io.DocuDirectory)

Example 10 with DocuDirectory

use of digilib.io.DocuDirectory in project digilib by robcast.

the class Scaler method getLastModified.

/**
 * Returns modification time relevant to the request for caching.
 *
 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.HttpServletRequest)
 */
public long getLastModified(HttpServletRequest request) {
    accountlog.debug("GetLastModified from " + request.getRemoteAddr() + " for " + request.getQueryString());
    long mtime = -1;
    try {
        // create new digilib request
        DigilibServletRequest dlReq = new DigilibServletRequest(request, dlConfig);
        DocuDirectory dd = dirCache.getDirectory(dlReq.getFilePath());
        if (dd != null) {
            mtime = dd.getDirMTime() / 1000 * 1000;
        }
    } catch (Exception e) {
        logger.error("error in getLastModified: " + e.getMessage());
    }
    logger.debug("  returns " + mtime);
    return mtime;
}
Also used : DigilibServletRequest(digilib.conf.DigilibServletRequest) DocuDirectory(digilib.io.DocuDirectory) ImageOpException(digilib.image.ImageOpException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthOpException(digilib.auth.AuthOpException)

Aggregations

DocuDirectory (digilib.io.DocuDirectory)10 DigilibServletRequest (digilib.conf.DigilibServletRequest)4 IOException (java.io.IOException)4 AuthOpException (digilib.auth.AuthOpException)3 ImageOpException (digilib.image.ImageOpException)3 File (java.io.File)2 List (java.util.List)2 ServletException (javax.servlet.ServletException)2 AuthnOps (digilib.auth.AuthnOps)1 AuthzOps (digilib.auth.AuthzOps)1 DocuImage (digilib.image.DocuImage)1 ImageJobDescription (digilib.image.ImageJobDescription)1 AliasingDocuDirCache (digilib.io.AliasingDocuDirCache)1 Directory (digilib.io.Directory)1 DocuDirCache (digilib.io.DocuDirCache)1 FileClass (digilib.io.FileOps.FileClass)1 ImageInput (digilib.io.ImageInput)1 ImageSet (digilib.io.ImageSet)1 DirMeta (digilib.meta.DirMeta)1 FileMeta (digilib.meta.FileMeta)1