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

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new e6403792ae Register known jakarta.mail exception classes for reflection
e6403792ae is described below

commit e6403792aea64055e68f8aba78d0db872dfba956
Author: James Netherton <jamesnether...@gmail.com>
AuthorDate: Thu Aug 3 15:00:28 2023 +0100

    Register known jakarta.mail exception classes for reflection
    
    Fixes #5142
---
 .../mail/deployment/SupportMailProcessor.java      | 29 +++++++++++++++++-----
 .../quarkus/component/mail/CamelResource.java      | 15 +++++++++++
 .../camel/quarkus/component/mail/CamelRoute.java   |  6 +++++
 .../camel/quarkus/component/mail/MailTest.java     | 13 ++++++++--
 4 files changed, 55 insertions(+), 8 deletions(-)

diff --git 
a/extensions-support/mail/deployment/src/main/java/org/apache/camel/quarkus/support/mail/deployment/SupportMailProcessor.java
 
b/extensions-support/mail/deployment/src/main/java/org/apache/camel/quarkus/support/mail/deployment/SupportMailProcessor.java
index 3fe3d640d9..4f7c76dcbc 100644
--- 
a/extensions-support/mail/deployment/src/main/java/org/apache/camel/quarkus/support/mail/deployment/SupportMailProcessor.java
+++ 
b/extensions-support/mail/deployment/src/main/java/org/apache/camel/quarkus/support/mail/deployment/SupportMailProcessor.java
@@ -39,32 +39,48 @@ import 
io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
 import 
io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
+import jakarta.mail.MessagingException;
 import jakarta.mail.Provider;
 import org.eclipse.angus.mail.handlers.handler_base;
