aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-09-02 12:18:49 -0400
committerJim Hahn <jrh3@att.com>2021-09-02 17:55:50 -0400
commit3495931d999bd8022724c6fea0166baf01b5e113 (patch)
treec0385152a6f3c23d3b84824123cce6cf36ccff6b /utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
parent32ab41fa6a6998ff88cd9dbef31f7c65f39ee0e1 (diff)
Create methods for creating temp files
Created utility methods to create temp files and set default permissions. These can be used in other repos to satisfy sonar. Also added code to close streams and clean up temp files created by some junit tests. Modified to use IOUtils to read a stream. Issue-ID: POLICY-3289 Change-Id: Ic83e4f4d7745be7b37ebb42bf1d81e303d8dadc9 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
index 9df98801..7701eae9 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
@@ -31,6 +31,8 @@ import java.nio.file.Files;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and write
@@ -40,6 +42,7 @@ import org.apache.commons.io.IOUtils;
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TextFileUtils {
+ private static final Logger logger = LoggerFactory.getLogger(TextFileUtils.class);
/**
* Method to return the contents of a text file as a string.
@@ -101,4 +104,35 @@ public final class TextFileUtils {
public static String getReaderAsString(final Reader textReader) throws IOException {
return IOUtils.toString(textReader);
}
+
+ /**
+ * Creates a temporary file, only accessible by the owner.
+ *
+ * @param prefix file name prefix
+ * @param suffix file name suffix
+ * @return a new, temporary file
+ * @throws IOException if an error occurs
+ */
+ public static File createTempFile(String prefix, String suffix) throws IOException {
+ /*
+ * Disabling sonar, because setDefaultPermissions() will set the permissions of
+ * the file.
+ */
+ var file = File.createTempFile(prefix, suffix); // NOSONAR
+
+ setDefaultPermissions(file);
+
+ return file;
+ }
+
+ /**
+ * Sets permissions on a file or directory so that only the owner can access it.
+ *
+ * @param file file or directory on which permissions are to be set
+ */
+ public static void setDefaultPermissions(File file) {
+ if (!file.setReadable(true, true) || !file.setWritable(true, true) || !file.setExecutable(true, true)) {
+ logger.warn("cannot set permissions for {}", file);
+ }
+ }
}