|
| 1 | +[[smb]] |
| 2 | +== SMB Support |
| 3 | + |
| 4 | +Spring Integration provides support for file transfer operations with SMB. |
| 5 | + |
| 6 | +The https://en.wikipedia.org/wiki/Server_Message_Block[Server Message Block] (SMB) is a simple network protocol that lets you transfer files to a shared file server. |
| 7 | + |
| 8 | +You need to include this dependency into your project: |
| 9 | + |
| 10 | +==== |
| 11 | +[source, xml, subs="normal", role="primary"] |
| 12 | +.Maven |
| 13 | +---- |
| 14 | +<dependency> |
| 15 | + <groupId>org.springframework.integration</groupId> |
| 16 | + <artifactId>spring-integration-smb</artifactId> |
| 17 | + <version>{project-version}</version> |
| 18 | +</dependency> |
| 19 | +---- |
| 20 | +[source, groovy, subs="normal", role="secondary"] |
| 21 | +.Gradle |
| 22 | +---- |
| 23 | +compile "org.springframework.integration:spring-integration-smb:{project-version}" |
| 24 | +---- |
| 25 | +==== |
| 26 | + |
| 27 | +=== Overview |
| 28 | + |
| 29 | +The https://github.com/codelibs/jcifs[Java CIFS] Client Library has been chosen as a Java implementation for the CIFS/SMB networking protocol. |
| 30 | +Its `SmbFile` abstraction is simply wrapped to the Spring Integration "Remote File" foundations like `SmbSession`, `SmbRemoteFileTemplate`, etc. |
| 31 | + |
| 32 | +The SMB Channel Adapters and support classes implementations are fully similar to existing components for (S)FTP or AWS S3 protocols. |
| 33 | +So, if you familiar with those components it is pretty straightforward to use. |
| 34 | + |
| 35 | +[[smb-session-factory]] |
| 36 | +=== SMB Session Factory |
| 37 | + |
| 38 | +Before configuring the SMB adapter, you must configure an SMB session factory. |
| 39 | +You can configure the SMB session factory with a regular bean definition, as the following examples show: |
| 40 | + |
| 41 | +The `SmbSessionFactory` exposes options to set the SMB protocol with Min/Max versions. |
| 42 | +For example, supporting a minimum version of SMB 2.1 and a maximum version of the SMB 3.1.1: |
| 43 | + |
| 44 | +[source,java] |
| 45 | +---- |
| 46 | +@Bean |
| 47 | +public SmbSessionFactory smbSessionFactory() { |
| 48 | + SmbSessionFactory smbSession = new SmbSessionFactory(); |
| 49 | + smbSession.setHost("myHost"); |
| 50 | + smbSession.setPort(445); |
| 51 | + smbSession.setDomain("myDomain"); |
| 52 | + smbSession.setUsername("myUser"); |
| 53 | + smbSession.setPassword("myPassword"); |
| 54 | + smbSession.setShareAndDir("myShareAndDir"); |
| 55 | + smbSession.setSmbMinVersion(DialectVersion.SMB210); |
| 56 | + smbSession.setSmbMaxVersion(DialectVersion.SMB311); |
| 57 | + return smbSession; |
| 58 | +} |
| 59 | +---- |
| 60 | + |
| 61 | +The `SmbSessionFactory` can be initialized with a custom `jcifs.CIFSContext`. |
| 62 | + |
| 63 | +NOTE: Setting of the SMB protocol Min/Max versions must be done in your implementation of `jcifs.CIFSContext`. |
| 64 | + |
| 65 | +[source,java] |
| 66 | +---- |
| 67 | +@Bean |
| 68 | +public SmbSessionFactory smbSessionFactory() { |
| 69 | + SmbSessionFactory smbSession = new SmbSessionFactory(new MyCIFSContext()); |
| 70 | + smbSession.setHost("myHost"); |
| 71 | + smbSession.setPort(445); |
| 72 | + smbSession.setDomain("myDomain"); |
| 73 | + smbSession.setUsername("myUser"); |
| 74 | + smbSession.setPassword("myPassword"); |
| 75 | + smbSession.setShareAndDir("myShareAndDir"); |
| 76 | + return smbSession; |
| 77 | +} |
| 78 | +---- |
| 79 | + |
| 80 | +[[smb-inbound]] |
| 81 | +=== SMB Inbound Channel Adapter |
| 82 | + |
| 83 | +To download SMB files locally the `SmbInboundFileSynchronizingMessageSource` is provided. |
| 84 | +It is simple extension of the `AbstractInboundFileSynchronizingMessageSource` which requires `SmbInboundFileSynchronizer` injection. |
| 85 | +For filtering remote files you still can use any existing `FileListFilter` implementations, but particular `SmbRegexPatternFileListFilter` and `SmbSimplePatternFileListFilter` are provided. |
| 86 | + |
| 87 | +[source,java] |
| 88 | +---- |
| 89 | +@Bean |
| 90 | +public SmbInboundFileSynchronizer smbInboundFileSynchronizer() { |
| 91 | + SmbInboundFileSynchronizer fileSynchronizer = |
| 92 | + new SmbInboundFileSynchronizer(smbSessionFactory()); |
| 93 | + fileSynchronizer.setFilter(compositeFileListFilter()); |
| 94 | + fileSynchronizer.setRemoteDirectory("mySharedDirectoryPath"); |
| 95 | + fileSynchronizer.setDeleteRemoteFiles(true); |
| 96 | + return fileSynchronizer; |
| 97 | +} |
| 98 | +
|
| 99 | +@Bean |
| 100 | +public CompositeFileListFilter<SmbFile> compositeFileListFilter() { |
| 101 | + CompositeFileListFilter<SmbFile> filters = new CompositeFileListFilter<>(); |
| 102 | + filters.addFilter(new SmbRegexPatternFileListFilter("^(?i).+((\\.txt))$")); |
| 103 | + return filters; |
| 104 | +} |
| 105 | +
|
| 106 | +@Bean |
| 107 | +public MessageChannel smbFileInputChannel() { |
| 108 | + return new DirectChannel(); |
| 109 | +} |
| 110 | +
|
| 111 | +@Bean |
| 112 | +@InboundChannelAdapter(value = "smbFileInputChannel", |
| 113 | + poller = @Poller(fixedDelay = "2000")) |
| 114 | +public MessageSource<File> smbMessageSource() { |
| 115 | + SmbInboundFileSynchronizingMessageSource messageSource = |
| 116 | + new SmbInboundFileSynchronizingMessageSource(smbInboundFileSynchronizer()); |
| 117 | + messageSource.setLocalDirectory(new File("myLocalDirectoryPath")); |
| 118 | + messageSource.setAutoCreateLocalDirectory(true); |
| 119 | + return messageSource; |
| 120 | +} |
| 121 | +---- |
| 122 | + |
| 123 | +For XML configuration the `<int-smb:inbound-channel-adapter>` component is provided. |
| 124 | + |
| 125 | +[[smb-outbound]] |
| 126 | +=== SMB Outbound Channel Adapter |
| 127 | + |
| 128 | +For writing files to an SMB share, and for XML `<int-smb:outbound-channel-adapter>` component we use the `SmbMessageHandler`. |
| 129 | +In case of Java configuration a `SmbMessageHandler` should be supplied with the `SmbSessionFactory` (or `SmbRemoteFileTemplate`). |
| 130 | + |
| 131 | +[source,java] |
| 132 | +---- |
| 133 | +@Bean |
| 134 | +@ServiceActivator(inputChannel = "storeToSmbShare") |
| 135 | +public MessageHandler smbMessageHandler(SmbSessionFactory smbSessionFactory) { |
| 136 | + SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory); |
| 137 | + handler.setRemoteDirectoryExpression( |
| 138 | + new LiteralExpression("remote-target-dir")); |
| 139 | + handler.setFileNameGenerator(m -> |
| 140 | + m.getHeaders().get(FileHeaders.FILENAME, String.class) + ".test"); |
| 141 | + handler.setAutoCreateDirectory(true); |
| 142 | + return handler; |
| 143 | +} |
| 144 | +---- |
0 commit comments