From 672a196da734dccfe54840440c8fd4640870f0f2 Mon Sep 17 00:00:00 2001 From: Gregory Bragg Date: Wed, 1 May 2019 14:44:42 -0400 Subject: [PATCH] GH-212: Smb with custom transport configuration Fixes https://github.com/spring-projects/spring-integration-extensions/issues/212 * Updated to be able to use min/max SMB version property settings * cleanup of formatting of file * Updated based on review comments * Updated readme for new functionality * Some polishing of readme and follow current naming conventions * Updated, missed naming convention on Javadoc * Updated per review comments * Enhancement - exposing the jcifs.CIFSContext interface * fixed check style build errors * Added JUnit test --- spring-integration-smb/README.md | 19 +++ .../smb/session/SmbSessionFactory.java | 34 +++- .../integration/smb/session/SmbShare.java | 12 ++ ...SmbSessionFactoryWithCIFSContextTests.java | 155 ++++++++++++++++++ 4 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionFactoryWithCIFSContextTests.java diff --git a/spring-integration-smb/README.md b/spring-integration-smb/README.md index f28e586..70f9124 100644 --- a/spring-integration-smb/README.md +++ b/spring-integration-smb/README.md @@ -29,6 +29,7 @@ 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` ## Overview @@ -84,3 +85,21 @@ public SmbSessionFactory smbSessionFactory() { return smbSession; } ```` + +### Intializing SmbSessionFactory with a custom implementation of the jcifs.CIFSContext interface + +NOTE: Setting of the SMB protocol min/max versions must be done in your implementation of jcifs.CIFSContext + +````java +@Bean +public SmbSessionFactory smbSessionFactory(new myCIFSContext()) { + SmbSessionFactory smbSession = new SmbSessionFactory(); + smbSession.setHost("myHost"); + smbSession.setPort(445); + smbSession.setDomain("myDomain"); + smbSession.setUsername("myUser"); + smbSession.setPassword("myPassword"); + smbSession.setShareAndDir("myShareAndDir"); + return smbSession; +} +```` 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 066a74e..338372b 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 @@ -23,7 +23,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.util.Assert; +import jcifs.CIFSContext; import jcifs.smb.SmbFile; /** @@ -36,10 +38,23 @@ public class SmbSessionFactory extends SmbConfig implements SessionFactory handler = new FileTransferringMessageHandler(smbSessionFactory); + handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); + handler.setFileNameGenerator(message -> "handlerContent.test"); + handler.setAutoCreateDirectory(true); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); + handler.handleMessage(new GenericMessage("hello")); + assertFileExists(file); + } + + class TestSmbSessionFactory extends SmbSessionFactory { + private CIFSContext context = null; + + protected TestSmbSessionFactory(CIFSContext _context) { + assertNotNull("CIFSContext object is null.", _context); + this.context = _context; + } + + @Override + protected SmbSession createSession() { + try { + // test for a constructor with a CIFSContext + SmbShare smbShare = new SmbShare(this, this.context); + assertNotNull("SmbShare object is null.", smbShare); + assertThat(smbShare.toString()).isEqualTo("smb://sambaguest:sambaguest@localhost:445/smb-share/"); + + // the rest has been copied from SmbSendingMessageHandlerTests + when(smbSession.remove(Mockito.anyString())).thenReturn(true); + when(smbSession.list(Mockito.anyString())).thenReturn(new SmbFile[0]); + + doAnswer(new Answer() { + + @Override + public Object answer(InvocationOnMock _invocation) throws Throwable { + String path = (String) _invocation.getArguments()[0]; + OutputStream os = (OutputStream) _invocation.getArguments()[1]; + writeToFile((this.getClass().getSimpleName() + " : TEST : " + path).getBytes(), os); + return null; + } + }).when(smbSession).read(Mockito.anyString(), Mockito.any(OutputStream.class)); + + doAnswer(_invocation -> { + InputStream inputStream = (InputStream) _invocation.getArguments()[0]; + String path = (String) _invocation.getArguments()[1]; + writeToFile(inputStream, path); + return null; + }).when(smbSession) + .write(Mockito.any(InputStream.class), Mockito.anyString()); + + // when(smbSession.write(Mockito.any(byte[].class), Mockito.anyString())).thenReturn(null); + // when(smbSession.write(Mockito.any(File.class), Mockito.anyString())).thenReturn(null); + + doAnswer(new Answer() { + + @Override + public Object answer(InvocationOnMock _invocation) { + String path = (String) _invocation.getArguments()[0]; + new File(path).mkdirs(); + return null; + } + }).when(smbSession).mkdir(Mockito.anyString()); + + doAnswer(_invocation -> { + String pathFrom = (String) _invocation.getArguments()[0]; + String pathTo = (String) _invocation.getArguments()[1]; + new File(pathFrom).renameTo(new File(pathTo)); + return null; + }).when(smbSession) + .rename(Mockito.anyString(), Mockito.anyString()); + + doNothing().when(smbSession).close(); + when(smbSession.isOpen()).thenReturn(true); + return smbSession; + } + catch (Exception _ex) { + throw new RuntimeException("Failed to create mock session.", _ex); + } + } + + } + +}