This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit e07452d79e1805ec5ae9d6a10c0643b66a44aeac
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Fri Apr 11 16:19:59 2025 +0200

    Check HTTP status when opening a file from an URL.
---
 .../org/apache/sis/io/stream/HttpByteChannel.java   | 21 +++++++++++++++++++++
 .../org/apache/sis/storage/internal/Resources.java  |  5 +++++
 .../sis/storage/internal/Resources.properties       |  1 +
 .../sis/storage/internal/Resources_fr.properties    |  1 +
 4 files changed, 28 insertions(+)

diff --git 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/HttpByteChannel.java
 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/HttpByteChannel.java
index 28b48f0adc..569da7bb6d 100644
--- 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/HttpByteChannel.java
+++ 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/HttpByteChannel.java
@@ -23,8 +23,11 @@ import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.FileNotFoundException;
+import java.nio.file.AccessDeniedException;
 import java.lang.ref.WeakReference;
 import java.util.List;
+import org.apache.sis.storage.internal.Resources;
 
 
 /**
@@ -113,6 +116,17 @@ final class HttpByteChannel extends FileCacheByteChannel {
         } catch (InterruptedException e) {
             throw new IOException(e);
         }
+        switch (response.statusCode()) {
+            default:  throw new IOException(cannotConnect(response));
+            case 204: // No Content
+            case 404: throw new FileNotFoundException(cannotConnect(response));
+            case 451: // Unavailable For Legal Reasons
+            case 401: // Unauthorized
+            case 403: throw new AccessDeniedException(cannotConnect(response));
+            case 203: // Non-Authoritative Information
+            case 200: break;    // OK
+            case 206: break;    // Partial content
+        }
         final InputStream stream  = response.body();
         final HttpHeaders headers = response.headers();
         range = headers.firstValue("Content-Range").orElse(null);
@@ -129,6 +143,13 @@ final class HttpByteChannel extends FileCacheByteChannel {
         }
     }
 
+    /**
+     * Returns an error message for an HTTP connection that failed.
+     */
+    private String cannotConnect(final HttpResponse<?> response) {
+        return Resources.format(Resources.Keys.CanNotConnectHTTP_2, filename, 
response.statusCode());
+    }
+
     /**
      * Invoked when this channel is no longer interested in reading bytes from 
the specified connection.
      *
diff --git 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.java
 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.java
index 158a6efe84..3ed3d3f62c 100644
--- 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.java
+++ 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.java
@@ -64,6 +64,11 @@ public class Resources extends IndexedResourceBundle {
          */
         public static final short AuxiliaryFileTooLarge_1 = 71;
 
+        /**
+         * Connection to “{0}” failed with HTTP error code {1}.
+         */
+        public static final short CanNotConnectHTTP_2 = 85;
+
         /**
          * Cannot create resources based on the content of “{0}” directory.
          */
diff --git 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.properties
 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.properties
index d06317bb09..3857d9333f 100644
--- 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.properties
+++ 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources.properties
@@ -21,6 +21,7 @@
 #
 AmbiguousName_4                   = Name \u201c{3}\u201d is ambiguous because 
it can be understood as either \u201c{1}\u201d or \u201c{2}\u201d in the 
context of \u201c{0}\u201d data.
 AuxiliaryFileTooLarge_1           = Auxiliary file \u201c{0}\u201d seems too 
large.
+CanNotConnectHTTP_2               = Connection to \u201c{0}\u201d failed with 
HTTP error code {1}.
 CanNotCreateFolderStore_1         = Cannot create resources based on the 
content of \u201c{0}\u201d directory.
 CanNotDeriveTypeFromFeature_1     = Cannot infer the feature type resulting 
from \u201c{0}\u201d filtering.
 CanNotGetCommonMetadata_2         = Cannot get metadata common to 
\u201c{0}\u201d files. The reason is: {1}
diff --git 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources_fr.properties
 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources_fr.properties
index fc8660bbed..764205bd8a 100644
--- 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources_fr.properties
+++ 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/internal/Resources_fr.properties
@@ -26,6 +26,7 @@
 #
 AmbiguousName_4                   = Le nom \u00ab\u202f{3}\u202f\u00bb est 
ambigu\u00eb car il peut \u00eatre interpr\u00e9t\u00e9 aussi bien comme 
\u00ab\u202f{1}\u202f\u00bb ou \u00ab\u202f{2}\u202f\u00bb dans le contexte des 
donn\u00e9es de \u00ab\u202f{0}\u202f\u00bb.
 AuxiliaryFileTooLarge_1           = Le fichier auxiliaire 
\u00ab\u202f{0}\u202f\u00bb semble trop grand.
+CanNotConnectHTTP_2               = La connexion \u00e0 
\u00ab\u202f{0}\u202f\u00bb a \u00e9chou\u00e9e avec le code d\u2019erreur HTTP 
{1}.
 CanNotCreateFolderStore_1         = Ne peut pas cr\u00e9er des ressources 
bas\u00e9es sur le contenu du r\u00e9pertoire \u00ab\u202f{0}\u202f\u00bb.
 CanNotDeriveTypeFromFeature_1     = Ne peut pas d\u00e9terminer le type de 
donn\u00e9es r\u00e9sultant du filtrage \u00ab\u202f{0}\u202f\u00bb.
 CanNotGetCommonMetadata_2         = Ne peut pas obtenir les 
m\u00e9ta-donn\u00e9es communes aux fichiers \u00ab\u202f{0}\u202f\u00bb. La 
raison est\u00a0: {1}

Reply via email to