Add subfolders creation
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / certification / writer / CertFileWriter.java
index 2829517..fec3ebd 100644 (file)
@@ -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();
+        }
+    }
 }