From 9ef8e04eade8bfeebcaed3b8d60d90fc4ba260dc Mon Sep 17 00:00:00 2001 From: Gregory Bragg Date: Fri, 29 Apr 2022 16:07:02 -0400 Subject: [PATCH] GH-246 Deprecate SmbCfg.replaceFile & useTempFile Fixes https://github.com/spring-projects/spring-integration-extensions/issues/246 The `replaceFile` & `useTempFile` options are not `SmbSession` responsibility. The `FileTransferringMessageHandler` covers a functionality of `replace` and `tmFile`. * Implement an `SmbMessageHandler` to hide a `replaceFile` setting on the `SmbSessionFactory` to retain backward compatibility * Mention an `SmbMessageHandler` in the README Updates after further unit testing Updated for initial PR review requested changes removed comment to satisfy PR review Updated to satisfy PR review requests for changes More updates after further PR review Updated again based on PR review comments * Suppress deprecations in the tests for these deprecated options * Add `package-info.java` to `outbound` * Delegate from one ctor to another in the `SmbMessageHandler` --- spring-integration-smb/README.md | 17 +++--- .../smb/outbound/SmbMessageHandler.java | 54 +++++++++++++++++++ .../smb/outbound/package-info.java | 4 ++ .../integration/smb/session/SmbConfig.java | 26 ++++++++- .../smb/session/SmbRemoteFileTemplate.java | 2 +- .../smb/session/SmbSessionFactory.java | 8 +-- .../smb/SmbMessageHistoryTests-context.xml | 1 - ...SmbInboundChannelAdapterSample-context.xml | 3 +- .../smb/config/SmbInboundOutboundSample.java | 2 - ...boundChannelAdapterParserTests-context.xml | 1 - ...mbOutboundChannelAdapterSample-context.xml | 3 +- ...oundRemoteFileSystemSynchronizerTests.java | 1 - .../SmbSendingMessageHandlerTests.java | 13 ++--- ...SmbSessionFactoryWithCIFSContextTests.java | 9 ++-- 14 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbMessageHandler.java create mode 100644 spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/package-info.java diff --git a/spring-integration-smb/README.md b/spring-integration-smb/README.md index 4ab903d..fb1cb15 100644 --- a/spring-integration-smb/README.md +++ b/spring-integration-smb/README.md @@ -13,12 +13,11 @@ This module adds Spring Integration support for [Server Message Block][] (SMB). ## Using Maven -Put the following block into pom.xml if using Maven: +Put the following block into pom.xml if using Maven, set the `` tag to the desired release: org.springframework.integration spring-integration-smb - 1.2.0.RELEASE ## Changes @@ -30,6 +29,11 @@ Put the following block into pom.xml if using Maven: ##### Version 1.2 * Ability to set the SMB min/max versions in the `SmbSessionFactory` via configuration in the JCIFS library * Ability to use a custom implementation of the `jcifs.CIFSContext` interface in the `SmbSessionFactory` + +##### Version 1.2.2 + * Updated to use the latest version of the [JCIFS](https://github.com/codelibs/jcifs) library + * Added implementation for `SmbMessageHandler` + * Deprecated legacy `replaceFile` and `useTempFile` flags in `SmbConfig` ## Overview @@ -48,15 +52,14 @@ For XML configuration the `` component is provi ### SMB Outbound Channel Adapter -There is no (yet) some SMB specific requirements for files transferring to SMB, so for XML `` component we simply reuse an existing `FileTransferringMessageHandler`. -In case of Java configuration that `FileTransferringMessageHandler` should be supplied with the `SmbSessionFactory` (or `SmbRemoteFileTemplate`). +For writing files to a SMB share, and for XML `` component we use the `SmbMessageHandler`. +In case of Java configuration a `SmbMessageHandler` should be supplied with the `SmbSessionFactory` (or `SmbRemoteFileTemplate`). ````java -@ServiceActivator(inputChannel = "storeToSmb") @Bean +@ServiceActivator(inputChannel = "storeToSmbShare") public MessageHandler smbMessageHandler(SmbSessionFactory smbSessionFactory) { - FileTransferringMessageHandler handler = - new FileTransferringMessageHandler<>(smbSessionFactory); + SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory); handler.setRemoteDirectoryExpression( new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(m -> diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbMessageHandler.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbMessageHandler.java new file mode 100644 index 0000000..5be5701 --- /dev/null +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbMessageHandler.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.integration.smb.outbound; + +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.file.support.FileExistsMode; +import org.springframework.integration.smb.session.SmbRemoteFileTemplate; +import org.springframework.integration.smb.session.SmbSessionFactory; + +import jcifs.smb.SmbFile; + +/** +* The SMB specific {@link FileTransferringMessageHandler} extension. Based on the +* {@link SmbRemoteFileTemplate}. +* +* @author Gregory Bragg +* @author Artem Bilan +* +* @since 1.2.2 +* +* @see SmbRemoteFileTemplate +*/ +public class SmbMessageHandler extends FileTransferringMessageHandler { + + public SmbMessageHandler(SessionFactory sessionFactory) { + this(sessionFactory, FileExistsMode.REPLACE); + } + + @SuppressWarnings("deprecation") + public SmbMessageHandler(SessionFactory sessionFactory, FileExistsMode mode) { + super(new SmbRemoteFileTemplate(sessionFactory), mode); + + if (sessionFactory instanceof SmbSessionFactory && FileExistsMode.REPLACE.equals(mode)) { + SmbSessionFactory smbSessionFactory = (SmbSessionFactory) sessionFactory; + smbSessionFactory.setReplaceFile(true); + } + } + +} diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/package-info.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/package-info.java new file mode 100644 index 0000000..58b10c9 --- /dev/null +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/package-info.java @@ -0,0 +1,4 @@ +/** + * Outbound Channel Adapter implementations for SMB protocol. + */ +package org.springframework.integration.smb.outbound; diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbConfig.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbConfig.java index 3e655bc..44d6c03 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbConfig.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,18 +131,42 @@ public class SmbConfig { return this.shareAndDir; } + /** + * The replace file option. + * @param _replaceFile true/false + * @deprecated as of 1.2.2 in favor of SmbMessageHandler + */ + @Deprecated public void setReplaceFile(boolean _replaceFile) { this.replaceFile = _replaceFile; } + /** + * The replace file option. + * @return true/false + * @deprecated as of 1.2.2 in favor of SmbMessageHandler + */ + @Deprecated public boolean isReplaceFile() { return this.replaceFile; } + /** + * The tmp file option. + * @param _useTempFile true/false + * @deprecated as of 1.2.2 in favor of SmbMessageHandler + */ + @Deprecated void setUseTempFile(boolean _useTempFile) { this.useTempFile = _useTempFile; } + /** + * The tmp file option. + * @return true/false + * @deprecated as of 1.2.2 in favor of SmbMessageHandler + */ + @Deprecated public boolean isUseTempFile() { return this.useTempFile; } diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbRemoteFileTemplate.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbRemoteFileTemplate.java index 1cff5ac..2272bf4 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbRemoteFileTemplate.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbRemoteFileTemplate.java @@ -22,7 +22,7 @@ import org.springframework.integration.file.remote.session.SessionFactory; import jcifs.smb.SmbFile; /** - * The SMP-specific {@link RemoteFileTemplate} implementation. + * The SMB-specific {@link RemoteFileTemplate} implementation. * * @author Artem Bilan */ diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSessionFactory.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSessionFactory.java index 338372b..3f3bd8b 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSessionFactory.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSessionFactory.java @@ -33,10 +33,11 @@ import jcifs.smb.SmbFile; * * @author Markus Spann * @author Gregory Bragg + * @author Artem Bilan */ public class SmbSessionFactory extends SmbConfig implements SessionFactory { - private static Log logger = LogFactory.getLog(SmbSessionFactory.class); + private static final Log logger = LogFactory.getLog(SmbSessionFactory.class); private CIFSContext context = null; @@ -65,6 +66,7 @@ public class SmbSessionFactory extends SmbConfig implements SessionFactory - + p:shareAndDir="smb-share/"/> - + p:shareAndDir="smb-share/"/> diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/inbound/SmbInboundRemoteFileSystemSynchronizerTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/inbound/SmbInboundRemoteFileSystemSynchronizerTests.java index 3b294ea..a8e9f43 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/inbound/SmbInboundRemoteFileSystemSynchronizerTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/inbound/SmbInboundRemoteFileSystemSynchronizerTests.java @@ -64,7 +64,6 @@ public class SmbInboundRemoteFileSystemSynchronizerTests extends AbstractBaseTes smbSessionFactory.setUsername("sambaguest"); smbSessionFactory.setPassword("sambaguest"); smbSessionFactory.setShareAndDir("smb-share/"); - smbSessionFactory.setReplaceFile(true); } // @Test diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/outbound/SmbSendingMessageHandlerTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/outbound/SmbSendingMessageHandlerTests.java index 5890b40..42c1606 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/outbound/SmbSendingMessageHandlerTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/outbound/SmbSendingMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,6 @@ import org.mockito.stubbing.Answer; import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.common.LiteralExpression; -import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.smb.AbstractBaseTests; import org.springframework.integration.smb.session.SmbSession; import org.springframework.integration.smb.session.SmbSessionFactory; @@ -45,6 +44,7 @@ import jcifs.smb.SmbFile; * @author Markus Spann * @author Artem Bilan * @author Prafull Kumar Soni + * @author Gregory Bragg */ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { @@ -62,13 +62,12 @@ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { smbSessionFactory.setUsername("sambaguest"); smbSessionFactory.setPassword("sambaguest"); smbSessionFactory.setShareAndDir("smb-share/"); - smbSessionFactory.setReplaceFile(true); } @Test public void testHandleFileContentMessage() { File file = createNewFile("remote-target-dir/handlerContent.test"); - FileTransferringMessageHandler handler = new FileTransferringMessageHandler(smbSessionFactory); + SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(message -> "handlerContent.test"); handler.setAutoCreateDirectory(true); @@ -81,7 +80,7 @@ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { @Test public void testHandleFileAsByte() { File file = createNewFile("remote-target-dir/handlerContent.test"); - FileTransferringMessageHandler handler = new FileTransferringMessageHandler(smbSessionFactory); + SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(message -> "handlerContent.test"); handler.setBeanFactory(mock(BeanFactory.class)); @@ -93,7 +92,7 @@ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { // @Test // public void testHandleFileMessage() throws Exception { // File file = createNewFile("remote-target-dir/template.mf.test"); -// FileTransferringMessageHandler handler = new FileTransferringMessageHandler(smbSessionFactory); +// SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory); // handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); // handler.setFileNameGenerator(new FileNameGenerator() { // public String generateFileName(Message message) { @@ -115,6 +114,7 @@ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { doAnswer(new Answer() { + @Override public Object answer(InvocationOnMock _invocation) throws Throwable { String path = (String) _invocation.getArguments()[0]; OutputStream os = (OutputStream) _invocation.getArguments()[1]; @@ -136,6 +136,7 @@ public class SmbSendingMessageHandlerTests extends AbstractBaseTests { doAnswer(new Answer() { + @Override public Object answer(InvocationOnMock _invocation) { String path = (String) _invocation.getArguments()[0]; new File(path).mkdirs(); diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionFactoryWithCIFSContextTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionFactoryWithCIFSContextTests.java index 1f00309..8c2cb98 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionFactoryWithCIFSContextTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionFactoryWithCIFSContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ public class SmbSessionFactoryWithCIFSContextTests extends AbstractBaseTests { private SmbSessionFactory smbSessionFactory; @Before - public void prepare() throws Exception { + public void prepare() { smbSession = mock(SmbSession.class); smbSessionFactory = new TestSmbSessionFactory(SingletonContext.getInstance()); @@ -65,7 +65,6 @@ public class SmbSessionFactoryWithCIFSContextTests extends AbstractBaseTests { smbSessionFactory.setUsername("sambaguest"); smbSessionFactory.setPassword("sambaguest"); smbSessionFactory.setShareAndDir("smb-share/"); - smbSessionFactory.setReplaceFile(true); } @Test @@ -77,12 +76,12 @@ public class SmbSessionFactoryWithCIFSContextTests extends AbstractBaseTests { handler.setAutoCreateDirectory(true); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - handler.handleMessage(new GenericMessage("hello")); + handler.handleMessage(new GenericMessage<>("hello")); assertFileExists(file); } class TestSmbSessionFactory extends SmbSessionFactory { - private CIFSContext context = null; + private CIFSContext context; protected TestSmbSessionFactory(CIFSContext _context) { assertNotNull("CIFSContext object is null.", _context);