generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PO-286: Add common services for sftp (#334)
* PO-286: Initial code * PO-286: update after local testing * Supress CVE * Supress CVE * Configure secrets from key vault * Bumping chart version/ fixing aliases * Add job to test the sftp * Add overloaded delte file methods --------- Co-authored-by: hmcts-jenkins-cnp <60659747+hmcts-jenkins-cnp[bot]@users.noreply.github.com>
- Loading branch information
1 parent
add9b84
commit 650472c
Showing
12 changed files
with
248 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package uk.gov.hmcts.opal.sftp; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.integration.file.remote.RemoteFileTemplate; | ||
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.function.Consumer; | ||
|
||
import static java.lang.String.format; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class SftpService { | ||
|
||
private final DefaultSftpSessionFactory inboundSessionFactory; | ||
private final DefaultSftpSessionFactory outboundSessionFactory; | ||
|
||
public void uploadOutboundFile(byte[] fileBytes, String path, String fileName) { | ||
uploadFile(outboundSessionFactory, fileBytes, path, fileName); | ||
} | ||
|
||
public void uploadFile(DefaultSftpSessionFactory sessionFactory, byte[] fileBytes, String path, String fileName) { | ||
var template = new RemoteFileTemplate<>(sessionFactory); | ||
template.execute(session -> { | ||
session.write(new ByteArrayInputStream(fileBytes), path + "/" + fileName); | ||
log.info(format("File %s uploaded successfully.", fileName)); | ||
return true; | ||
}); | ||
} | ||
|
||
public boolean downloadInboundFile(String path, String fileName, Consumer<InputStream> fileProcessor) { | ||
return downloadFile(inboundSessionFactory, path, fileName, fileProcessor); | ||
} | ||
|
||
public boolean downloadOutboundFile(String path, String fileName, Consumer<InputStream> fileProcessor) { | ||
return downloadFile(outboundSessionFactory, path, fileName, fileProcessor); | ||
} | ||
|
||
public boolean downloadFile(DefaultSftpSessionFactory sessionFactory, | ||
String path, | ||
String fileName, | ||
Consumer<InputStream> fileProcessor) { | ||
var template = new RemoteFileTemplate<>(sessionFactory); | ||
return template.get(path + "/" + fileName, fileProcessor::accept); | ||
} | ||
|
||
public boolean deleteOutboundFile(String path, String fileName) { | ||
return deleteFile(outboundSessionFactory, path, fileName); | ||
} | ||
|
||
public boolean deleteInboundFile(String path, String fileName) { | ||
return deleteFile(inboundSessionFactory, path, fileName); | ||
} | ||
|
||
public boolean deleteFile(DefaultSftpSessionFactory sessionFactory, String path, String fileName) { | ||
var template = new RemoteFileTemplate<>(sessionFactory); | ||
return template.execute(session -> session.remove(path + "/" + fileName)); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/uk/gov/hmcts/opal/sftp/config/SftpConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package uk.gov.hmcts.opal.sftp.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class SftpConfiguration { | ||
|
||
private final SftpProperties sftpProperties; | ||
|
||
@Bean | ||
public DefaultSftpSessionFactory inboundSessionFactory() { | ||
DefaultSftpSessionFactory sessionFactory = new DefaultSftpSessionFactory(); | ||
sessionFactory.setHost(sftpProperties.getInbound().getHost()); | ||
sessionFactory.setPort(sftpProperties.getInbound().getPort()); | ||
sessionFactory.setUser(sftpProperties.getInbound().getUser()); | ||
sessionFactory.setPassword(sftpProperties.getInbound().getPassword()); | ||
sessionFactory.setAllowUnknownKeys(true); | ||
|
||
return sessionFactory; | ||
} | ||
|
||
@Bean | ||
public DefaultSftpSessionFactory outboundSessionFactory() { | ||
DefaultSftpSessionFactory sessionFactory = new DefaultSftpSessionFactory(); | ||
sessionFactory.setHost(sftpProperties.getOutbound().getHost()); | ||
sessionFactory.setPort(sftpProperties.getOutbound().getPort()); | ||
sessionFactory.setUser(sftpProperties.getOutbound().getUser()); | ||
sessionFactory.setPassword(sftpProperties.getOutbound().getPassword()); | ||
sessionFactory.setAllowUnknownKeys(true); | ||
|
||
return sessionFactory; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/uk/gov/hmcts/opal/sftp/config/SftpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package uk.gov.hmcts.opal.sftp.config; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class SftpConnection { | ||
|
||
private String host; | ||
private int port; | ||
private String user; | ||
private String password; | ||
private String location; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/uk/gov/hmcts/opal/sftp/config/SftpProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package uk.gov.hmcts.opal.sftp.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Data | ||
@ConfigurationProperties(prefix = "opal.sftp") | ||
public class SftpProperties { | ||
|
||
private SftpConnection inbound; | ||
private SftpConnection outbound; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.