diff options
Diffstat (limited to 'certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java')
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java index 2829517c..fec3ebd3 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java @@ -23,6 +23,7 @@ import org.onap.aaf.certservice.client.certification.exception.CertFileWriterExc import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; @@ -32,10 +33,15 @@ public class CertFileWriter { private static final Logger LOGGER = LoggerFactory.getLogger(CertFileWriter.class); private final String destPath; - public CertFileWriter(String destPath) { + private CertFileWriter(String destPath) { this.destPath = destPath; } + public static CertFileWriter createWithDir(String destPath) { + createDirIfNotExists(destPath); + return new CertFileWriter(destPath); + } + public void saveData(byte[] data, String filename) throws CertFileWriterException { LOGGER.debug("Attempt to save file {} in path {}", filename, destPath); try (FileOutputStream outputStream = new FileOutputStream(Path.of(destPath, filename).toString())) { @@ -45,4 +51,12 @@ public class CertFileWriter { throw new CertFileWriterException(e); } } + + private static void createDirIfNotExists(String destPath) { + File destFolderPath = new File(destPath); + if (!destFolderPath.exists()) { + LOGGER.debug("Destination path not exists, subdirectories are created"); + destFolderPath.mkdirs(); + } + } } |