From f67a5012d032b1531fecd918fafdcd745f8c894c Mon Sep 17 00:00:00 2001
From: Robert Butora <robert.butora@inaf.it>
Date: Mon, 23 Dec 2024 16:24:11 +0200
Subject: [PATCH] resolver: adds support for HTTP-dav (http:// https://) and
 files (file://)

---
 .../java/common/resolver/ResolverFromId.java  | 132 +++++++++++++-----
 .../src/main/java/cutout/SodaImpl.java        |  10 +-
 2 files changed, 106 insertions(+), 36 deletions(-)

diff --git a/data-access/servlet/src/main/java/common/resolver/ResolverFromId.java b/data-access/servlet/src/main/java/common/resolver/ResolverFromId.java
index 847aa6a..20b04b3 100644
--- a/data-access/servlet/src/main/java/common/resolver/ResolverFromId.java
+++ b/data-access/servlet/src/main/java/common/resolver/ResolverFromId.java
@@ -27,52 +27,116 @@ class ResolverFromId implements Resolver
    {
       LOGGER.fine("trace " + pubdid);
 
-      boolean isIvoid = pubdid.substring(0,6).equals("ivo://");
+      final boolean isIvoid = pubdid.substring(0,6).equals("ivo://");
+      final boolean isDav   = pubdid.startsWith("http://") || pubdid.startsWith("https://");
+      final boolean isFile = pubdid.startsWith("file://");
 
       if(isIvoid)
       {
+         LOGGER.finer("isIvoid");
          resolveIvoid(pubdid);
-
          // FIXME resolve subsurveyId by matching relPathname to subsurveys::storage-path & file-filter
-
-         LOGGER.finer("relPathname : " + relPathname);
-         LOGGER.finer("hdunum      : " + String.valueOf(hdunum));
-         LOGGER.finer("subsurveyId : " + ((subsurveyId == null) ? "null" : this.subsurveyId) );
       }
-      else
+      else if(isDav)
       {
-         throw new IllegalArgumentException("IVOID expected: ID must start with 'ivo://' but received: " + pubdid);
+         LOGGER.finer("isDav");
+         resolveDav(pubdid);
       }
-   }
-
-
-
-   private void resolveIvoid(String pubdid)
-   {
-      LOGGER.fine("trace " + pubdid);
-
-      int qmarkIx = pubdid.lastIndexOf("?");
-      int dhashIx = pubdid.lastIndexOf("#");// returns -1 if hash not found
-
-      boolean hash_not_found = (dhashIx < 0);
-
-      if(hash_not_found)
+      else if(isFile)
       {
-         relPathname = pubdid.substring(  qmarkIx + 1 );
-         hdunum = 1;
+         LOGGER.finer("isFile");
+         resolveFile(pubdid);
       }
       else
       {
-         relPathname = pubdid.substring(  qmarkIx + 1, dhashIx );
-
-         if((dhashIx+1) == pubdid.length())
-            throw new IllegalArgumentException(
-                  "if ID's last hash must be followed by HDU extension number however: " + pubdid);
-         else
-            hdunum = 1 + Integer.parseInt( pubdid.substring( dhashIx + 1 ) );
-      }
-   }
-
+         throw new IllegalArgumentException("IVOID expected: ID must start with 'ivo://' but received: " + pubdid);
+		}
+
+		LOGGER.finer("relPathname : " + relPathname);
+		LOGGER.finer("hdunum      : " + String.valueOf(hdunum));
+		LOGGER.finer("subsurveyId : " + ((subsurveyId == null) ? "null" : this.subsurveyId) );
+	}
+
+
+
+	private void resolveIvoid(String pubdid)
+	{
+		LOGGER.fine("trace " + pubdid);
+
+		int qmarkIx = pubdid.lastIndexOf("?");
+		int dhashIx = pubdid.lastIndexOf("#");// returns -1 if hash not found
+
+		boolean hash_not_found = (dhashIx < 0);
+
+		if(hash_not_found)
+		{
+			relPathname = pubdid.substring(  qmarkIx + 1 );
+			hdunum = 1;
+		}
+		else
+		{
+			relPathname = pubdid.substring(  qmarkIx + 1, dhashIx );
+
+			if((dhashIx+1) == pubdid.length())
+				throw new IllegalArgumentException(
+						"if ID's last hash must be followed by HDU extension number however: " + pubdid);
+			else
+				hdunum = 1 + Integer.parseInt( pubdid.substring( dhashIx + 1 ) );
+		}
+	}
+
+
+	private void resolveDav(String pubdid)
+	{
+		LOGGER.fine("trace " + pubdid);
+
+		int dhashIx = pubdid.lastIndexOf("#");// returns -1 if hash not found
+
+		boolean hash_not_found = (dhashIx < 0);
+
+		if(hash_not_found)
+		{
+			relPathname = pubdid;
+			hdunum = 1;
+		}
+		else
+		{
+			relPathname = pubdid.substring( 0, dhashIx );
+
+			if((dhashIx+1) == pubdid.length())
+				throw new IllegalArgumentException(
+						"if ID's last hash must be followed by HDU extension number however: " + pubdid);
+			else
+				hdunum = 1 + Integer.parseInt( pubdid.substring( dhashIx + 1 ) );
+		}
+	}
+
+
+	private void resolveFile(String pubdid)
+	{
+		LOGGER.fine("trace " + pubdid);
+
+		int slashIx = pubdid.indexOf("/"); // first slash in ://
+		int dhashIx = pubdid.lastIndexOf("#");// returns -1 if hash not found
+
+		boolean hash_not_found = (dhashIx < 0);
+
+		if(hash_not_found)
+		{
+			relPathname = pubdid.substring( slashIx+2, dhashIx );
+			hdunum = 1;
+		}
+		else
+		{
+			relPathname = pubdid.substring( slashIx+2, dhashIx );
+
+			if((dhashIx+1) == pubdid.length())
+				throw new IllegalArgumentException(
+						"if ID's last hash must be followed by HDU extension number however: " + pubdid);
+			else
+				hdunum = 1 + Integer.parseInt( pubdid.substring( dhashIx + 1 ) );
+		}
+	}
 
 }
 
