Mercurial > hg > digilib
changeset 1620:1c87dbe782ab iiif-presentation-2
config option "iiif-image-cors" to set CORS header on all image responses. some cleanup.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 30 May 2017 19:21:15 +0200 |
parents | 221685dd882a |
children | 0b6ee26b3d37 |
files | common/src/main/java/digilib/util/ParameterMap.java iiif-presentation/src/main/java/digilib/conf/ManifestServletConfiguration.java iiif-presentation/src/main/java/digilib/servlet/Manifester.java servlet/src/main/java/digilib/servlet/ServletOps.java servlet2/src/main/java/digilib/servlet/Scaler.java servlet2/src/main/java/digilib/servlet/ScalerNoThread.java servlet3/src/main/java/digilib/servlet/Scaler.java text/src/main/java/digilib/servlet/Texter.java |
diffstat | 8 files changed, 126 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/common/src/main/java/digilib/util/ParameterMap.java Tue May 30 18:13:30 2017 +0200 +++ b/common/src/main/java/digilib/util/ParameterMap.java Tue May 30 19:21:15 2017 +0200 @@ -130,7 +130,7 @@ /** Get the Parameter with the corresponding key. * - * Returns null if no element is associated with key. + * Returns empty string if no element is associated with key. * * @param key * @return @@ -142,7 +142,7 @@ /** Get the Parameter with the corresponding key. * - * Returns null if no element is associated with key. + * Returns 0 if no element is associated with key. * * @param key * @return @@ -154,7 +154,7 @@ /** Get the Parameter with the corresponding key. * - * Returns null if no element is associated with key. + * Returns 0 if no element is associated with key. * * @param key * @return @@ -166,7 +166,7 @@ /** Get the Parameter with the corresponding key. * - * Returns null if no element is associated with key. + * Returns false if no element is associated with key. * * @param key * @return
--- a/iiif-presentation/src/main/java/digilib/conf/ManifestServletConfiguration.java Tue May 30 18:13:30 2017 +0200 +++ b/iiif-presentation/src/main/java/digilib/conf/ManifestServletConfiguration.java Tue May 30 19:21:15 2017 +0200 @@ -1,5 +1,32 @@ package digilib.conf; +/* + * #%L + * + * ManifesteServletConfiguration.java + * + * Digital Image Library servlet components + * %% + * Copyright (C) 2003 - 2017 MPIWG Berlin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + * Author: Robert Casties (robcast@sourceforge.net) + * Created on 24.5.2017 + */ + import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener;
--- a/iiif-presentation/src/main/java/digilib/servlet/Manifester.java Tue May 30 18:13:30 2017 +0200 +++ b/iiif-presentation/src/main/java/digilib/servlet/Manifester.java Tue May 30 19:21:15 2017 +0200 @@ -3,7 +3,7 @@ /* * #%L * - * Texter.java -- Servlet for displaying text + * Manifester.java -- Servlet for creating IIIF Presentation API manifests. * * Digital Image Library servlet components * %% @@ -58,7 +58,7 @@ import digilib.util.ImageSize; /** - * Servlet for displaying text + * Servlet for creating IIIF Presentation API manifests. * * * @author casties @@ -69,31 +69,34 @@ private static final long serialVersionUID = 6678666342141409868L; /** Servlet version */ - public static String tlVersion = ManifestServletConfiguration.getClassVersion(); + public static String mfVersion = ManifestServletConfiguration.getClassVersion(); /** DigilibConfiguration instance */ - DigilibServletConfiguration dlConfig = null; + protected DigilibServletConfiguration dlConfig = null; /** general logger */ - Logger logger = Logger.getLogger("digilib.manifester"); + protected Logger logger = Logger.getLogger("digilib.manifester"); /** logger for accounting requests */ protected static Logger accountlog = Logger.getLogger("account.manifester.request"); /** AuthOps instance */ - AuthzOps authzOp; + protected AuthzOps authzOp; /** DocuDirCache instance */ - DocuDirCache dirCache; + protected DocuDirCache dirCache; /** use authentication */ - boolean useAuthorization = false; + protected boolean useAuthorization = false; /** scaler servlet path */ - String scalerServletPath; + protected String scalerServletPath; /** character for IIIF path separation */ - private String iiifPathSep; + protected String iiifPathSep; + + /** set CORS header ACAO* for info requests */ + protected boolean corsForInfoRequests = true; /* * (non-Javadoc) @@ -103,7 +106,7 @@ public void init(ServletConfig config) throws ServletException { super.init(config); - System.out.println("***** Digital Image Library IIF Manifest Servlet (version " + tlVersion + ") *****"); + System.out.println("***** Digital Image Library IIF Manifest Servlet (version " + mfVersion + ") *****"); // get our ServletContext ServletContext context = config.getServletContext(); @@ -114,7 +117,7 @@ throw new ServletException("No Configuration!"); } // say hello in the log file - logger.info("***** Digital Image Library IIIF Manifest Servlet (version " + tlVersion + ") *****"); + logger.info("***** Digital Image Library IIIF Manifest Servlet (version " + mfVersion + ") *****"); // set our AuthOps useAuthorization = dlConfig.getAsBoolean("use-authorization"); @@ -125,6 +128,8 @@ scalerServletPath = dlConfig.getAsString("scaler-servlet-path"); // IIIF path separator iiifPathSep = dlConfig.getAsString("iiif-slash-replacement"); + // CORS for info requests + corsForInfoRequests = dlConfig.getAsBoolean("iiif-info-cors"); } /** @@ -225,7 +230,7 @@ /* * set CORS header ACAO "*" for info response as per IIIF spec */ - if (dlConfig.getAsBoolean("iiif-info-cors")) { + if (corsForInfoRequests) { String origin = request.getHeader("Origin"); if (origin != null) { response.setHeader("Access-Control-Allow-Origin", "*");
--- a/servlet/src/main/java/digilib/servlet/ServletOps.java Tue May 30 18:13:30 2017 +0200 +++ b/servlet/src/main/java/digilib/servlet/ServletOps.java Tue May 30 19:21:15 2017 +0200 @@ -41,6 +41,7 @@ import org.apache.log4j.Logger; +import digilib.conf.DigilibServletConfiguration; import digilib.conf.DigilibServletRequest; import digilib.image.DocuImage; import digilib.image.ImageOpException; @@ -52,16 +53,39 @@ public class ServletOps { - private static Logger logger = Logger.getLogger("servlet.op"); + protected static Logger logger = Logger.getLogger("servlet.op"); + + protected static DigilibServletConfiguration dlConfig; + + /** set CORS header ACAO* for info requests */ + protected static boolean corsForInfoRequests = true; + + /** set CORS header ACAO* for image requests */ + protected static boolean corsForImageRequests = true; /** + * @return the dlConfig + */ + public static DigilibServletConfiguration getDlConfig() { + return dlConfig; + } + + /** + * @param dlConfig the dlConfig to set + */ + public static void setDlConfig(DigilibServletConfiguration dlConfig) { + ServletOps.dlConfig = dlConfig; + corsForInfoRequests = dlConfig.getAsBoolean("iiif-info-cors"); + corsForImageRequests = dlConfig.getAsBoolean("iiif-image-cors"); + } + + /** * convert a string with a list of pathnames into an array of strings using * the system's path separator string */ public static String[] getPathArray(String paths) { // split list into directories - StringTokenizer dirs = new StringTokenizer(paths, - java.io.File.pathSeparator); + StringTokenizer dirs = new StringTokenizer(paths, java.io.File.pathSeparator); int n = dirs.countTokens(); if (n < 1) { return null; @@ -247,6 +271,9 @@ logger.error("No response!"); return; } + /* + * set content-type + */ if (mt == null) { // auto-detect mime-type mt = FileOps.mimeForFile(f); @@ -255,7 +282,6 @@ } } response.setContentType(mt); - // open file if (mt.startsWith("application")) { if (name == null) { // no download name -- use filename @@ -263,7 +289,19 @@ } response.addHeader("Content-Disposition", "attachment; filename=\""+name+"\""); } - FileInputStream inFile = null; + + /* + * set CORS header ACAO "*" for image response + */ + if (corsForImageRequests) { + // TODO: would be nice to check request for Origin header + response.setHeader("Access-Control-Allow-Origin", "*"); + } + + /* + * open file + */ + FileInputStream inFile = null; try { inFile = new FileInputStream(f); OutputStream outStream = response.getOutputStream(); @@ -314,9 +352,8 @@ * @throws ImageOpException * @throws ServletException Exception on sending data. */ - public static void sendImage(DocuImage img, String mimeType, - HttpServletResponse response, Logger logger) throws ImageOpException, - ServletException { + public static void sendImage(DocuImage img, String mimeType, HttpServletResponse response, Logger logger) + throws ImageOpException, ServletException { if (response == null) { logger.error("No response!"); return; @@ -353,6 +390,15 @@ return; } + + /* + * set CORS header ACAO "*" for image response + */ + if (corsForImageRequests) { + // TODO: would be nice to check request for Origin header + response.setHeader("Access-Control-Allow-Origin", "*"); + } + /* * write the image */ @@ -377,7 +423,8 @@ * @throws ServletException * @see <a href="http://www-sul.stanford.edu/iiif/image-api/1.1/#info">IIIF Image Information Request</a> */ - public static void sendIiifInfo(DigilibServletRequest dlReq, HttpServletResponse response, Logger logger) throws ServletException { + public static void sendIiifInfo(DigilibServletRequest dlReq, HttpServletResponse response, Logger logger) + throws ServletException { if (response == null) { logger.error("No response!"); return; @@ -421,7 +468,7 @@ /* * set CORS header ACAO "*" for info response as per IIIF spec */ - if (dlReq.getDigilibConfig().getAsBoolean("iiif-info-cors")) { + if (corsForInfoRequests) { String origin = dlReq.getServletRequest().getHeader("Origin"); if (origin != null) { response.setHeader("Access-Control-Allow-Origin", "*"); @@ -429,7 +476,7 @@ } PrintWriter writer; - if (dlReq.getDigilibConfig().getAsString("iiif-api-version").startsWith("2.")) { + if (dlConfig.getAsString("iiif-api-version").startsWith("2.")) { /* * IIIF Image API version 2 image information */
--- a/servlet2/src/main/java/digilib/servlet/Scaler.java Tue May 30 18:13:30 2017 +0200 +++ b/servlet2/src/main/java/digilib/servlet/Scaler.java Tue May 30 19:21:15 2017 +0200 @@ -147,6 +147,9 @@ // Executor imageJobCenter = (DigilibJobCenter<DocuImage>) dlConfig.getValue("servlet.worker.imageexecutor"); + // configure ServletOps + ServletOps.setDlConfig(dlConfig); + denyImgFile = ServletOps.getFile(dlConfig.getAsFile("denied-image"), context); errorImgFile = ServletOps.getFile(dlConfig.getAsFile("error-image"), context); notfoundImgFile = ServletOps.getFile(dlConfig.getAsFile("notfound-image"), context);
--- a/servlet2/src/main/java/digilib/servlet/ScalerNoThread.java Tue May 30 18:13:30 2017 +0200 +++ b/servlet2/src/main/java/digilib/servlet/ScalerNoThread.java Tue May 30 19:21:15 2017 +0200 @@ -130,6 +130,9 @@ // DocuDirCache instance dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); + // configure ServletOps + ServletOps.setDlConfig(dlConfig); + denyImgFile = ServletOps.getFile((File) dlConfig.getValue("denied-image"), context); errorImgFile = ServletOps.getFile((File) dlConfig.getValue("error-image"), context); notfoundImgFile = ServletOps.getFile((File) dlConfig.getValue("notfound-image"), context);
--- a/servlet3/src/main/java/digilib/servlet/Scaler.java Tue May 30 18:13:30 2017 +0200 +++ b/servlet3/src/main/java/digilib/servlet/Scaler.java Tue May 30 19:21:15 2017 +0200 @@ -42,7 +42,6 @@ import digilib.auth.AuthOpException; import digilib.auth.AuthzOps; -import digilib.conf.DigilibConfiguration; import digilib.conf.DigilibOption; import digilib.conf.DigilibServlet3Configuration; import digilib.conf.DigilibServletConfiguration; @@ -104,7 +103,7 @@ protected boolean sendFileAllowed = true; /** DigilibConfiguration instance */ - protected DigilibConfiguration dlConfig; + protected DigilibServletConfiguration dlConfig; /** use authorization database */ protected boolean useAuthorization = false; @@ -147,6 +146,9 @@ // Executor imageJobCenter = (DigilibJobCenter<DocuImage>) dlConfig.getValue("servlet.worker.imageexecutor"); + // configure ServletOps + ServletOps.setDlConfig(dlConfig); + denyImgFile = ServletOps.getFile(dlConfig.getAsFile("denied-image"), context); errorImgFile = ServletOps.getFile(dlConfig.getAsFile("error-image"), context); notfoundImgFile = ServletOps.getFile(dlConfig.getAsFile("notfound-image"), context);
--- a/text/src/main/java/digilib/servlet/Texter.java Tue May 30 18:13:30 2017 +0200 +++ b/text/src/main/java/digilib/servlet/Texter.java Tue May 30 19:21:15 2017 +0200 @@ -62,28 +62,25 @@ public static String tlVersion = TextServletConfiguration.getClassVersion(); /** DigilibConfiguration instance */ - DigilibServletConfiguration dlConfig = null; + protected DigilibServletConfiguration dlConfig = null; /** general logger */ - Logger logger = Logger.getLogger("digilib.texter"); + protected Logger logger = Logger.getLogger("digilib.texter"); /** logger for accounting requests */ protected static Logger accountlog = Logger.getLogger("account.texter.request"); /** FileOps instance */ - FileOps fileOp; + protected FileOps fileOp; /** AuthOps instance */ - AuthzOps authzOp; - - /** ServletOps instance */ - ServletOps servletOp; + protected AuthzOps authzOp; /** DocuDirCache instance */ - DocuDirCache dirCache; + protected DocuDirCache dirCache; /** use authentication */ - boolean useAuthorization = false; + protected boolean useAuthorization = false; /* * (non-Javadoc) @@ -113,6 +110,8 @@ authzOp = (AuthzOps) dlConfig.getValue(DigilibServletConfiguration.AUTHZ_OP_KEY); // DocuDirCache instance dirCache = (DocuDirCache) dlConfig.getValue(TextServletConfiguration.TEXT_DIR_CACHE_KEY); + // configure ServletOps + ServletOps.setDlConfig(dlConfig); } /* @@ -192,7 +191,7 @@ * @return The wanted Textfile or null if there wasn't a file. */ - private TextFile getTextFile(DigilibServletRequest dlRequest, String subDirectory) { + protected TextFile getTextFile(DigilibServletRequest dlRequest, String subDirectory) { String loadPathName = dlRequest.getFilePath() + subDirectory; // find the file(set) return (TextFile) dirCache.getFile(loadPathName, dlRequest.getAsInt("pn"));