using application.properties values to create a SFTP connection

Viewed 645

my application.properties file look like this:

sftp.host=0.0.0.0
sftp.port=22
sftp.user=foo
sftp.password=pass

and my upload class with upload method looks like this:

public class UpAndDownLoad {

  @Value("${sftp.host}")
  private String sftpHost;
  @Value("${sftp.port}")
  private int sftpPort;
  @Value("${sftp.user}")
  private String sftpUser;
  @Value("${sftp.password}")
  private String sftpPasword;

  private DefaultSftpSessionFactory getSftpFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setAllowUnknownKeys(true);
    factory.setUser(sftpUser);
    factory.setPassword(sftpPasword);
    return factory;
  }

  public void upload() throws IOException {
    SftpSession session = getSftpFactory().getSession();
    InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
    session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
    session.close();
  }

Whenever I type out the values of sftp host, user, password the upload method works meticulously.

  private DefaultSftpSessionFactory getSftpFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost("0.0.0.0");
    factory.setPort(22);
    factory.setAllowUnknownKeys(true);
    factory.setUser("foo");
    factory.setPassword("passs");
    return factory;
  }

But as soon as i pass in the vlaues from the application properties, like shown at the second code block, it fails with: java.lang.IllegalStateException: failed to create SFTP Session

2 Answers

Simply you modify your applicationContext.xml by this adding <context:property-holder>.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

       <context:property-placeholder location="classpath:application.properties"/>

</beans>

or if Java based Config, then this :

@Configuration
@PropertySource("classpath:application.properties")
public class Config {

}

Make the class as component.

@Component
public class UpAndDownLoad {

  @Value("${sftp.host}")
  private String sftpHost;
  @Value("${sftp.port}")
  private int sftpPort;
  @Value("${sftp.user}")
  private String sftpUser;
  @Value("${sftp.password}")
  private String sftpPasword;

  private DefaultSftpSessionFactory getSftpFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setAllowUnknownKeys(true);
    factory.setUser(sftpUser);
    factory.setPassword(sftpPasword);
    return factory;
  }

  public void upload() throws IOException {
    SftpSession session = getSftpFactory().getSession();
    InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
    session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
    session.close();
  }

}

If you want @Value annotations to work you should make this object spring managed. add @Component on it and @Autowire

better way is to create separate SftpProperties as @ConfigurationProperties

@Component
@ConfigurationProperties("sftp")
public class SftpProperties {

    private String host;
    private int port;
    private String user;
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public String toString() {
        return "SftpProperties{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

And you will have more declarative configuration and auto completion into your application.properties

@Component
public class UpAndDownLoad {

    final
    SftpProperties props;

   @Autowired
    public UpAndDownLoad(SftpProperties props) {
        this.props = props;
    }
    private DefaultSftpSessionFactory getSftpFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost(props.getHost());
        factory.setPort(props.getPort());
        factory.setAllowUnknownKeys(true);
        factory.setUser(props.getUser());
        factory.setPassword(props.getPassword());
        return factory;
    }

    public void upload() throws IOException {
        SftpSession session = getSftpFactory().getSession();
        InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
        session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
        session.close();
    }
}
Related