changeset 1615:20a0d05847a2 iiif-presentation-2

More inheritance and parsing options for DigilibRequest.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Fri, 26 May 2017 15:44:20 +0200
parents e3c6e4c0d93d
children 4a54658a376c
files common/src/main/java/digilib/conf/DigilibRequest.java servlet/src/main/java/digilib/conf/DigilibServletRequest.java
diffstat 2 files changed, 140 insertions(+), 152 deletions(-) [+]
line wrap: on
line diff
--- a/common/src/main/java/digilib/conf/DigilibRequest.java	Thu May 25 19:31:40 2017 +0200
+++ b/common/src/main/java/digilib/conf/DigilibRequest.java	Fri May 26 15:44:20 2017 +0200
@@ -31,6 +31,9 @@
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.EnumSet;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import org.apache.log4j.Logger;
@@ -63,13 +66,26 @@
 public class DigilibRequest extends ParameterMap {
 
     private static Logger logger = Logger.getLogger("digilib.request");
+    
+    /**
+     * special options for parsing the request. 
+     */
+    public static enum ParsingOption {
+    	omitIiifImageApi
+    }
 
+    /** active pasing options */
+    public EnumSet<ParsingOption> parsingOptions = EnumSet.noneOf(ParsingOption.class);
+    
     /** IIIF path prefix (taken from config) */
     protected String iiifPrefix = "IIIF";
     
     /** IIIF slash replacement (taken from config) */
     protected String iiifSlashReplacement = null;
     
+    /** parse IIIF path as IIIF image API */
+    public boolean parseIiifImageApi = true;
+    
     /** error message while configuring */
     public String errorMessage = null;
 
@@ -154,6 +170,9 @@
         newParameter("request.path", "", null, 'i');
         // base URL (from http:// to below /servlet)
         newParameter("base.url", null, null, 'i');
+        // elements of IIIF API path
+        newParameter("request.iiif.elements", null, null, 'i');
+        
         /*
          * Parameters of type 'c' are for the clients use
          */
@@ -291,13 +310,9 @@
             return false;
         }
         
-        String identifier = null;
-        String region = null;
-        String size = null;
-        String rotation = null;
-        String quality = null;
-        String format = null;
-
+        List<String> params = new ArrayList<String>(5);
+        setValue("request.iiif.elements", params);
+        
         // enable passing of delimiter to get empty parameters
         StringTokenizer query = new StringTokenizer(path, "/", true);
         String token;
@@ -317,80 +332,89 @@
             }
         }
         /*
-         * second parameter identifier (encoded)
+         * following parameters
          */
