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

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


The following commit(s) were added to refs/heads/main by this push:
     new a484235733c Fixed Smb producer issues with required headers and 
default FileExist parameter (#15759)
a484235733c is described below

commit a484235733cdb9807dc7e57b63e428d8e108cbf8
Author: JiriOndrusek <[email protected]>
AuthorDate: Mon Sep 30 16:40:56 2024 +0200

    Fixed Smb producer issues with required headers and default FileExist 
parameter (#15759)
---
 .../camel-smb/src/main/docs/smb-component.adoc     |  3 ++
 .../apache/camel/component/smb/SmbProducer.java    | 14 +++++
 .../component/smb/SmbComponentConnectionIT.java    | 63 ++++++++++++++++++++++
 3 files changed, 80 insertions(+)

diff --git a/components/camel-smb/src/main/docs/smb-component.adoc 
b/components/camel-smb/src/main/docs/smb-component.adoc
index 5f89dc63b0c..531ed5f8536 100644
--- a/components/camel-smb/src/main/docs/smb-component.adoc
+++ b/components/camel-smb/src/main/docs/smb-component.adoc
@@ -50,6 +50,9 @@ include::partial$component-endpoint-options.adoc[]
 include::partial$component-endpoint-headers.adoc[]
 // component headers: END
 
+== Producer
+
+For the SMB producer to operate correctly, the header `Exchange.FILE_NAME` has 
to be included in the exchange.
 
 == Examples
 
diff --git 
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbProducer.java
 
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbProducer.java
index 8661001da8a..998e304688a 100644
--- 
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbProducer.java
+++ 
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbProducer.java
@@ -166,6 +166,10 @@ public class SmbProducer extends DefaultProducer {
     @Override
     public void process(final Exchange exchange) {
         String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, 
String.class);
+        if (fileName == null || fileName.isEmpty()) {
+            //without filename, the file can not be written
+            throw new RuntimeCamelException("Header " + Exchange.FILE_NAME + " 
is missing, cannot create");
+        }
 
         SmbConfiguration configuration = getEndpoint().getConfiguration();
         String path = (configuration.getPath() == null) ? "" : 
configuration.getPath();
@@ -216,6 +220,16 @@ public class SmbProducer extends DefaultProducer {
                     throw new UnsupportedOperationException("TryRename is not 
implemented for this producer at the moment");
             }
 
+            if (shareFile == null) {
+                //open for writing
+                shareFile = share.openFile(file.getPath(),
+                        FILE_WRITE_DATA_ACCESSMASK,
+                        FILE_ATTRIBUTES_NORMAL,
+                        SMB2ShareAccess.ALL,
+                        SMB2CreateDisposition.FILE_CREATE,
+                        FILE_DIRECTORY_CREATE_OPTIONS);
+            }
+
             InputStream is = (exchange.getMessage(InputStream.class) == null)
                     ? exchange.getMessage().getBody(InputStream.class) : 
exchange.getMessage(InputStream.class);
 
diff --git 
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentConnectionIT.java
 
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentConnectionIT.java
index aeb9f8d1b7a..28a71ca5423 100644
--- 
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentConnectionIT.java
+++ 
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentConnectionIT.java
@@ -17,16 +17,20 @@
 package org.apache.camel.component.smb;
 
 import java.io.IOException;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 import com.hierynomus.smbj.SmbConfig;
+import com.hierynomus.smbj.share.File;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFileExist;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.infra.smb.services.SmbService;
 import org.apache.camel.test.infra.smb.services.SmbServiceFactory;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.Assert;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 import org.slf4j.Logger;
@@ -49,6 +53,48 @@ public class SmbComponentConnectionIT extends 
CamelTestSupport {
         mock.assertIsSatisfied();
     }
 
+    @Test
+    public void testSendReceive() throws Exception {
+
+        MockEndpoint mock = getMockEndpoint("mock:received_send");
+        mock.expectedMessageCount(1);
+
+        template.sendBodyAndHeader("seda:send", "Hello World", 
Exchange.FILE_NAME, "file_send.doc");
+
+        mock.assertIsSatisfied();
+        File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
+
+        Assert.assertEquals("Hello World", new 
String(hFile.getInputStream().readAllBytes(), "UTF-8"));
+    }
+
+    @Test
+    public void testDefaultIgnore() throws Exception {
+
+        MockEndpoint mock = getMockEndpoint("mock:received_ignore");
+        mock.expectedMessageCount(1);
+
+        template.sendBodyAndHeader("seda:send", "Hello World", 
Exchange.FILE_NAME, "file_ignore.doc");
+        template.sendBodyAndHeader("seda:send", "Good Bye", 
Exchange.FILE_NAME, "file_ignore.doc");
+
+        mock.assertIsSatisfied();
+        File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
+        Assert.assertEquals("Hello World", new 
String(hFile.getInputStream().readAllBytes(), "UTF-8"));
+    }
+
+    @Test
+    public void testOverride() throws Exception {
+
+        MockEndpoint mock = getMockEndpoint("mock:received_override");
+        mock.expectedMessageCount(1);
+        template.sendBodyAndHeader("seda:send", "Hello World22", 
Exchange.FILE_NAME, "file_override.doc");
+        template.sendBodyAndHeaders("seda:send", "Good Bye", 
Map.of(Exchange.FILE_NAME, "file_override.doc",
+                SmbConstants.SMB_FILE_EXISTS, 
GenericFileExist.Override.name()));
+
+        mock.assertIsSatisfied();
+        File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
+        Assert.assertEquals("Good Bye", new 
String(hFile.getInputStream().readAllBytes(), "UTF-8"));
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -73,6 +119,23 @@ public class SmbComponentConnectionIT extends 
CamelTestSupport {
                 from("seda:intermediate?concurrentConsumers=4")
                         .process(this::process)
                         .to("mock:result");
+
+                from("seda:send")
+                        .toF("smb:%s/%s?username=%s&password=%s&path=/", 
service.address(), service.shareName(),
+                                service.userName(), service.password());
+
+                
fromF("smb:%s/%s?username=%s&password=%s&searchPattern=*_override.doc&path=/", 
service.address(),
+                        service.shareName(),
+                        service.userName(), service.password())
+                        .to("mock:received_override");
+                
fromF("smb:%s/%s?username=%s&password=%s&searchPattern=*_ignore.doc&path=/", 
service.address(),
+                        service.shareName(),
+                        service.userName(), service.password())
+                        .to("mock:received_ignore");
+                
fromF("smb:%s/%s?username=%s&password=%s&searchPattern=*_send.doc&path=/", 
service.address(),
+                        service.shareName(),
+                        service.userName(), service.password())
+                        .to("mock:received_send");
             }
         };
     }

Reply via email to