gnodet-bot commented on code in PR #2326:
URL: https://github.com/apache/maven-resolver/pull/2326#discussion_r4617177515


##########
maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457Reporter.java:
##########
@@ -86,10 +110,57 @@ protected static String readBody(InputStream is) throws 
IOException {
             body.write(chunk, 0, read);
             remaining -= read;
         }
-        return body.toString(StandardCharsets.UTF_8.name());
+        return new String(body.toByteArray(), charset);
     }
 
-    protected boolean hasRFC9457ContentType(String contentType) {
+    protected static Charset extractCharsetFromContentType(String contentType, 
Charset defaultCharset) {
+        if (contentType == null) {
+            return defaultCharset;
+        }
+
+        int paramsStart = contentType.indexOf(';');
+        if (paramsStart < 0 || paramsStart == contentType.length() - 1) {
+            return defaultCharset;
+        }
+
+        String[] parameters = contentType.substring(paramsStart + 
1).split(";");
+        for (String parameter : parameters) {
+            String trimmedParameter = parameter.trim();
+            if (trimmedParameter.isEmpty()) {
+                continue;
+            }
+
+            int separator = trimmedParameter.indexOf('=');
+            if (separator <= 0 || separator == trimmedParameter.length() - 1) {
+                continue;
+            }
+
+            String name = trimmedParameter.substring(0, separator).trim();
+            if (!"charset".equalsIgnoreCase(name)) {
+                continue;
+            }
+
+            String value = trimmedParameter.substring(separator + 1).trim();
+            if (value.length() >= 2 && value.startsWith("\"") && 
value.endsWith("\"")) {
+                value = value.substring(1, value.length() - 1).trim();
+            }
+
+            if (value.isEmpty()) {
+                continue;
+            }
+
+            try {
+                return Charset.forName(value);
+            } catch (Exception e) {

Review Comment:
   ⚠️ **Broad exception catch.** Only `Charset.forName()` can throw in this try 
block, and it only throws `IllegalCharsetNameException` or 
`UnsupportedCharsetException`. Catching `Exception` would silently swallow 
unexpected bugs introduced by future refactoring inside this block (e.g., a 
`NullPointerException`).
   
   ```suggestion
               } catch (IllegalCharsetNameException | 
UnsupportedCharsetException e) {
   ```



##########
maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457Reporter.java:
##########
@@ -133,12 +203,50 @@ public void generateException(T response, 
BiConsumerChecked<Integer, String, E>
                 }
                 if (rfc9457Payload != null) {
                     throw new HttpRFC9457Exception(statusCode, reasonPhrase, 
rfc9457Payload);
+                } else {
+                    throw new HttpTransporterException(
+                            statusCode, reasonPhrase, 
generateBodyExtract(body, contentType));
                 }
-                baseException.accept(statusCode, reasonPhrase);
-                return;
+            } else {
+                // No body found but it is representing a RFC 9457 message due 
to the content type.
+                throw new HttpRFC9457Exception(statusCode, reasonPhrase, 
RFC9457Payload.INSTANCE);
+            }
+        } else {
+            try {
+                body = getBody(response);
+                throw new HttpTransporterException(statusCode, reasonPhrase, 
generateBodyExtract(body, contentType));
+            } catch (IOException e) {
+                throw new HttpTransporterException(statusCode, reasonPhrase);
             }
-            throw new HttpRFC9457Exception(statusCode, reasonPhrase, 
RFC9457Payload.INSTANCE);
         }
-        baseException.accept(statusCode, reasonPhrase);
+    }
+
+    /**
+     * Generates a body extract from the given body string (usually HTML)
+     * This method can be overridden to provide custom logic for generating a 
body extract.

Review Comment:
   🐛 **Incorrect Javadoc — `static` methods cannot be overridden.** 
`generateBodyExtract` is `public static`, so "This method can be overridden" is 
factually wrong and will mislead subclass authors who try to customise body 
extraction by overriding it (the override will never be called via static 
dispatch). Either:
   - Remove the claim and document the actual behavior (text types pass 
through, binary types get a placeholder); or
   - Make the method non-static so subclasses can actually override it.
   
   ```suggestion
        * Generates a body extract from the given body string (usually HTML).
        * For text-based content types ({@code text/*}, {@code *\/json}, {@code 
*\/xml}) the body string is returned as-is.
        * For binary content types a placeholder message is returned to avoid 
embedding non-printable data in the exception.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to