-        if (query.hasMoreTokens()) {
+        while (query.hasMoreTokens()) {
             token = getNextDecodedToken(query);
             if (!token.equals("/")) {
-                identifier = token;
-                // skip /
-                if (query.hasMoreTokens()) {
-                    query.nextToken();
-                }
-            }
-        }
-        /*
-         * third parameter region
-         */
-        if (query.hasMoreTokens()) {
-            token = getNextDecodedToken(query);
-            if (!token.equals("/")) {
-                region = token;
-                // skip /
-                if (query.hasMoreTokens()) {
-                    query.nextToken();
-                }
-            }
-        }
-        /*
-         * fourth parameter size
-         */
-        if (query.hasMoreTokens()) {
-            token = getNextDecodedToken(query);
-            if (!token.equals("/")) {
-                size = token;
+            	params.add(token);
                 // skip /
                 if (query.hasMoreTokens()) {
                     query.nextToken();
                 }
-            }
-        }
-        /*
-         * fifth parameter rotation
-         */
-        if (query.hasMoreTokens()) {
-            token = getNextDecodedToken(query);
-            if (!token.equals("/")) {
-                rotation = token;
-                // skip /
-                if (query.hasMoreTokens()) {
-                    query.nextToken();
-                }
-            }
-        }
-        /*
-         * sixth parameter quality.format
-         */
-        if (query.hasMoreTokens()) {
-            token = getNextDecodedToken(query);
-            // quality.format -- color depth and output format
-            try {
-                String[] parms = token.split("\\.");
-                // quality param
-                quality = parms[0];
-                // format param
-                if (parms.length > 1) {
-                    format = parms[1];
-                }
-            } catch (Exception e) {
-                errorMessage = "Error parsing quality and format parameters in IIIF path!";
-                logger.error(errorMessage, e);
-                return false;
+            } else {
+            	// empty parameter
+            	params.add(null);
             }
         }
 
+        if (parsingOptions.contains(ParsingOption.omitIiifImageApi)) {
+        	return true;
+        }
+        
+        /*
+         * parse sequence of parameters as IIIF image API
+         */
+        String identifier = null;
+        String region = null;
+        String size = null;
+        String rotation = null;
+        String quality = null;
+        String format = null;
+
+		if (params.size() > 0) {
+			/*
+			 * first parameter identifier (encoded)
+			 */
+			identifier = params.get(0);
+			
+			if (params.size() > 1) {
+				/*
+				 * second parameter region
+				 */
+				region = params.get(1);
+				
+				if (params.size() > 2) {
+					/*
+					 * third parameter size
+					 */
+					size = params.get(2);
+					
+					if (params.size() > 3) {
+						/*
+						 * fourth parameter rotation
+						 */
+						rotation = params.get(3);
+						
+						if (params.size() > 4) {
+							/*
+							 * fifth parameter quality.format
+							 */
+							String qf = params.get(4);
+							if (qf != null) {
+								// quality.format -- color depth and output
+								// format
+								try {
+									String[] parms = qf.split("\\.");
+									// quality param
+									quality = parms[0];
+									// format param
+									if (parms.length > 1) {
+										format = parms[1];
+									}
+								} catch (Exception e) {
+									errorMessage = "Error parsing quality and format parameters in IIIF path!";
+									logger.error(errorMessage, e);
+									return false;
+								}
+							}
+						}
+					}
+				}
+			}
+		}
         // set request with these parameters
-        return setWithIiifParams(identifier, region, size, rotation, quality, format);        
+        return setWithIiifImageParams(identifier, region, size, rotation, quality, format);
     }
 
     private String getNextDecodedToken(StringTokenizer tokens) {
@@ -417,7 +441,7 @@
 	 * @param format
 	 * @return
 	 */
-	public boolean setWithIiifParams(String identifier, String region, String size, 
+	public boolean setWithIiifImageParams(String identifier, String region, String size, 
 			String rotation, String quality, String format) {
         // alway set HTTP status code error reporting
         options.setOption(DigilibOption.errcode);
@@ -427,14 +451,7 @@
          */
         if (identifier != null) {
             try {
-                if (identifier.contains("%")) {
-                    // still escape chars -- decode again
-                    identifier = URLDecoder.decode(identifier, "UTF-8");
-                }
-                if (iiifSlashReplacement != null && identifier.contains(iiifSlashReplacement)) {
-                    // change replacement back to slash
-                    identifier = identifier.replace(iiifSlashReplacement, "/");
-                }
+                identifier = decodeIiifIdentifier(identifier);
                 setValueFromString("fn", identifier);
             } catch (UnsupportedEncodingException e) {
                 errorMessage = "Error decoding identifier in IIIF path!";
@@ -614,6 +631,23 @@
         return true;
 	}
 
+	/**
+	 * @param identifier
+	 * @return
+	 * @throws UnsupportedEncodingException
+	 */
+	public String decodeIiifIdentifier(String identifier) throws UnsupportedEncodingException {
+		if (identifier.contains("%")) {
+		    // still escape chars -- decode again
+		    identifier = URLDecoder.decode(identifier, "UTF-8");
+		}
+		if (iiifSlashReplacement != null && identifier.contains(iiifSlashReplacement)) {
+		    // change replacement back to slash
+		    identifier = identifier.replace(iiifSlashReplacement, "/");
+		}
+		return identifier;
+	}
+
 	
     /**
      * Test if option string <code>opt</code> is set. Checks if the substring
--- a/servlet/src/main/java/digilib/conf/DigilibServletRequest.java	Thu May 25 19:31:40 2017 +0200
+++ b/servlet/src/main/java/digilib/conf/DigilibServletRequest.java	Fri May 26 15:44:20 2017 +0200
@@ -1,5 +1,7 @@
 package digilib.conf;
 
+import java.util.EnumSet;
+
 /*
  * #%L
  * DigilibRequest.java
@@ -73,7 +75,6 @@
      * ServletRequest. All undefined parameters are set to default values.
      * 
      * @param request
-     * @throws ImageOpException 
      */
     public DigilibServletRequest(HttpServletRequest request) {
         super();
@@ -86,7 +87,7 @@
      * ServletRequest. All undefined parameters are set to default values.
      * 
      * @param request
-     * @throws ImageOpException 
+     * @param config
      */
     public DigilibServletRequest(HttpServletRequest request, DigilibConfiguration config) {
         super(config);
@@ -94,58 +95,34 @@
         initOptions();
     }
 
+    /**
+     * Creates a new instance of DigilibRequest with parameters from a
+     * ServletRequest. All undefined parameters are set to default values.
+     * 
+     * @param request
+     * @param config
+     * @param parsingOptions
+     */
+	public DigilibServletRequest(HttpServletRequest request, DigilibConfiguration config,
+			EnumSet<ParsingOption> parsingOptions) {
+        super(config);
+        this.parsingOptions = parsingOptions;
+        setWithRequest(request);
+        initOptions();
+    }
+
     /* (non-Javadoc)
      * @see digilib.conf.DigilibRequest#initParams()
      * Define and set up parameters with default values.
      */
     @Override
     protected void initParams() {
-        // TODO: check if we can call super.initParams()
+        super.initParams();
         /*
-         * Definition of parameters and default values. Parameter of type 's'
+         * Definition of additional parameters and default values. Parameter of type 's'
          * are for the servlet.
          */
 
-        // url of the page/document (second part)
-        newParameter("fn", "", null, 's');
-        // page number
-        newParameter("pn", new Integer(1), null, 's');
-        // width of client in pixels
-        newParameter("dw", new Integer(0), null, 's');
-        // height of client in pixels
-        newParameter("dh", new Integer(0), null, 's');
-        // left edge of image (float from 0 to 1)
-        newParameter("wx", new Float(0), null, 's');
-        // top edge in image (float from 0 to 1)
-        newParameter("wy", new Float(0), null, 's');
-        // width of image (float from 0 to 1)
-        newParameter("ww", new Float(1), null, 's');
-        // height of image (float from 0 to 1)
-        newParameter("wh", new Float(1), null, 's');
-        // scale factor
-        newParameter("ws", new Float(1), null, 's');
-        // special options like 'fit' for gifs
-        newParameter("mo", this.options, null, 's');
-        // rotation angle (degree)
-        newParameter("rot", new Float(0), null, 's');
-        // contrast enhancement factor
-        newParameter("cont", new Float(0), null, 's');
-        // brightness enhancement factor
-        newParameter("brgt", new Float(0), null, 's');
-        // color multiplicative factors
-        newParameter("rgbm", "0/0/0", null, 's');
-        // color additive factors
-        newParameter("rgba", "0/0/0", null, 's');
-        // display dpi resolution (total)
-        newParameter("ddpi", new Float(0), null, 's');
-        // display dpi X resolution
-        newParameter("ddpix", new Float(0), null, 's');
-        // display dpi Y resolution
-        newParameter("ddpiy", new Float(0), null, 's');
-        // scale factor for mo=ascale
-        newParameter("scale", new Float(1), null, 's');
-        // color conversion operation
-        newParameter("colop", "", null, 's');
         // OpenID Connect ID token
         newParameter("id_token", "", null, 's');
 
@@ -154,39 +131,12 @@
          * but are for the servlets or JSPs internal use.
          */
 
-        // url of the page/document (first part, may be empty)
-        newParameter("request.path", "", null, 'i');
-        // base URL (from http:// to below /servlet)
-        newParameter("base.url", null, null, 'i');
         // DocuImage instance for this request
         newParameter("docu.image", image, null, 'i');
         image = null;
         // HttpServletRequest for this request
         newParameter("servlet.request", servletRequest, null, 'i');
         servletRequest = null;
-
-        /*
-         * Parameters of type 'c' are for the clients use
-         */
-
-        // "real" filename
-        newParameter("img.fn", "", null, 'c');
-        // image dpi x
-        newParameter("img.dpix", new Integer(0), null, 'c');
-        // image dpi y
-        newParameter("img.dpiy", new Integer(0), null, 'c');
-        // hires image size x
-        newParameter("img.pix_x", new Integer(0), null, 'c');
-        // hires image size y
-        newParameter("img.pix_y", new Integer(0), null, 'c');
-        
-        /*
-         * set local variables from config
-         */
-        if (config != null) {
-            iiifPrefix = config.getAsString("iiif-prefix");
-            iiifSlashReplacement = config.getAsString("iiif-slash-replacement");
-        }
     }
 
     /*
@@ -212,7 +162,9 @@
         setValue("servlet.request", request);
         // request path (after servlet, before "?")
         String path = request.getPathInfo();
-        // decide if its IIIF API
+        /*
+         * is it IIIF API?
+         */
         if (path != null && path.startsWith(iiifPrefix, 1)) {
             // for IIIF we need the undecoded path :-(
             String uri = request.getRequestURI();
@@ -228,7 +180,9 @@
                 setValue("dw", -1);
             }
         } else {
-            // decide if it's old-style or new-style digilib
+            /*
+             * is it old-style or new-style digilib?
+             */
             String qs = ((HttpServletRequest) request).getQueryString();
             if (qs != null) {
                 if (qs.indexOf("&amp;") > -1) {