From c4a8fe669a696a03ac8a873df7ed4ed9482f0866 Mon Sep 17 00:00:00 2001 From: Otavio Rodolfo Piske Date: Tue, 1 Oct 2024 11:43:01 -0300 Subject: [PATCH] CAMEL-21277: use WrappedFile in camel-smb for greater consistency --- .../smb/converter/SmbConverterLoader.java | 65 ------------- .../org/apache/camel/TypeConverterLoader | 2 - .../camel/component/smb/SmbConsumer.java | 8 +- .../apache/camel/component/smb/SmbFile.java | 71 ++++++++++++++ .../component/smb/converter/SmbConverter.java | 41 -------- .../smb/SmbComponentConnectionIT.java | 21 +++-- .../camel/component/smb/SmbComponentIT.java | 3 +- .../smb/SmbComponentTypeConversionIT.java | 93 ------------------- 8 files changed, 89 insertions(+), 215 deletions(-) delete mode 100644 components/camel-smb/src/generated/java/org/apache/camel/component/smb/converter/SmbConverterLoader.java delete mode 100644 components/camel-smb/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader create mode 100644 components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbFile.java delete mode 100644 components/camel-smb/src/main/java/org/apache/camel/component/smb/converter/SmbConverter.java delete mode 100644 components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentTypeConversionIT.java diff --git a/components/camel-smb/src/generated/java/org/apache/camel/component/smb/converter/SmbConverterLoader.java b/components/camel-smb/src/generated/java/org/apache/camel/component/smb/converter/SmbConverterLoader.java deleted file mode 100644 index da9c125ba19dc..0000000000000 --- a/components/camel-smb/src/generated/java/org/apache/camel/component/smb/converter/SmbConverterLoader.java +++ /dev/null @@ -1,65 +0,0 @@ -/* Generated by camel build tools - do NOT edit this file! */ -package org.apache.camel.component.smb.converter; - -import javax.annotation.processing.Generated; - -import org.apache.camel.CamelContext; -import org.apache.camel.CamelContextAware; -import org.apache.camel.DeferredContextBinding; -import org.apache.camel.Exchange; -import org.apache.camel.TypeConversionException; -import org.apache.camel.TypeConverterLoaderException; -import org.apache.camel.spi.TypeConverterLoader; -import org.apache.camel.spi.TypeConverterRegistry; -import org.apache.camel.support.SimpleTypeConverter; -import org.apache.camel.support.TypeConverterSupport; -import org.apache.camel.util.DoubleMap; - -/** - * Generated by camel build tools - do NOT edit this file! - */ -@Generated("org.apache.camel.maven.packaging.TypeConverterLoaderGeneratorMojo") -@SuppressWarnings("unchecked") -@DeferredContextBinding -public final class SmbConverterLoader implements TypeConverterLoader, CamelContextAware { - - private CamelContext camelContext; - - public SmbConverterLoader() { - } - - @Override - public void setCamelContext(CamelContext camelContext) { - this.camelContext = camelContext; - } - - @Override - public CamelContext getCamelContext() { - return camelContext; - } - - @Override - public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException { - registerConverters(registry); - } - - private void registerConverters(TypeConverterRegistry registry) { - addTypeConverter(registry, byte[].class, com.hierynomus.smbj.share.File.class, false, - (type, exchange, value) -> getSmbConverter().toByteArray((com.hierynomus.smbj.share.File) value)); - addTypeConverter(registry, java.io.InputStream.class, com.hierynomus.smbj.share.File.class, false, - (type, exchange, value) -> getSmbConverter().toInputStream((com.hierynomus.smbj.share.File) value)); - } - - private static void addTypeConverter(TypeConverterRegistry registry, Class toType, Class fromType, boolean allowNull, SimpleTypeConverter.ConversionMethod method) { - registry.addTypeConverter(toType, fromType, new SimpleTypeConverter(allowNull, method)); - } - - private volatile org.apache.camel.component.smb.converter.SmbConverter smbConverter; - private org.apache.camel.component.smb.converter.SmbConverter getSmbConverter() { - if (smbConverter == null) { - smbConverter = new org.apache.camel.component.smb.converter.SmbConverter(); - CamelContextAware.trySetCamelContext(smbConverter, camelContext); - } - return smbConverter; - } -} diff --git a/components/camel-smb/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader b/components/camel-smb/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader deleted file mode 100644 index dbd9de44e9081..0000000000000 --- a/components/camel-smb/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader +++ /dev/null @@ -1,2 +0,0 @@ -# Generated by camel build tools - do NOT edit this file! -org.apache.camel.component.smb.converter.SmbConverterLoader diff --git a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java index 6eecc1ba21e6e..309e28b9fa57d 100644 --- a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java +++ b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java @@ -88,9 +88,11 @@ private int pollDirectory(DiskShare share, SmbConfiguration configuration, int p smbIOBean.createOptions()); repository.add(fullFilePath); - exchange.getMessage().setHeader(SmbConstants.SMB_FILE_PATH, file.getPath()); - exchange.getMessage().setHeader(SmbConstants.SMB_UNC_PATH, file.getUncPath()); - exchange.getMessage().setBody(file); + + SmbFile smbFile = new SmbFile(file); + + smbFile.populateHeaders(exchange); + exchange.getMessage().setBody(smbFile); try { getProcessor().process(exchange); } catch (Exception e) { diff --git a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbFile.java b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbFile.java new file mode 100644 index 0000000000000..b1e60970ec8c0 --- /dev/null +++ b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbFile.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.smb; + +import java.io.IOException; +import java.io.InputStream; + +import com.hierynomus.smbj.share.File; +import org.apache.camel.Exchange; +import org.apache.camel.WrappedFile; + +public class SmbFile implements WrappedFile { + + private final File file; + + public SmbFile(File file) { + this.file = file; + } + + void populateHeaders(Exchange exchange) { + exchange.getMessage().setHeader(SmbConstants.SMB_FILE_PATH, file.getPath()); + exchange.getMessage().setHeader(SmbConstants.SMB_UNC_PATH, file.getUncPath()); + + exchange.getMessage().setHeader(Exchange.FILE_NAME, file.getFileInformation().getNameInformation().toString()); + } + + @Override + public File getFile() { + return file; + } + + public String getPath() { + return getFile().getPath(); + } + + public String getUncPath() { + return getFile().getUncPath(); + } + + public InputStream getInputStream() { + return getFile().getInputStream(); + } + + public long getSize() { + return file.getFileInformation().getStandardInformation().getEndOfFile(); + } + + @Override + public Object getBody() { + try (InputStream is = file.getInputStream()) { + return is.readAllBytes(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/components/camel-smb/src/main/java/org/apache/camel/component/smb/converter/SmbConverter.java b/components/camel-smb/src/main/java/org/apache/camel/component/smb/converter/SmbConverter.java deleted file mode 100644 index 32e956159356f..0000000000000 --- a/components/camel-smb/src/main/java/org/apache/camel/component/smb/converter/SmbConverter.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.camel.component.smb.converter; - -import java.io.IOException; -import java.io.InputStream; - -import org.apache.camel.Converter; - -@Converter(generateLoader = true) -public class SmbConverter { - - @Converter - public InputStream toInputStream(com.hierynomus.smbj.share.File smbFile) { - return smbFile.getInputStream(); - } - - @Converter - public byte[] toByteArray(com.hierynomus.smbj.share.File smbFile) { - try (InputStream is = toInputStream(smbFile)) { - return is.readAllBytes(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } -} 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 28a71ca542375..3ef6f6cebcd56 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,11 +17,11 @@ package org.apache.camel.component.smb; import java.io.IOException; +import java.nio.charset.StandardCharsets; 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; @@ -62,9 +62,9 @@ public void testSendReceive() throws Exception { template.sendBodyAndHeader("seda:send", "Hello World", Exchange.FILE_NAME, "file_send.doc"); mock.assertIsSatisfied(); - File hFile = mock.getExchanges().get(0).getIn().getBody(File.class); + SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class); - Assert.assertEquals("Hello World", new String(hFile.getInputStream().readAllBytes(), "UTF-8")); + Assert.assertEquals("Hello World", new String(file.getInputStream().readAllBytes(), "UTF-8")); } @Test @@ -77,8 +77,8 @@ public void testDefaultIgnore() throws Exception { 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")); + SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class); + Assert.assertEquals("Hello World", new String(file.getInputStream().readAllBytes(), StandardCharsets.UTF_8)); } @Test @@ -91,18 +91,21 @@ public void testOverride() throws Exception { 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")); + SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class); + Assert.assertEquals("Good Bye", new String(file.getInputStream().readAllBytes(), StandardCharsets.UTF_8)); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { private void process(Exchange exchange) throws IOException { - final byte[] data = exchange.getMessage().getBody(byte[].class); + final SmbFile data = exchange.getMessage().getBody(SmbFile.class); final String filePath = exchange.getMessage().getHeader(SmbConstants.SMB_FILE_PATH, String.class); final String uncPath = exchange.getMessage().getHeader(SmbConstants.SMB_UNC_PATH, String.class); - LOG.debug("Read exchange {} ({}) with contents: {}", filePath, uncPath, new String(data)); + final String name = exchange.getMessage().getHeader(Exchange.FILE_NAME, String.class); + + LOG.debug("Read exchange name {} at {} ({}) with contents: {} (bytes {})", name, filePath, uncPath, + new String(data.getInputStream().readAllBytes(), StandardCharsets.UTF_8), data.getSize()); } public void configure() { diff --git a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentIT.java b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentIT.java index 7d1eba986792a..890faf528419f 100644 --- a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentIT.java +++ b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentIT.java @@ -21,7 +21,6 @@ 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; @@ -62,7 +61,7 @@ public void testSmbSendFile() throws Exception { protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { private void process(Exchange exchange) throws IOException { - final File file = exchange.getMessage().getBody(File.class); + final SmbFile file = exchange.getMessage().getBody(SmbFile.class); try (InputStream inputStream = file.getInputStream()) { LOG.debug("Read exchange: {}, with contents: {}", file.getPath(), diff --git a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentTypeConversionIT.java b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentTypeConversionIT.java deleted file mode 100644 index c2cb857764bce..0000000000000 --- a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbComponentTypeConversionIT.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.smb; - -import java.io.IOException; -import java.io.InputStream; -import java.util.concurrent.TimeUnit; - -import com.hierynomus.smbj.SmbConfig; -import org.apache.camel.EndpointInject; -import org.apache.camel.Exchange; -import org.apache.camel.builder.RouteBuilder; -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.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SmbComponentTypeConversionIT extends CamelTestSupport { - private static final Logger LOG = LoggerFactory.getLogger(SmbComponentTypeConversionIT.class); - - @RegisterExtension - public static SmbService service = SmbServiceFactory.createService(); - - @EndpointInject("mock:result") - protected MockEndpoint mockResultEndpoint; - - private boolean allValidConversions = true; - - @Test - public void testSmbRead() throws Exception { - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedMessageCount(100); - - mock.assertIsSatisfied(); - Assertions.assertTrue(allValidConversions, "There were invalid conversions"); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - private void process(Exchange exchange) { - final byte[] data = exchange.getMessage().getBody(byte[].class); - - if (data == null || data.length == 0) { - allValidConversions = false; - } - - LOG.debug("Read exchange as bytes with contents: {}", new String(data)); - - final InputStream is = exchange.getMessage().getBody(InputStream.class); - try { - LOG.debug("Read exchange via InputStream with contents: {}", new String(is.readAllBytes())); - } catch (IOException e) { - allValidConversions = false; - throw new RuntimeException(e); - } - } - - public void configure() { - SmbConfig config = SmbConfig.builder() - .withTimeout(120, TimeUnit.SECONDS) // Timeout sets Read, Write, and Transact timeouts (default is 60 seconds) - .withSoTimeout(180, TimeUnit.SECONDS) // Socket Timeout (default is 0 seconds, blocks forever) - .build(); - context.getRegistry().bind("smbConfig", config); - - fromF("smb:%s/%s?username=%s&password=%s&path=/&smbConfig=#smbConfig", service.address(), service.shareName(), - service.userName(), service.password()) - .process(this::process) - .to("mock:result"); - - } - }; - } -}