Search code examples
javaspringftpspring-integrationspring-el

Spring Integration FtpOutboundGateway setLocalDirectory with system property and #remoteDirectory


I have an application that pulls in content to a local filesystem via FTP where the location is defined by a system property:

Properties.java:

public static final String STORAGE_PATH = "${project.storage-path}";

FtpConfiguration.java:

@Value(Properties.STORAGE_PATH)
private String storagePath;

@Value(Properties.STORAGE_PATH + "/FTP")
private String ftpStoragePath;

@MessagingGateway(defaultRequestChannel = "ftpChannel")
public interface Gateway {
    public List<File> fetchFiles(String ftpDirectory);
}

@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public FtpOutboundGateway gateway() {

    FtpOutboundGateway gateway = new FtpOutboundGateway(sessionFactory(), "mget", "payload");

    gateway.setOptions("-R");
    gateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("ftpStoragePath+#remoteDirectory"));

    return gateway;
}

I need to copy the content to the local filesystem beginning at STORAGE_PATH appended with "/FTP" while preserving the hierarchy on the remote FTP filesystem (using #remoteDirectory). I am trying every variation of this that makes sense from the docs:

.parseExpression("ftpStoragePath+#remoteDirectory")
.parseExpression("${ftpStoragePath}+#remoteDirectory")
.parseExpression(ftpStoragePath+"+#remoteDirectory")

etc...

I cannot figure out the proper syntax for the SpelExpressionParser. It works if I hardcode the value for ftpStoragePath, but this obviously will not work for custom deployments. The error is generated when a scheduled task calls the following:

FtpTaskSync.java:

List<File> files = AppContextUtils.getBean(Gateway.class).fetchFiles("/*");

This is the error that is thrown when calling .fetchFiles():

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ftpStoragePath' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)

Different variations of SPEL strings throw different errors. I have searched extensively but cannot find another example that is doing something similar, is this possible?


Solution

  • Your last example is close, but you need to make the property value a literal from SpEL's perspective; something like...

    .parseExpression("'" + this.ftpStoragePath + "'" + "#remoteDirectory")