diff --git a/data-access/servlet/src/main/java/cutout/SodaImpl.java b/data-access/servlet/src/main/java/cutout/SodaImpl.java
index 07e4fad..9466f54 100644
--- a/data-access/servlet/src/main/java/cutout/SodaImpl.java
+++ b/data-access/servlet/src/main/java/cutout/SodaImpl.java
@@ -55,7 +55,13 @@ class SodaImpl implements Soda
       boolean pixels_valid = (pixels != null);
 
       String boundsString = "";
-      String absPathname = fitsPaths.surveys() + "/" + relPathname;
+
+      final boolean isDavCall = relPathname.startsWith("http://") || relPathname.startsWith("https://");
+      final boolean isAbsPath = relPathname.startsWith("/"); // Resolver removes schema from file://
+                                                             //  file:///some_abs_path -> /soma_abs_path
+                                                             //  file://some_rel_path -> some_rel_path
+
+      String absPathname = (isDavCall || isAbsPath) ? relPathname : (fitsPaths.surveys() +"/"+ relPathname);
 
       if( !pixels_valid )
       {
@@ -116,7 +122,7 @@ class SodaImpl implements Soda
 
          String[] cmdCut = new String[5];
          cmdCut[0] = "/usr/local/bin/vlkb";
-         cmdCut[1] = "imcopy";
+         cmdCut[1] = isDavCall ? "imcopydav" : "imcopy";
          cmdCut[2] = absPathname;
          cmdCut[3] = String.valueOf(hdunum-1);
          cmdCut[4] = pixFilterString;
-- 
GitLab