+import org.jboss.jandex.ClassInfo;
 import org.jboss.jandex.DotName;
 
 class SupportMailProcessor {
 
     @BuildStep
-    void process(CombinedIndexBuildItem combinedIndex, 
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
+    ReflectiveClassBuildItem registerForReflection(CombinedIndexBuildItem 
combinedIndex) {
+        String[] mailExceptionClassNames = combinedIndex.getIndex()
+                .getAllKnownSubclasses(MessagingException.class.getName())
+                .stream()
+                .map(ClassInfo::name)
+                .map(DotName::toString)
+                .toArray(String[]::new);
+
+        return 
ReflectiveClassBuildItem.builder(mailExceptionClassNames).build();
+    }
+
+    @BuildStep
+    void registerServices(
+            CombinedIndexBuildItem combinedIndex,
+            BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
             BuildProducer<NativeImageResourceBuildItem> resource,
             BuildProducer<ServiceProviderBuildItem> services) {
         List<String> providers = 
resources("META-INF/services/jakarta.mail.Provider")
                 .flatMap(this::lines)
                 .filter(s -> !s.startsWith("#"))
-                .collect(Collectors.toList());
+                .toList();
 
         List<String> streamProviders = 
resources("META-INF/services/jakarta.mail.util.StreamProvider")
                 .flatMap(this::lines)
                 .filter(s -> !s.startsWith("#"))
-                .collect(Collectors.toList());
+                .toList();
 
         List<String> imp1 = providers.stream()
                 .map(this::loadClass)
                 .map(this::instantiate)
                 .map(Provider.class::cast)
                 .map(Provider::getClassName)
-                .collect(Collectors.toList());
+                .toList();
 
         List<String> imp2 = Stream.of("META-INF/javamail.default.providers", 
"META-INF/javamail.providers")
                 .flatMap(this::resources)
@@ -74,7 +90,7 @@ class SupportMailProcessor {
                 .map(String::trim)
                 .filter(s -> s.startsWith("class="))
                 .map(s -> s.substring("class=".length()))
-                .collect(Collectors.toList());
+                .toList();
 
         List<String> imp3 = resources("META-INF/mailcap")
                 .flatMap(this::lines)
@@ -83,7 +99,7 @@ class SupportMailProcessor {
                 .map(String::trim)
                 .filter(s -> s.startsWith("x-java-content-handler="))
                 .map(s -> s.substring("x-java-content-handler=".length()))
-                .collect(Collectors.toList());
+                .toList();
 
         
reflectiveClass.produce(ReflectiveClassBuildItem.builder(Stream.concat(providers.stream(),
                 Stream.concat(streamProviders.stream(),
@@ -115,6 +131,7 @@ class SupportMailProcessor {
     @BuildStep
     void registerDependencyForIndex(BuildProducer<IndexDependencyBuildItem> 
items) {
         items.produce(new IndexDependencyBuildItem("jakarta.activation", 
"jakarta.activation-api"));
+        items.produce(new IndexDependencyBuildItem("jakarta.mail", 
"jakarta.mail-api"));
         items.produce(new IndexDependencyBuildItem("org.eclipse.angus", 
"angus-activation"));
         items.produce(new IndexDependencyBuildItem("org.eclipse.angus", 
"angus-mail"));
     }
diff --git 
a/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelResource.java
 
b/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelResource.java
index 06084dee3b..6da11d7f74 100644
--- 
a/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelResource.java
+++ 
b/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelResource.java
@@ -42,6 +42,7 @@ import jakarta.ws.rs.PathParam;
 import jakarta.ws.rs.Produces;
 import jakarta.ws.rs.QueryParam;
 import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.ProducerTemplate;
@@ -240,4 +241,18 @@ public class CamelResource {
         }).collect(Collectors.toList());
     }
 
+    @Path("/exception")
+    @GET
+    public Response throwMailConnectException() {
+        try {
+            producerTemplate.requestBody("direct:throwMailConnectException", 
(Object) null);
+            return Response.ok().build();
+        } catch (Exception e) {
+            Class<?> causeClass = e.getClass();
+            if (e.getCause() != null) {
+                causeClass = e.getCause().getClass();
+            }
+            return Response.serverError().entity(causeClass.getName()).build();
+        }
+    }
 }
diff --git 
a/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelRoute.java
 
b/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelRoute.java
index 05c323a60d..f6c0fac239 100644
--- 
a/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelRoute.java
+++ 
b/integration-tests/mail/src/main/java/org/apache/camel/quarkus/component/mail/CamelRoute.java
@@ -45,6 +45,7 @@ import org.apache.camel.component.mail.MailMessage;
 import org.apache.camel.support.jsse.KeyManagersParameters;
 import org.apache.camel.support.jsse.SSLContextParameters;
 import org.apache.camel.support.jsse.TrustManagersParameters;
+import org.eclipse.angus.mail.util.MailConnectException;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
 
 @ApplicationScoped
@@ -85,6 +86,9 @@ public class CamelRoute extends RouteBuilder {
 
     @Override
     public void configure() {
+        onException(MailConnectException.class)
+                .log("Failed to connect to mail host")
+                .handled(false);
 
         from("direct:sendMail")
                 .toF("smtp://localhost:%d?username=%s&password=%s", smtpPort, 
USERNAME, PASSWORD);
@@ -149,6 +153,8 @@ public class CamelRoute extends RouteBuilder {
                     map.put("convertedStream", is);
                 });
 
+        from("direct:throwMailConnectException")
+                .to("smtp://bad.host.org?to=f...@bar.com");
     }
 
     private Map<String, Object> handleMail(Exchange exchange) throws 
MessagingException {
diff --git 
a/integration-tests/mail/src/test/java/org/apache/camel/quarkus/component/mail/MailTest.java
 
b/integration-tests/mail/src/test/java/org/apache/camel/quarkus/component/mail/MailTest.java
index 5750046c91..30de0a2d9c 100644
--- 
a/integration-tests/mail/src/test/java/org/apache/camel/quarkus/component/mail/MailTest.java
+++ 
b/integration-tests/mail/src/test/java/org/apache/camel/quarkus/component/mail/MailTest.java
@@ -37,9 +37,9 @@ import io.restassured.http.ContentType;
 import jakarta.json.bind.JsonbBuilder;
 import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.ServiceStatus;
+import org.eclipse.angus.mail.util.MailConnectException;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
-import org.hamcrest.Matchers;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
@@ -51,6 +51,7 @@ import org.testcontainers.shaded.org.awaitility.Awaitility;
 import static org.apache.camel.quarkus.component.mail.CamelRoute.EMAIL_ADDRESS;
 import static org.apache.camel.quarkus.component.mail.CamelRoute.PASSWORD;
 import static org.apache.camel.quarkus.component.mail.CamelRoute.USERNAME;
+import static org.hamcrest.Matchers.is;
 
 @QuarkusTest
 @QuarkusTestResource(MailTestResource.class)
@@ -94,7 +95,7 @@ public class MailTest {
                 .post("/api/service/reset")
                 .then()
                 .statusCode(200)
-                .body("message", Matchers.is("Performed reset"));
+                .body("message", is("Performed reset"));
 
         RestAssured.get("/mail/stopConsumers")
                 .then()
@@ -338,6 +339,14 @@ public class MailTest {
         Assertions.assertTrue(sorted.get(3).contains("message 4"));
     }
 
+    @Test
+    void testThrowMailConnectException() {
+        RestAssured.get("/mail/exception")
+                .then()
+                .statusCode(500)
+                .body(is(MailConnectException.class.getName()));
+    }
+
     // helper methods
 
     private void startRoute(CamelRoute.Routes route) {

Reply via email to