-1

I have a fresh Spring Boot Project. I want to setup a simple SMB Server, which I can connect to with my Windows Explorer. I simply want to share one folder and be able to connect to the server with a set username and password. The folder I want to share is D:\smb-files\folder1 .

I configured a SMC Configuration:

package com.example.smb;

import java.io.File;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.smb.dsl.Smb;
import org.springframework.integration.smb.session.SmbSessionFactory;

import jcifs.DialectVersion;

@Configuration
public class SmbConfig {
    
    public static final String REMOTE_DIRECTORY = "folder1";

    @Bean
    public SmbSessionFactory smbSessionFactory() {
        SmbSessionFactory smbSession = new SmbSessionFactory();
        smbSession.setHost("myHost");
        smbSession.setPort(445);
        smbSession.setDomain("myDomain");
        smbSession.setUsername("myUser");
        smbSession.setPassword("myPassword");
        smbSession.setShareAndDir("myShareAndDir");
        smbSession.setSmbMinVersion(DialectVersion.SMB210);
        smbSession.setSmbMaxVersion(DialectVersion.SMB311);
        return smbSession;
    }

    @Bean
    public IntegrationFlow smbInboundFlow() {
        
        return IntegrationFlow
            .from(Smb.inboundAdapter(smbSessionFactory())
                    .preserveTimestamp(true)
                    .remoteDirectory(REMOTE_DIRECTORY)
                    .regexFilter(".*\\.txt$")
                    .localFilename(f -> f.toUpperCase() + ".a")
                    .localDirectory(new File("d:/smb-files")),
                        e -> e.id("smbInboundAdapter")
                    .autoStartup(true)
                    .poller(Pollers.fixedDelay(5000)))
            .handle(m -> System.out.println(m.getPayload()))
            .get();
    }
    
    @Bean
    public IntegrationFlow smbOutboundFlow() {
        return IntegrationFlow.from("toSmbChannel")
                .handle(Smb.outboundAdapter(smbSessionFactory(), FileExistsMode.REPLACE)
                        .useTemporaryFileName(false)
                        .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
                        .remoteDirectory(REMOTE_DIRECTORY)
                ).get();
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toSmbChannel")
         void sendToSmb(File file);
    }
}

I tried to stick as close to this Spring Documentation as possible. I added a global String "REMOTE_DIRECTORY" and changed the localDirectory to "d:/smb-files". (I created this directory on my disk and even tested to write a simple file to the directory -> it worked). I created a folder within smb-files called folder1.
I added the following dependency to the pom.xml :

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-smb</artifactId>
</dependency>

I configured Spring Integrations to log on DEBUG level (application.properties:

logging.level.org.springframework.integration=DEBUG

Error:

In Short: Problem occurred while synchronizing 'folder1' to local directory

Log of the application:

[payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:10.724+02:00 DEBUG 4300 --- [   scheduling-1] o.s.integration.handler.LoggingHandler   : bean '_org.springframework.integration.errorLogger.handler' for component '_org.springframework.integration.errorLogger' received message: ErrorMessage [payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:10.725+02:00 ERROR 4300 --- [   scheduling-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:348)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:267)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:69)
    at org.springframework.integration.endpoint.AbstractFetchLimitingMessageSource.doReceive(AbstractFetchLimitingMessageSource.java:47)
    at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:142)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:212)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:443)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.pollForMessage(AbstractPollingEndpoint.java:412)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$createPoller$4(AbstractPollingEndpoint.java:348)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:57)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$createPoller$5(AbstractPollingEndpoint.java:341)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:96)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:461)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:341)
    ... 20 more
Caused by: java.lang.IllegalStateException: Failed to create session.
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:65)
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:39)
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:447)
    ... 21 more
Caused by: java.io.IOException: Unable to initialize share: smb://myDomain;myUser:myPassword@myHost:445/myShareAndDir
    at org.springframework.integration.smb.session.SmbShare.init(SmbShare.java:113)
    at org.springframework.integration.smb.session.SmbSessionFactory.createSession(SmbSessionFactory.java:86)
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:62)
    ... 23 more
Caused by: jcifs.smb.SmbException: Failed to connect to server
    at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:429)
    at jcifs.smb.SmbFile.ensureTreeConnected(SmbFile.java:573)
    at jcifs.smb.SmbFile.exists(SmbFile.java:873)
    at org.springframework.integration.smb.session.SmbShare.init(SmbShare.java:98)
    ... 25 more
Caused by: java.net.UnknownHostException: myHost
    at jcifs.netbios.NameServiceClientImpl.getAllByName(NameServiceClientImpl.java:1054)
    at jcifs.netbios.NameServiceClientImpl.getAllByName(NameServiceClientImpl.java:55)
    at jcifs.smb.SmbTransportPoolImpl.getSmbTransport(SmbTransportPoolImpl.java:173)
    at jcifs.smb.SmbTransportPoolImpl.getSmbTransport(SmbTransportPoolImpl.java:48)
    at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:565)
    at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:489)
    at jcifs.smb.SmbTreeConnection.connect(SmbTreeConnection.java:465)
    at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:426)
    ... 28 more

2023-04-29T18:35:10.725+02:00 DEBUG 4300 --- [   scheduling-1] o.s.i.channel.PublishSubscribeChannel    : postSend (sent=true) on channel 'bean 'errorChannel'', message: ErrorMessage [payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:15.733+02:00  INFO 4300 --- [   scheduling-1] o.s.i.smb.session.SmbSessionFactory      : SMB share init: myHost:445/myShareAndDir


This was a empty Spring project created on start.spring.io without any dependencies added.

I tried to configure a valid hostname and a valid domain. All lead to the same error. In addition to that I tried to change the ShareAndDir name to match the REMOTE_DIRECTORY . I deleted and recreated the directories on my disk.

I read the Spring documentation multiple times.

I appreciate any help. Thanks in advance.

Max
  • 11
  • 1
  • I am not sure I understood what you are trying to do. Do you want create a SMB server or client? AFAIK, Spring SMB/CIFS integration allows you to connect to an existing shared folder, and not to create a local SMB shared folder. – luca.vercelli Apr 29 '23 at 17:26
  • Thanks for your comment. I want to provide folders to be accessable via SMB. This programm will run on a linux system. I want to provide specific locations to be accesssible for certain users. – Max Apr 29 '23 at 18:13

1 Answers1

0

I don't know much about Springboot, but seems like this is the cause:

Caused by: java.net.UnknownHostException: myHost

You don't specify a variable, but a literal string, which is a invalid address. You should use something like "127.0.0.1".

That is the same with the other configurations, except the port:

smbSession.setHost("myHost");
smbSession.setPort(445);
smbSession.setDomain("myDomain");
smbSession.setUsername("myUser");
smbSession.setPassword("myPassword");
smbSession.setShareAndDir("myShareAndDir");
SteinGaming
  • 1,163
  • 2
  • 5
  • 10