changeset 1173:00b81be47b1c

FileValidator
author Devel 2
date Tue, 16 Jun 2020 15:57:46 +0200
parents 0e280ef89e8e
children f19a7d241c8e
files stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/main/java/com/passus/st/validation/FileValidator.java
diffstat 2 files changed, 95 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 16 15:57:21 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 16 15:57:46 2020 +0200
@@ -33,6 +33,7 @@
 import com.passus.st.plugin.PluginConstants;
 import com.passus.st.reader.pcap.PcapDataBlock;
 import com.passus.st.reader.pcap.PcapDataBlockReader;
+import com.passus.st.validation.FileValidator;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -526,7 +527,7 @@
             );
 
             return mapDef(
-                    tupleDef("fileName", STRING_DEF),
+                    tupleDef("fileName", valueDef().addValidator(new FileValidator())),
                     tupleDef("loops", INT_GREATER_EQUAL_ZERO_DEF).setRequired(false),
                     tupleDef("loopDelay", LONG_GREATER_EQUAL_ZERO_DEF).setRequired(false),
                     tupleDef("sessionProc", sessionProcDef).setRequired(false),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/validation/FileValidator.java	Tue Jun 16 15:57:46 2020 +0200
@@ -0,0 +1,93 @@
+package com.passus.st.validation;
+
+import com.passus.config.ConfigurationContext;
+import com.passus.config.validation.Errors;
+import com.passus.config.validation.Validator;
+
+import java.io.File;
+
+public class FileValidator implements Validator {
+
+    public static final PortValidator INSTANCE = new PortValidator();
+
+    private boolean exists = true;
+
+    private boolean readable = true;
+
+    private boolean writable = false;
+
+    public FileValidator() {
+    }
+
+    public FileValidator(boolean exists, boolean readable, boolean writable) {
+        this.exists = exists;
+        this.readable = readable;
+        this.writable = writable;
+    }
+
+    public boolean exists() {
+        return exists;
+    }
+
+    public FileValidator exists(boolean exists) {
+        this.exists = exists;
+        return this;
+    }
+
+    public boolean readable() {
+        return readable;
+    }
+
+    public FileValidator readable(boolean readable) {
+        this.readable = readable;
+        return this;
+    }
+
+    public boolean writable() {
+        return writable;
+    }
+
+    public FileValidator writable(boolean writable) {
+        this.writable = writable;
+        return this;
+    }
+
+    @Override
+    public boolean supports(Class<?> clazz) {
+        return File.class.equals(clazz)
+                || String.class.isAssignableFrom(clazz);
+    }
+
+    @Override
+    public void validate(Object target, Errors errors, ConfigurationContext context) {
+        try {
+            if (target == null) {
+                return;
+            }
+
+            File file;
+            if (target instanceof File) {
+                file = (File) target;
+            } else if (target instanceof String) {
+                file = new File(target.toString());
+            } else {
+                errors.reject("Invalid file definition.");
+                return;
+            }
+
+            if(!file.isAbsolute()) {
+
+            }
+
+            if (exists && !file.exists()) {
+                errors.reject("File '" + target + "' does not exists.");
+            } else if (readable && !file.canRead()) {
+                errors.reject("File '" + target + "' is not readable.");
+            } else if (writable && !file.canWrite()) {
+                errors.reject("File '" + target + "' is not writable.");
+            }
+        } catch (Exception ignore) {
+            errors.reject("Invalid file definition.");
+        }
+    }
+}
\ No newline at end of file