You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/02 05:09:12 UTC

[camel] branch master updated: CAMEL-15608: Add support for multipart uploads to platform-http-vertx (#4337)

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d3cbae  CAMEL-15608: Add support for multipart uploads to platform-http-vertx (#4337)
9d3cbae is described below

commit 9d3cbaee00f44636fccb08c0917857c6e74875bb
Author: James Netherton <ja...@users.noreply.github.com>
AuthorDate: Fri Oct 2 06:08:56 2020 +0100

    CAMEL-15608: Add support for multipart uploads to platform-http-vertx (#4337)
---
 .../camel/attachment}/CamelFileDataSource.java     |  2 +-
 .../camel/http/common/DefaultHttpBinding.java      |  1 +
 components/camel-platform-http-vertx/pom.xml       |  4 ++
 .../http/vertx/VertxPlatformHttpConsumer.java      | 27 ++++++-----
 .../http/vertx/VertxPlatformHttpEngine.java        | 13 +-----
 .../http/vertx/VertxPlatformHttpEngineTest.java    | 54 ++++++++++++++++++++++
 .../ROOT/pages/camel-3x-upgrade-guide-3_6.adoc     |  6 +++
 7 files changed, 82 insertions(+), 25 deletions(-)

diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelFileDataSource.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/CamelFileDataSource.java
similarity index 97%
rename from components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelFileDataSource.java
rename to components/camel-attachments/src/main/java/org/apache/camel/attachment/CamelFileDataSource.java
index c7518e7..edde8cb 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelFileDataSource.java
+++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/CamelFileDataSource.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.http.common;
+package org.apache.camel.attachment;
 
 import java.io.File;
 
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
index 248b15f..e7f9c56 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
@@ -46,6 +46,7 @@ import org.apache.camel.Message;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.StreamCache;
 import org.apache.camel.attachment.AttachmentMessage;
+import org.apache.camel.attachment.CamelFileDataSource;
 import org.apache.camel.converter.stream.CachedOutputStream;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.support.ExchangeHelper;
diff --git a/components/camel-platform-http-vertx/pom.xml b/components/camel-platform-http-vertx/pom.xml
index f89ceba..5c2a807b3 100644
--- a/components/camel-platform-http-vertx/pom.xml
+++ b/components/camel-platform-http-vertx/pom.xml
@@ -43,6 +43,10 @@
             <artifactId>camel-platform-http</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-attachments</artifactId>
+        </dependency>
+        <dependency>
             <groupId>io.vertx</groupId>
             <artifactId>vertx-web</artifactId>
             <version>${vertx-version}</version>
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
index 125a4ed..16dd352 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
@@ -24,6 +24,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.regex.Pattern;
 
+import javax.activation.DataHandler;
+
 import io.vertx.core.Handler;
 import io.vertx.core.MultiMap;
 import io.vertx.core.Vertx;
@@ -35,9 +37,10 @@ import io.vertx.ext.web.RoutingContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
+import org.apache.camel.attachment.AttachmentMessage;
+import org.apache.camel.attachment.CamelFileDataSource;
 import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
 import org.apache.camel.component.platform.http.spi.Method;
-import org.apache.camel.component.platform.http.spi.UploadAttacher;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.support.DefaultConsumer;
 import org.apache.camel.support.DefaultMessage;
@@ -58,7 +61,6 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
     private static final Pattern PATH_PARAMETER_PATTERN = Pattern.compile("\\{([^/}]+)\\}");
 
     private final List<Handler<RoutingContext>> handlers;
-    private final UploadAttacher uploadAttacher;
     private final String fileNameExtWhitelist;
 
     private Route route;
@@ -66,12 +68,10 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
     public VertxPlatformHttpConsumer(
                                      PlatformHttpEndpoint endpoint,
                                      Processor processor,
-                                     List<Handler<RoutingContext>> handlers,
-                                     UploadAttacher uploadAttacher) {
+                                     List<Handler<RoutingContext>> handlers) {
         super(endpoint, processor);
 
         this.handlers = handlers;
-        this.uploadAttacher = uploadAttacher;
         this.fileNameExtWhitelist
                 = endpoint.getFileNameExtWhitelist() == null ? null : endpoint.getFileNameExtWhitelist().toLowerCase(Locale.US);
     }
@@ -185,21 +185,23 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
                 },
                 false,
                 result -> {
+                    Throwable failure = null;
                     try {
                         if (result.succeeded()) {
                             try {
                                 writeResponse(ctx, exchange, getEndpoint().getHeaderFilterStrategy());
                             } catch (Exception e) {
-                                getExceptionHandler().handleException(
-                                        "Failed handling platform-http endpoint " + getEndpoint().getPath(),
-                                        e);
+                                failure = e;
                             }
                         } else {
+                            failure = result.cause();
+                        }
+
+                        if (failure != null) {
                             getExceptionHandler().handleException(
                                     "Failed handling platform-http endpoint " + getEndpoint().getPath(),
-                                    result.cause());
-
-                            ctx.fail(result.cause());
+                                    failure);
+                            ctx.fail(failure);
                         }
                     } finally {
                         doneUoW(exchange);
@@ -283,7 +285,8 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
             }
             if (accepted) {
                 final File localFile = new File(upload.uploadedFileName());
-                uploadAttacher.attachUpload(localFile, fileName, message);
+                final AttachmentMessage attachmentMessage = message.getExchange().getMessage(AttachmentMessage.class);
+                attachmentMessage.addAttachment(fileName, new DataHandler(new CamelFileDataSource(localFile, fileName)));
             } else {
                 LOGGER.debug(
                         "Cannot add file as attachment: {} because the file is not accepted according to fileNameExtWhitelist: {}",
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
index 7d030d5..43a63c1 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
@@ -28,7 +28,6 @@ import org.apache.camel.Processor;
 import org.apache.camel.component.platform.http.PlatformHttpConstants;
 import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
 import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
-import org.apache.camel.component.platform.http.spi.UploadAttacher;
 import org.apache.camel.spi.annotations.JdkService;
 import org.apache.camel.support.service.ServiceSupport;
 
@@ -39,7 +38,6 @@ import org.apache.camel.support.service.ServiceSupport;
 @JdkService(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_FACTORY)
 public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformHttpEngine {
     private List<Handler<RoutingContext>> handlers;
-    private UploadAttacher uploadAttacher;
 
     public VertxPlatformHttpEngine() {
         this.handlers = Collections.emptyList();
@@ -55,14 +53,6 @@ public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformH
         }
     }
 
-    public UploadAttacher getUploadAttacher() {
-        return uploadAttacher;
-    }
-
-    public void setUploadAttacher(UploadAttacher uploadAttacher) {
-        this.uploadAttacher = uploadAttacher;
-    }
-
     @Override
     protected void doStart() throws Exception {
         // no-op
@@ -78,7 +68,6 @@ public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformH
         return new VertxPlatformHttpConsumer(
                 endpoint,
                 processor,
-                handlers,
-                uploadAttacher);
+                handlers);
     }
 }
diff --git a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index 06b07e7..1cfc2a3 100644
--- a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++ b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -16,11 +16,18 @@
  */
 package org.apache.camel.component.platform.http.vertx;
 
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.concurrent.TimeUnit;
 
+import javax.activation.DataHandler;
+
 import io.vertx.core.VertxOptions;
+import io.vertx.ext.web.handler.BodyHandler;
 import org.apache.camel.CamelContext;
+import org.apache.camel.attachment.AttachmentMessage;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.platform.http.PlatformHttpComponent;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -36,6 +43,7 @@ import org.junit.jupiter.api.Test;
 import static io.restassured.RestAssured.given;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
 
 public class VertxPlatformHttpEngineTest {
     public static SSLContextParameters serverSSLParameters;
@@ -375,4 +383,50 @@ public class VertxPlatformHttpEngineTest {
             context.stop();
         }
     }
+
+    @Test
+    public void testFileUpload() throws Exception {
+        final int port = AvailablePortFinder.getNextAvailable();
+        final String fileContent = "Test multipart upload content";
+        final File tempFile = File.createTempFile("platform-http", ".txt");
+        final CamelContext context = new DefaultCamelContext();
+
+        try {
+            VertxPlatformHttpServerConfiguration conf = new VertxPlatformHttpServerConfiguration();
+            conf.setBindPort(port);
+
+            VertxPlatformHttpServerConfiguration.BodyHandler bodyHandler
+                    = new VertxPlatformHttpServerConfiguration.BodyHandler();
+            bodyHandler.setUploadsDirectory(tempFile.getParent());
+            conf.setBodyHandler(bodyHandler);
+
+            Files.write(tempFile.toPath(), fileContent.getBytes(StandardCharsets.UTF_8));
+
+            context.addService(new VertxPlatformHttpServer(conf));
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("platform-http:/upload")
+                            .process(exchange -> {
+                                AttachmentMessage message = exchange.getMessage(AttachmentMessage.class);
+                                DataHandler attachment = message.getAttachment(tempFile.getName());
+                                message.setBody(attachment.getContent());
+                            });
+                }
+            });
+
+            context.start();
+
+            given()
+                    .port(conf.getBindPort())
+                    .multiPart(tempFile)
+                    .when()
+                    .post("/upload")
+                    .then()
+                    .statusCode(200)
+                    .body(is(fileContent));
+        } finally {
+            context.stop();
+        }
+    }
 }
diff --git a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_6.adoc b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_6.adoc
index 3149baa..f1f7ea3 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_6.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_6.adoc
@@ -158,3 +158,9 @@ Should eb replaced by:
 === Camel Karaf
 
 The following features has been removed due they become not compatible with OSGi: `camel-atmosphere-websocket`.
+
+=== CamelFileDataSource
+
+Class `CamelFileDataSource` has moved from `camel-http-common` in package `org.apache.camel.http.common` to `camel-attachments` package `org.apache.camel.attachment.CamelFileDataSource`.
+
+If your code directly depends on this class, you will need to update the package reference to the new location.