filechampion4j 0.9.8.3
File validation library for Java
Loading...
Searching...
No Matches
dev.filechampion.filechampion4j.FileValidator Class Reference
Collaboration diagram for dev.filechampion.filechampion4j.FileValidator:
Collaboration graph

Public Member Functions

 FileValidator (JSONObject configJsonObject) throws IllegalArgumentException
 
ValidationResponse validateFile (String fileCategory, byte[] originalFile, String fileName, Path outputDir, String mimeString)
 
ValidationResponse validateFile (String fileCategory, Path filePath, String fileName, Path outputDir, String mimeString)
 
ValidationResponse validateFile (String fileCategory, byte[] originalFile, String fileName, String mimeString)
 
ValidationResponse validateFile (String fileCategory, byte[] originalFile, String fileName, Path outputDir)
 
ValidationResponse validateFile (String fileCategory, Path filePath, String fileName, String mimeString)
 
ValidationResponse validateFile (String fileCategory, Path filePath, String fileName, Path outputDir)
 
ValidationResponse validateFile (String fileCategory, byte[] originalFile, String fileName)
 
ValidationResponse validateFile (String fileCategory, Path filePath, String fileName)
 

Private Member Functions

ValidationResponse validateFileMain ()
 
ValidationResponse doValidations (String originalFilenameClean)
 
void logInfo (StringBuilder message)
 
void logWarn (StringBuilder message)
 
void logSevere (StringBuilder message)
 
void logFine (StringBuilder message)
 
void loadPlugins ()
 
void checkPluginsConfig ()
 
void checkPluginsExist (JSONObject validationsJsonObject, String categroyKey, String extensionKey)
 
void checkMethodInputs ()
 
String executeBeforePlugins (String fileCategory, String fileExtension)
 
void executeAfterPlugins ()
 
String executeAfterPluginsProcess (String fileCategory, String fileExtension)
 
String executePlugin (String extensionPlugin, Map< String, StepConfig > stepConfigs, String fileExtension)
 
boolean isBlank (String str)
 
Boolean deleteTempDir (Path tempFilePath)
 
String getFileExtension (String fileName)
 
Map< String, String > calculateChecksum (byte[] fileBytes)
 
String saveFileToOutputDir (String fileCategory, String fileExtension, Path outDir, String fileName, byte[] fileBytes)
 
String setFileAttributes (Path targetFilePath, String changeOwnershipUser, String changePermissionsMode)
 

Private Attributes

JSONObject configJsonObject
 
PluginsHelper pluginsHelper
 
Map< String, StepConfigstepConfigsBefore = new HashMap<>()
 
Map< String, StepConfigstepConfigsAfter = new HashMap<>()
 
Extensions extensions
 
ValidationsHelper validationsHelper
 
StringBuilder sharedStringBuilder = new StringBuilder()
 
String sharedStepMessage = "Step: "
 
String errorResponse = "File is not valid."
 
String fileCategory
 
String fileName
 
Path filePath
 
byte[] originalFile
 
String mimeString
 
Path outDir
 
String fileExtension
 
String commonFileError = "Error reading file: "
 
String commonLogString
 
int responseMsgCountFail
 
StringBuilder sbresponseAggregationFail
 
int responseMsgCountSuccess
 
StringBuilder sbresponseAggregationSuccess
 
List< String > checksumAlgorithms = new ArrayList<>()
 

Static Private Attributes

static final Logger LOG = LogManager.getLogManager().getLogger("")
 
static final Logger LOGGER = Logger.getLogger(FileValidator.class.getName())
 
static final List< String > supportedAlgorithms = Arrays.asList("MD5", "SHA-1", "SHA-256", "SHA-512")
 

Detailed Description

This class is used to validate files

Author
filechampion
Version
0.9.8.3
See also
FileChampion4j Docs

Definition at line 29 of file FileValidator.java.

Constructor & Destructor Documentation

◆ FileValidator()

dev.filechampion.filechampion4j.FileValidator.FileValidator ( JSONObject  configJsonObject) throws IllegalArgumentException

This method is used to initiate the class with relevant json configurations

Parameters
configJsonObject(JSONObject) - The json object containing the Validations and Plugins configurations for the class.
Exceptions
IllegalArgumentException- If the configJsonObject is null or empty, or if the configJsonObject contains errors.

Definition at line 71 of file FileValidator.java.

71 {
72 if (configJsonObject == null || configJsonObject.isEmpty() || !configJsonObject.has("Validations")) {
73 throw new IllegalArgumentException("Config JSON object cannot be null or empty, and must have Validations section.");
74 } else {
75 try {
76 if (configJsonObject.has("General") && configJsonObject.getJSONObject("General").has("Checksums"))
77 {
78 JSONArray checksums = configJsonObject.getJSONObject("General").getJSONArray("Checksums");
79 for (int i = 0; i < checksums.length(); i++) {
80 if (!supportedAlgorithms.contains(checksums.getString(i))) {
81 throw new IllegalArgumentException("The hash algorithm '" + checksums.getString(i) + "'' is not one of: " + supportedAlgorithms.toString() + ".");
82 }
83 checksumAlgorithms.add(checksums.getString(i));
84 }
85 } else {
86 checksumAlgorithms.add("SHA-256");
87 }
88
89 extensions = new Extensions(configJsonObject.getJSONObject("Validations"));
90 validationsHelper = new ValidationsHelper(extensions);
91 } catch (Exception e) {
92 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error initializing extensions: ")
93 .append(e.getMessage());
95 throw new IllegalArgumentException("Error initializing extensions: " + e.getMessage());
96 }
97 }
98
99 this.configJsonObject = configJsonObject;
100 if (configJsonObject.has("Plugins")) {
101 try {
102 pluginsHelper = new PluginsHelper(configJsonObject.getJSONObject("Plugins"));
103 loadPlugins();
105 } catch (Exception e) {
106 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error initializing plugins: ")
107 .append(e.getMessage());
109 throw new IllegalArgumentException("Error initializing plugins: " + e.getMessage());
110 }
111 }
112 }
static final List< String > supportedAlgorithms

References dev.filechampion.filechampion4j.FileValidator.checkPluginsConfig(), dev.filechampion.filechampion4j.FileValidator.checksumAlgorithms, dev.filechampion.filechampion4j.FileValidator.configJsonObject, dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.loadPlugins(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.pluginsHelper, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, dev.filechampion.filechampion4j.FileValidator.supportedAlgorithms, and dev.filechampion.filechampion4j.FileValidator.validationsHelper.

Here is the call graph for this function:

Member Function Documentation

◆ calculateChecksum()

Map< String, String > dev.filechampion.filechampion4j.FileValidator.calculateChecksum ( byte[]  fileBytes)
private

Calculate the checksum of the file

Parameters
fileBytes(byte[]) the file bytes of the file being validated
Returns
String (String) the SHA-256 checksum of the file

Definition at line 792 of file FileValidator.java.

792 {
793 CalculateChecksum checksumInstance = new CalculateChecksum(fileBytes);
794 Map<String, String> checksums = new HashMap<>();
795 for (String algorithm : checksumAlgorithms) {
796 try {
797 byte[] checksum = checksumInstance.getChecksum(algorithm);
798 checksums.put(algorithm, new BigInteger(1, checksum).toString(16));
799 } catch (Exception e) {
800 e.printStackTrace();
801 return null;
802 }
803 }
804 return checksums.size() > 0 ? checksums : null;
805 }

References dev.filechampion.filechampion4j.FileValidator.checksumAlgorithms, and dev.filechampion.filechampion4j.CalculateChecksum.getChecksum().

Referenced by dev.filechampion.filechampion4j.FileValidator.doValidations().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ checkMethodInputs()

void dev.filechampion.filechampion4j.FileValidator.checkMethodInputs ( )
private

This method is used to check that method inputs are as expected

Exceptions
IllegalArgumentException- If any of the inputs are null, empty, or does not contain appropiate values
IOException- If there is an error reading the file

Definition at line 515 of file FileValidator.java.

515 {
516 if (isBlank(fileCategory)) {
517 throw new IllegalArgumentException("fileCategory cannot be null or empty.");
518 }
519 if (isBlank(fileName)) {
520 throw new IllegalArgumentException("fileName cannot be null or empty.");
521 }
522 if (fileName.indexOf(".") == -1) {
523 throw new IllegalArgumentException("fileName must contain a file extension.");
524 }
525 if (outDir != null && !Files.exists(outDir)) {
526 throw new IllegalArgumentException("outDir does not exist.");
527 }
528 if (filePath != null && !Files.exists(filePath)) {
529 throw new IllegalArgumentException("filepath does not exist.");
530 } else if (filePath != null && Files.isDirectory(filePath)) {
531 throw new IllegalArgumentException("filepath cannot be a directory.");
532 } else if (filePath != null) {
533 try {
534 Path path = filePath;
535 originalFile = Files.readAllBytes(path);
536 } catch (IOException e) {
538 .append(e.getMessage());
540 throw new IllegalArgumentException("Error reading file: " + e.getMessage());
541 }
542 }
543 if (originalFile == null || originalFile.length == 0) {
544 throw new IllegalArgumentException("originalFile cannot be null or empty.");
545 }
546 }

References dev.filechampion.filechampion4j.FileValidator.commonFileError, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.filePath, dev.filechampion.filechampion4j.FileValidator.isBlank(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.outDir, and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ checkPluginsConfig()

void dev.filechampion.filechampion4j.FileValidator.checkPluginsConfig ( )
private

Method to check that all extensions defined in config exist in plugins configuration from Validation.json

Definition at line 480 of file FileValidator.java.

480 {
481 // check that all extensions defined plugins exist in maps
482 JSONObject validationsJsonObject = configJsonObject.getJSONObject("Validations");
483 for (int i = 0; i < validationsJsonObject.length(); i++) {
484 String categroyKey = validationsJsonObject.names().getString(i);
485 for (int k=0; k < validationsJsonObject.getJSONObject(categroyKey).length(); k++) {
486 String extensionKey = validationsJsonObject.getJSONObject(categroyKey).names().getString(k);
487 if (validationsJsonObject.getJSONObject(categroyKey).getJSONObject(extensionKey).has("extension_plugins")) {
488 checkPluginsExist(validationsJsonObject, categroyKey, extensionKey);
489 }
490 }
491 }
492 }
void checkPluginsExist(JSONObject validationsJsonObject, String categroyKey, String extensionKey)

References dev.filechampion.filechampion4j.FileValidator.checkPluginsExist(), and dev.filechampion.filechampion4j.FileValidator.configJsonObject.

Referenced by dev.filechampion.filechampion4j.FileValidator.FileValidator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ checkPluginsExist()

void dev.filechampion.filechampion4j.FileValidator.checkPluginsExist ( JSONObject  validationsJsonObject,
String  categroyKey,
String  extensionKey 
)
private

Method to check that each extension defined in config exist in plugins configuration

Parameters
validationsJsonObject(JSONObject) - the validations.json object
categroyKey(String) - the category key
extensionKey(String) - the extension key

Definition at line 500 of file FileValidator.java.

500 {
501 for (String pluginName : validationsJsonObject.getJSONObject(categroyKey).getJSONObject(extensionKey).getJSONArray("extension_plugins").toList().toArray(new String[0])) {
502 if (!stepConfigsBefore.containsKey(pluginName) && !stepConfigsAfter.containsKey(pluginName)) {
503 sharedStringBuilder.replace(0, sharedStringBuilder.length(), sharedStepMessage).append(pluginName).append(" defined in config does not exist in plugins configuration");
505 throw new IllegalArgumentException(sharedStringBuilder.toString());
506 }
507 }
508 }

References dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.sharedStepMessage, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, dev.filechampion.filechampion4j.FileValidator.stepConfigsAfter, and dev.filechampion.filechampion4j.FileValidator.stepConfigsBefore.

Referenced by dev.filechampion.filechampion4j.FileValidator.checkPluginsConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteTempDir()

Boolean dev.filechampion.filechampion4j.FileValidator.deleteTempDir ( Path  tempFilePath)
private

Helper method to delete the temporary directory

Parameters
tempFilePath(Path) the path to the temporary file
Returns
Boolean (Boolean) true if the temporary directory was deleted successfully, false otherwise

Definition at line 761 of file FileValidator.java.

761 {
762 try (Stream<Path> walk = Files.walk(tempFilePath)) {
763 walk.sorted(Comparator.reverseOrder())
764 .map(Path::toFile)
765 .forEach(File::delete);
766 return true;
767 } catch (Exception e) {
768 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error: Delete temporary directoy failed: ").append(e.getMessage());
770 return false;
771 }
772 }

References dev.filechampion.filechampion4j.FileValidator.logWarn(), and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.executePlugin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doValidations()

ValidationResponse dev.filechampion.filechampion4j.FileValidator.doValidations ( String  originalFilenameClean)
private

Following initial validations and before plugins, this method is used to execute the validations for the file.

Parameters
originalFilenameClean(String) a string containing the cleaned file name
Returns
ValidationResponse (ValidationResponse) a ValidationResponse object containing the results of the validation
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 342 of file FileValidator.java.

342 {
343 try {
345 } catch (Exception e) {
346 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error in doValidations: ").append(e.getMessage());
348 return new ValidationResponse(false, errorResponse, sharedStringBuilder.toString() , originalFilenameClean, null, null);
349 }
350
351 if (sharedStringBuilder.indexOf("Invalid") > -1) {
354 return new ValidationResponse(false, errorResponse, sbresponseAggregationFail.toString(), originalFilenameClean, null, null);
355 } else {
357 }
358
359 // Check for after plugins
361 Map<String, String> checksumMap = new HashMap<>();
362
363 boolean isAddChecksum = extensions.getValidationValue(fileCategory, fileExtension, "add_checksum") != null
364 ? (boolean) extensions.getValidationValue(fileCategory, fileExtension, "add_checksum") : true;
365 if (isAddChecksum) {
366 checksumMap = calculateChecksum(originalFile);
367 }
368
369
370 // Check if file passed all defined validations, return false and reason if not.
371 if (responseMsgCountFail > 0) {
373 return new ValidationResponse(false, errorResponse, sbresponseAggregationFail.toString(), originalFilenameClean, originalFile, checksumMap);
374 }
375
376 // Check if the file name should be encoded
377 String encodedFileName = "";
378 boolean isNameEncoding = extensions.getValidationValue(fileCategory, fileExtension, "name_encoding") != null ? (boolean) extensions.getValidationValue(fileCategory, fileExtension, "name_encoding") : false;
379 if (isNameEncoding) {
380 sharedStringBuilder.replace(0, sharedStringBuilder.length(), Base64.getEncoder().encodeToString(originalFilenameClean.getBytes(StandardCharsets.UTF_8))).append(".").append(fileExtension);
381 encodedFileName = sharedStringBuilder.toString();
382 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
383 .append("File name: ")
384 .append(originalFilenameClean)
385 .append(" has been successfully encoded to: ")
386 .append(encodedFileName);
388 }
389 String targetFileName = encodedFileName.isEmpty() ? originalFilenameClean : encodedFileName;
390
391 // Check if the file should be saved to output directory
392 String savedFilePath;
393 if (outDir != null && !isBlank(outDir.toString())) {
394 savedFilePath = saveFileToOutputDir(fileCategory, fileExtension, outDir, targetFileName, originalFile);
395 if (savedFilePath.contains("Error:")) {
396 // Return valid file response if file failed to save to output directory
397 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
398 .append("File is valid but failed to save to output directory: ")
399 .append(savedFilePath);
401 return new ValidationResponse(true, "File is valid but failed to save to output directory", sbresponseAggregationSuccess.toString(), originalFilenameClean, originalFile, checksumMap);
402 }
403 // Return valid file response if file was saved to output directory
404 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
405 .append("File is valid and was saved to output directory: ")
406 .append(savedFilePath);
408 return new ValidationResponse(true, "File is valid and was saved to output directory", sbresponseAggregationSuccess.toString(), originalFilenameClean, originalFile, checksumMap);
409 }
410
411 // Return valid response if file passed all validations but is not meant to be saved to disk
412 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
413 .append("File is valid: ")
414 .append(originalFilenameClean);
416 return new ValidationResponse(true, "File is valid", sbresponseAggregationSuccess.toString(), originalFilenameClean, originalFile, checksumMap);
417 }
Object getValidationValue(String category, String extension, String validationKey)
String saveFileToOutputDir(String fileCategory, String fileExtension, Path outDir, String fileName, byte[] fileBytes)
Map< String, String > calculateChecksum(byte[] fileBytes)
StringBuilder getValidationResults(String fileCategory, String fileName, byte[] originalFile, String mimeString)

References dev.filechampion.filechampion4j.FileValidator.calculateChecksum(), dev.filechampion.filechampion4j.FileValidator.errorResponse, dev.filechampion.filechampion4j.FileValidator.executeAfterPlugins(), dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.ValidationsHelper.getValidationResults(), dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.isBlank(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.logInfo(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.mimeString, dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.outDir, dev.filechampion.filechampion4j.FileValidator.responseMsgCountFail, dev.filechampion.filechampion4j.FileValidator.responseMsgCountSuccess, dev.filechampion.filechampion4j.FileValidator.saveFileToOutputDir(), dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationFail, dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationSuccess, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validationsHelper.

Referenced by dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeAfterPlugins()

void dev.filechampion.filechampion4j.FileValidator.executeAfterPlugins ( )
private

Definition at line 599 of file FileValidator.java.

599 {
600 // Check for after plugins
601 if (extensions.getValidationValue(fileCategory, fileExtension, "extension_plugins") != null) {
602 String executionResults = executeAfterPluginsProcess(fileCategory, fileExtension);
603 if (executionResults.contains(". Failed for step:")) {
604 sbresponseAggregationFail.append(System.lineSeparator() + ++responseMsgCountFail + ". ")
605 .append("Error in executeAfterPlugins: ")
606 .append(executionResults)
607 .append(commonLogString);
609 } else if (executionResults.contains(". Error for step:")) {
610 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
611 .append("executeAfterPlugins passed with error: ")
612 .append(executionResults);
614 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error in executeAfterPlugins: ").append(executionResults);
616 } else {
617 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
618 .append("executeAfterPlugins executed successfully: ")
619 .append(executionResults);
621 }
622 } else {
623 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
624 .append("No after plugins to execute");
626 }
627 }
String executeAfterPluginsProcess(String fileCategory, String fileExtension)

References dev.filechampion.filechampion4j.FileValidator.commonLogString, dev.filechampion.filechampion4j.FileValidator.executeAfterPluginsProcess(), dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.responseMsgCountFail, dev.filechampion.filechampion4j.FileValidator.responseMsgCountSuccess, dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationFail, dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationSuccess, and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.doValidations().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeAfterPluginsProcess()

String dev.filechampion.filechampion4j.FileValidator.executeAfterPluginsProcess ( String  fileCategory,
String  fileExtension 
)
private

Execute and check results of plugins configured to run after the validations

Parameters
fileCategory(String) the file category of the file being validated
fileExtension(String) the file extension of the file being validated
Returns
String (String) a string containing the results of the plugin execution

Definition at line 635 of file FileValidator.java.

635 {
636 String responseAggregation = "";
637 char responseMsgCount = 'a';
638 StringBuilder sbResponseAggregation = new StringBuilder(responseAggregation);
639
640 ArrayList plugins = (ArrayList) extensions.getValidationValue(fileCategory, fileExtension, "extension_plugins");
641 for (int i = 0; i < plugins.size(); i++) {
642 String extensionPlugin = (String) plugins.get(i);
643 for (String step : stepConfigsAfter.keySet()) {
644 if (step.equals(extensionPlugin)) {
645 String stepResults = executePlugin(extensionPlugin, stepConfigsAfter, fileExtension);
647 .append(stepConfigsAfter.get(extensionPlugin).getName()).append(" Success, Results: Error");
648 String sharedString = ", Results: ";
649 if (stepResults.startsWith(sharedStringBuilder.toString()) || stepResults.startsWith("Error ")) {
650 if (stepConfigsAfter.get(extensionPlugin).getOnFail().equals("fail")) {
651 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
652 .append("Failed for step: ")
653 .append(stepConfigsAfter.get(extensionPlugin).getName())
654 .append(sharedString)
655 .append(stepResults);
656 logFine(sbResponseAggregation);
657 return sbResponseAggregation.toString();
658 }
659 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
660 .append("Error for step: ")
661 .append(stepConfigsAfter.get(extensionPlugin).getName())
662 .append(sharedString + stepResults);
663 logFine(sbResponseAggregation);
664 ++responseMsgCount;
665 responseAggregation = sbResponseAggregation.toString();
666 } else {
667 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
668 .append("Success for step: ")
669 .append(stepConfigsAfter.get(extensionPlugin).getName());
670 logFine(sbResponseAggregation);
671 ++responseMsgCount;
672 }
673 }
674 }
675 }
676 return "executeAfterPlugins completed: " + sbResponseAggregation.toString();
677 }
String executePlugin(String extensionPlugin, Map< String, StepConfig > stepConfigs, String fileExtension)

References dev.filechampion.filechampion4j.FileValidator.executePlugin(), dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.sharedStepMessage, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.stepConfigsAfter.

Referenced by dev.filechampion.filechampion4j.FileValidator.executeAfterPlugins().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeBeforePlugins()

String dev.filechampion.filechampion4j.FileValidator.executeBeforePlugins ( String  fileCategory,
String  fileExtension 
)
private

Execute and check results of plugins configured to run before the validations

Parameters
fileCategory(String) the file category of the file being validated
fileExtension(String) the file extension of the file being validated
Returns
String (String) a string containing the results of the plugin execution

Definition at line 554 of file FileValidator.java.

554 {
555 String responseAggregation = "";
556 char responseMsgCount = 'a';
557 StringBuilder sbResponseAggregation = new StringBuilder(responseAggregation);
558
559
560 ArrayList plugins = (ArrayList) extensions.getValidationValue(fileCategory, fileExtension, "extension_plugins");
561 for (int i = 0; i < plugins.size(); i++) {
562 String extensionPlugin = (String) plugins.get(i);
563 for (String step : stepConfigsBefore.keySet()) {
564 if (step.equals(extensionPlugin)) {
565 String stepResults = executePlugin(extensionPlugin, stepConfigsBefore, fileExtension);
567 .append(stepConfigsBefore.get(extensionPlugin).getName()).append(" Success, Results: Error");
568 String sharedString = ", Results: ";
569 if (stepResults.startsWith(sharedStringBuilder.toString()) || stepResults.startsWith("Error ")) {
570 if (stepConfigsBefore.get(extensionPlugin).getOnFail().equals("fail")) {
571 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
572 .append("Failed for step: ")
573 .append(stepConfigsBefore.get(extensionPlugin).getName())
574 .append(sharedString)
575 .append(stepResults);
576 logFine(sbResponseAggregation);
577 return sbResponseAggregation.toString();
578 }
579 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
580 .append("Error for step: ")
581 .append(stepConfigsBefore.get(extensionPlugin).getName())
582 .append(sharedString + stepResults);
583 logFine(sbResponseAggregation);
584 ++responseMsgCount;
585 responseAggregation = sbResponseAggregation.toString();
586 } else {
587 sbResponseAggregation.append(System.lineSeparator()).append("\t") .append(responseMsgCount + ". ")
588 .append("Success for step: ")
589 .append(stepConfigsBefore.get(extensionPlugin).getName());
590 logFine(sbResponseAggregation);
591 ++responseMsgCount;
592 }
593 }
594 }
595 }
596 return "executeBeforePlugins completed: " + sbResponseAggregation.toString();
597 }

References dev.filechampion.filechampion4j.FileValidator.executePlugin(), dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.sharedStepMessage, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.stepConfigsBefore.

Referenced by dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executePlugin()

String dev.filechampion.filechampion4j.FileValidator.executePlugin ( String  extensionPlugin,
Map< String, StepConfig stepConfigs,
String  fileExtension 
)
private

Execute a single plugin step

Parameters
extensionPlugin(String) a string containing the name of the plugin step to execute
stepConfigs(Map<String, StepConfig>) a map containing the configuration for the step to execute
fileExtension(String) the file extension of the file being validated
Returns
String (String) a string containing the results of the plugin execution

Definition at line 686 of file FileValidator.java.

686 {
687 Map<String, String> stepResultsMap = new HashMap<>();
688 String extensionPluginName = stepConfigs.get(extensionPlugin).getName();
689 sharedStringBuilder.replace(0, sharedStringBuilder.length(), sharedStepMessage).append(extensionPluginName);
691
692 if (stepConfigs.get(extensionPlugin).getType().equals("cli")) {
693 Map<String, Map<String, String>> stepResults = stepConfigs.get(extensionPlugin)
694 .getCliPluginHelper()
695 .execute(fileExtension, originalFile);
696 stepResultsMap.putAll(stepResults.get(stepResults.keySet().toArray()[0]));
697
698 if (!stepResultsMap.isEmpty() && stepResults.containsKey("Success")) {
699 String newFilePath = stepResultsMap.get(extensionPluginName.substring(extensionPluginName.lastIndexOf(".")+1,
700 extensionPluginName.length()) + ".filePath");
701 String newB64Content = stepResultsMap.get(extensionPluginName.substring(extensionPluginName.lastIndexOf(".")+1,
702 extensionPluginName.length()) + ".fileContent");
703
704 if (!isBlank(newFilePath)) {
705 try {
706 Path newFile = new File(newFilePath).toPath();
707 originalFile = Files.readAllBytes(newFile);
708 deleteTempDir(newFile.getParent().toAbsolutePath());
709 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Successfully read plugin expected file: ").append(newFilePath);
711 } catch (IOException e) {
712 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error reading plugin expected file: ").append(e.getMessage());
714 return sharedStringBuilder.toString();
715 }
716 }
717 if (!isBlank(newB64Content)) {
718 try {
719 originalFile = Base64.getDecoder().decode(newB64Content);
720 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Successfully decoded plugin expected file");
722 } catch (Exception e) {
723 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error decoding plugin expected file: ").append(e.getMessage());
725 return sharedStringBuilder.toString();
726 }
727 }
728 }
729 for(Map.Entry<String, String> entry : stepResultsMap.entrySet()) {
730 String errorMsg = entry.getValue();
731 String errorDetails = entry.getKey();
732 sharedStringBuilder.replace(0, sharedStringBuilder.length(), sharedStepMessage).append(extensionPluginName)
733 .append(" Success, Results: ")
734 .append(errorDetails)
735 .append("\"")
736 .append(errorMsg);
738 }
739 return sharedStringBuilder.toString();
740 } else if (stepConfigs.get(extensionPlugin).getType().equals("http")) {
741 // TODO: Implement http plugin type
742 }
743 return sharedStringBuilder.toString();
744 }

References dev.filechampion.filechampion4j.FileValidator.deleteTempDir(), dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.FileValidator.isBlank(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.sharedStepMessage, and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.executeAfterPluginsProcess(), and dev.filechampion.filechampion4j.FileValidator.executeBeforePlugins().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFileExtension()

String dev.filechampion.filechampion4j.FileValidator.getFileExtension ( String  fileName)
private

Parse the file extension from the file name

Parameters
fileName(String) the name of the file being validated
Returns
String (String) the file extension of the file being validated

Definition at line 779 of file FileValidator.java.

779 {
780 int dotIndex = fileName.lastIndexOf('.');
781 if (dotIndex == -1 || dotIndex == fileName.length() - 1) {
782 return "";
783 }
784 return fileName.substring(dotIndex + 1);
785 }

References dev.filechampion.filechampion4j.FileValidator.fileName.

Referenced by dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the caller graph for this function:

◆ isBlank()

boolean dev.filechampion.filechampion4j.FileValidator.isBlank ( String  str)
private

isBlank wrapper method for support of Java 8

Parameters
str(String) the string to check if empty or null
Returns
boolean (boolean) true if the string is empty or null, false otherwise

Definition at line 752 of file FileValidator.java.

752 {
753 return str == null || str.trim().isEmpty();
754 }

Referenced by dev.filechampion.filechampion4j.FileValidator.checkMethodInputs(), dev.filechampion.filechampion4j.FileValidator.doValidations(), and dev.filechampion.filechampion4j.FileValidator.executePlugin().

Here is the caller graph for this function:

◆ loadPlugins()

void dev.filechampion.filechampion4j.FileValidator.loadPlugins ( )
private

Method to load the plugins objects into maps

Definition at line 467 of file FileValidator.java.

467 {
468 for (PluginConfig pluginConfig : pluginsHelper.getPluginConfigs().values()) {
469 for (String step : pluginConfig.getStepConfigs().keySet()) {
470 StepConfig stepConfig = pluginConfig.getStepConfigs().get(step);
471 stepConfigsBefore.put(stepConfig.isRunBefore()? step : "", stepConfig.isRunBefore()? stepConfig : null);
472 stepConfigsAfter.put(stepConfig.isRunAfter()? step : "", stepConfig.isRunAfter()? stepConfig : null);
473 }
474 }
475 }
Map< String, PluginConfig > getPluginConfigs()

References dev.filechampion.filechampion4j.PluginsHelper.getPluginConfigs(), dev.filechampion.filechampion4j.PluginsHelper.StepConfig.isRunAfter(), dev.filechampion.filechampion4j.PluginsHelper.StepConfig.isRunBefore(), dev.filechampion.filechampion4j.FileValidator.pluginsHelper, dev.filechampion.filechampion4j.FileValidator.stepConfigsAfter, and dev.filechampion.filechampion4j.FileValidator.stepConfigsBefore.

Referenced by dev.filechampion.filechampion4j.FileValidator.FileValidator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logFine()

void dev.filechampion.filechampion4j.FileValidator.logFine ( StringBuilder  message)
private

◆ logInfo()

void dev.filechampion.filechampion4j.FileValidator.logInfo ( StringBuilder  message)
private

LOGGER.info wrapper

Parameters
message(String) - message to log

Definition at line 428 of file FileValidator.java.

428 {
429 if (LOGGER.isLoggable(Level.INFO)) {
430 LOGGER.info(message.toString());
431 }
432 }

References dev.filechampion.filechampion4j.FileValidator.LOGGER.

Referenced by dev.filechampion.filechampion4j.FileValidator.doValidations(), and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the caller graph for this function:

◆ logSevere()

void dev.filechampion.filechampion4j.FileValidator.logSevere ( StringBuilder  message)
private

LOGGER.severe wrapper

Parameters
message(String) - message to log

Definition at line 448 of file FileValidator.java.

448 {
449 if (LOGGER.isLoggable(Level.SEVERE)) {
450 LOGGER.severe(message.toString());
451 }
452 }

References dev.filechampion.filechampion4j.FileValidator.LOGGER.

Referenced by dev.filechampion.filechampion4j.FileValidator.saveFileToOutputDir(), and dev.filechampion.filechampion4j.FileValidator.setFileAttributes().

Here is the caller graph for this function:

◆ logWarn()

◆ saveFileToOutputDir()

String dev.filechampion.filechampion4j.FileValidator.saveFileToOutputDir ( String  fileCategory,
String  fileExtension,
Path  outDir,
String  fileName,
byte[]  fileBytes 
)
private

Helper method to save the file defined file attributes to the output directory and return the path to the saved file

Parameters
fileCategory(String) the file category of the file being validated
fileExtension(String) the file extension of the file being validated
outDir(Path) the path to the output directory
fileName(String) the name of the file being validated
fileBytes(byte[]) the file bytes of the file being validated
Returns
String (String) the path to the saved file

Definition at line 816 of file FileValidator.java.

816 {
817 Path targetFilePath = Paths.get(outDir.toString(), fileName);
818 try {
819 Files.write(targetFilePath, fileBytes, StandardOpenOption.CREATE);
820 } catch (IOException e) {
821 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error: Saving file to directory failed: ").append(e.getMessage());
823 return sharedStringBuilder.toString();
824 }
825
826 boolean changeOwnership = extensions.getValidationValue(fileCategory, fileExtension, "change_ownership") != null
827 ? (boolean) extensions.getValidationValue(fileCategory, fileExtension, "change_ownership") : false;
828 if (changeOwnership) {
829 String changeOwnershipUser = (String) extensions.getValidationValue(fileCategory, fileExtension, "change_ownership_user");
830 String changePermissionsMode = (String) extensions.getValidationValue(fileCategory, fileExtension, "change_ownership_mode");
831 String changeOwnershipStatus = setFileAttributes(targetFilePath, changeOwnershipUser, changePermissionsMode);
832 if (changeOwnershipStatus.contains("Error:")) {
833 return changeOwnershipStatus;
834 }
835 }
836 return targetFilePath.toAbsolutePath().toString();
837 }
String setFileAttributes(Path targetFilePath, String changeOwnershipUser, String changePermissionsMode)

References dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.logSevere(), dev.filechampion.filechampion4j.FileValidator.outDir, dev.filechampion.filechampion4j.FileValidator.setFileAttributes(), and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.doValidations().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setFileAttributes()

String dev.filechampion.filechampion4j.FileValidator.setFileAttributes ( Path  targetFilePath,
String  changeOwnershipUser,
String  changePermissionsMode 
)
private

Helper method to set the file attributes

Parameters
targetFilePath(Path) the path to the file
changeOwnershipUser(String) the user to change the ownership to
changePermissionsMode(String) the permissions to change the file to
Returns
String (String) the status of the file attribute change

Definition at line 846 of file FileValidator.java.

846 {
847 FileAclHelper fileAclHelper = new FileAclHelper(targetFilePath, changeOwnershipUser, changePermissionsMode);
848 String newFileAttributesStatus = fileAclHelper.changeFileAcl();
849 if (newFileAttributesStatus.contains("Error:")) {
850 try {
851 Files.deleteIfExists(targetFilePath);
852 } catch (IOException e) {
853 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error: Failed to delete file from permissions change operation: ").append(e.getMessage());
855 }
856 sharedStringBuilder.replace(0, sharedStringBuilder.length(), newFileAttributesStatus);
858 return newFileAttributesStatus;
859 } else {
860 return "Success: File attributes changed successfully";
861 }
862 }

References dev.filechampion.filechampion4j.FileAclHelper.changeFileAcl(), dev.filechampion.filechampion4j.FileValidator.logSevere(), and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.saveFileToOutputDir().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validateFile() [1/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
byte[]  originalFile,
String  fileName 
)

This method is used to validate the file as file bytes, and return the results.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
originalFile(byte[]) - The file to be validated as a byte array.
fileName(String) - The original name of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 256 of file FileValidator.java.

256 {
257 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating file bytes of: ")
258 .append(fileName);
260 this.fileCategory = fileCategory;
261 this.fileName = fileName;
262 this.originalFile = originalFile;
263 return validateFileMain();
264 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [2/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
byte[]  originalFile,
String  fileName,
Path  outputDir 
)

This method is used to validate the file as file bytes, save the file to the output directory if passed validations, and return the results.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
originalFile(byte[]) - The file to be validated as a byte array.
fileName(String) - The original name of the file to be validated.
outputDir(Path) - The directory to save the file to if it passes validations.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 195 of file FileValidator.java.

195 {
196 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating and Storing file bytes of: ")
197 .append(fileName);
199 this.fileCategory = fileCategory;
200 this.fileName = fileName;
201 this.originalFile = originalFile;
202 this.outDir = outputDir;
203 return validateFileMain();
204 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [3/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
byte[]  originalFile,
String  fileName,
Path  outputDir,
String  mimeString 
)

This method is used to validate the file bytes with existing mime type, typically present in web related file uploads, and save the file to the output directory if passed validations.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
originalFile(byte[]) - The file to be validated as a byte array.
fileName(String) - The original name of the file to be validated.
outputDir(Path) - The directory to save the file to if it passes validations.
mimeString(String) - The mime type of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 125 of file FileValidator.java.

125 {
126 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating with request mime ")
127 .append(mimeString)
128 .append(" and storage for file bytes of: ")
129 .append(fileName);
131 this.fileCategory = fileCategory;
132 this.fileName = fileName;
133 this.originalFile = originalFile;
134 this.mimeString = mimeString;
135 this.outDir = outputDir;
136 return validateFileMain();
137 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.mimeString, dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [4/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
byte[]  originalFile,
String  fileName,
String  mimeString 
)

This method is used to validate the file as file bytes, with existing mime type, typically present in web related file uploads.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
originalFile(byte[]) - The file to be validated as a byte array.
fileName(String) - The original name of the file to be validated.
mimeString(String) - The mime type of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 173 of file FileValidator.java.

173 {
174 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating with request ")
175 .append(mimeString)
176 .append(" mime for file bytes of: ")
177 .append(fileName);
179 this.fileCategory = fileCategory;
180 this.fileName = fileName;
181 this.originalFile = originalFile;
182 this.mimeString = mimeString;
183 return validateFileMain();
184 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.mimeString, dev.filechampion.filechampion4j.FileValidator.originalFile, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [5/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
Path  filePath,
String  fileName 
)

This method is used to validate the file in target path, and return the results.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
filePath(Path) - The target file path to be validated as a String.
fileName(String) - The original name of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 274 of file FileValidator.java.

274 {
275 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating file path of: ")
276 .append(fileName);
278 this.fileCategory = fileCategory;
279 this.fileName = fileName;
280 this.filePath = filePath;
281 return validateFileMain();
282 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.filePath, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [6/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
Path  filePath,
String  fileName,
Path  outputDir 
)

This method is used to validate the file in target path, save the file to the output directory if passed validations, and return the results.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
filePath(Path) - The target file path to be validated as a String.
fileName(String) - The original name of the file to be validated.
outputDir(Path) - The directory to save the file to if it passes validations.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 237 of file FileValidator.java.

237 {
238 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating and Storing file path of: ")
239 .append(fileName);
241 this.fileCategory = fileCategory;
242 this.fileName = fileName;
243 this.outDir = outputDir;
244 this.filePath = filePath;
245 return validateFileMain();
246 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.filePath, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [7/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
Path  filePath,
String  fileName,
Path  outputDir,
String  mimeString 
)

This method is used to validate the file path with existing mime type, typically present in web related file uploads, and save the file to the output directory if passed validations.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
filePath(Path) - The target file path to be validated as a String.
fileName(String) - The original name of the file to be validated.
outputDir(Path) - The directory to save the file to if it passes validations.
mimeString(String) - The mime type of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 150 of file FileValidator.java.

150 {
151 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating with request mime ")
152 .append(mimeString)
153 .append(" and storage for file path of: ")
154 .append(fileName);
156 this.fileCategory = fileCategory;
157 this.fileName = fileName;
158 this.mimeString = mimeString;
159 this.outDir = outputDir;
160 this.filePath = filePath;
161 return validateFileMain();
162 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.filePath, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.mimeString, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFile() [8/8]

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFile ( String  fileCategory,
Path  filePath,
String  fileName,
String  mimeString 
)

This method is used to validate the file path with existing mime type, typically present in web related file uploads.

Parameters
fileCategory(String) - The category of the file to be validated. This is used to determine which validations to run.
filePath(Path) - The target file path to be validated as a String.
fileName(String) - The original name of the file to be validated.
mimeString(String) - The mime type of the file to be validated.
Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 215 of file FileValidator.java.

215 {
216 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating with request mime ")
217 .append(mimeString)
218 .append(" for file path of: ")
219 .append(fileName);
221 this.fileCategory = fileCategory;
222 this.fileName = fileName;
223 this.mimeString = mimeString;
224 this.filePath = filePath;
225 return validateFileMain();
226 }

References dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.filePath, dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.mimeString, dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder, and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

Here is the call graph for this function:

◆ validateFileMain()

ValidationResponse dev.filechampion.filechampion4j.FileValidator.validateFileMain ( )
private

This method is the internal entry point for the file validation process.

Returns
(ValidationResponse) - The results of the validations.
Exceptions
IllegalArgumentException- If any of the required inputs are null or empty.

Definition at line 289 of file FileValidator.java.

289 {
290 commonLogString = String.format(" for file extension: %s", fileExtension);
292 sbresponseAggregationFail = new StringBuilder("");
294 sbresponseAggregationSuccess = new StringBuilder("");
295
296 // Check that the input parameters are not null or empty
298
299 // Initialize variables
301 String originalFilenameClean = fileName.replaceAll("[^\\p{IsAlphabetic}\\p{IsDigit}.]", "_");
302
303 // Log the file type category being validated
304 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Validating ").append(originalFilenameClean).append(", as file type: ").append(fileCategory);
306
307 // Check for before plugins
308 if (extensions.getValidationValue(fileCategory, fileExtension, "extension_plugins") != null) {
309 String executionResults = executeBeforePlugins(fileCategory, fileExtension);
310 if (executionResults.contains(". Failed for step:")) {
311 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "executeBeforePlugins failed for file: ").append(originalFilenameClean).append(", Results: ").append(executionResults);
313 return new ValidationResponse(false, errorResponse, sharedStringBuilder.toString() , originalFilenameClean, null, null);
314 } else if (executionResults.contains(". Error for step:")) {
315 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "Error executing Plugins defined to run before validations for file: ").append(originalFilenameClean).append(", Results: ").append(executionResults);
316 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
317 .append("executeBeforePlugins passed with error: ")
318 .append(executionResults);
321 } else {
322 sbresponseAggregationSuccess.append(System.lineSeparator() + ++responseMsgCountSuccess + ". ")
323 .append("executeBeforePlugins executed successfully: ")
324 .append(executionResults);
326 sharedStringBuilder.replace(0, sharedStringBuilder.length(), executionResults);
328 }
329 } else {
330 sharedStringBuilder.replace(0, sharedStringBuilder.length(), "No before plugins defined for file: ").append(originalFilenameClean);
332 }
333 return (doValidations(originalFilenameClean));
334 }
ValidationResponse doValidations(String originalFilenameClean)
String executeBeforePlugins(String fileCategory, String fileExtension)

References dev.filechampion.filechampion4j.FileValidator.checkMethodInputs(), dev.filechampion.filechampion4j.FileValidator.commonLogString, dev.filechampion.filechampion4j.FileValidator.doValidations(), dev.filechampion.filechampion4j.FileValidator.errorResponse, dev.filechampion.filechampion4j.FileValidator.executeBeforePlugins(), dev.filechampion.filechampion4j.FileValidator.extensions, dev.filechampion.filechampion4j.FileValidator.fileCategory, dev.filechampion.filechampion4j.FileValidator.fileExtension, dev.filechampion.filechampion4j.FileValidator.fileName, dev.filechampion.filechampion4j.FileValidator.getFileExtension(), dev.filechampion.filechampion4j.Extensions.getValidationValue(), dev.filechampion.filechampion4j.FileValidator.logFine(), dev.filechampion.filechampion4j.FileValidator.logInfo(), dev.filechampion.filechampion4j.FileValidator.logWarn(), dev.filechampion.filechampion4j.FileValidator.responseMsgCountFail, dev.filechampion.filechampion4j.FileValidator.responseMsgCountSuccess, dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationFail, dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationSuccess, and dev.filechampion.filechampion4j.FileValidator.sharedStringBuilder.

Referenced by dev.filechampion.filechampion4j.FileValidator.validateFile().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ checksumAlgorithms

List<String> dev.filechampion.filechampion4j.FileValidator.checksumAlgorithms = new ArrayList<>()
private

◆ commonFileError

String dev.filechampion.filechampion4j.FileValidator.commonFileError = "Error reading file: "
private

◆ commonLogString

String dev.filechampion.filechampion4j.FileValidator.commonLogString
private

◆ configJsonObject

JSONObject dev.filechampion.filechampion4j.FileValidator.configJsonObject
private

◆ errorResponse

String dev.filechampion.filechampion4j.FileValidator.errorResponse = "File is not valid."
private

◆ extensions

◆ fileCategory

◆ fileExtension

◆ fileName

◆ filePath

Path dev.filechampion.filechampion4j.FileValidator.filePath
private

◆ LOG

final Logger dev.filechampion.filechampion4j.FileValidator.LOG = LogManager.getLogManager().getLogger("")
staticprivate

Initialize logging configuration from logging.properties file in resources folder

Definition at line 33 of file FileValidator.java.

◆ LOGGER

final Logger dev.filechampion.filechampion4j.FileValidator.LOGGER = Logger.getLogger(FileValidator.class.getName())
staticprivate

◆ mimeString

String dev.filechampion.filechampion4j.FileValidator.mimeString
private

◆ originalFile

◆ outDir

◆ pluginsHelper

PluginsHelper dev.filechampion.filechampion4j.FileValidator.pluginsHelper
private

◆ responseMsgCountFail

◆ responseMsgCountSuccess

◆ sbresponseAggregationFail

StringBuilder dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationFail
private

◆ sbresponseAggregationSuccess

StringBuilder dev.filechampion.filechampion4j.FileValidator.sbresponseAggregationSuccess
private

◆ sharedStepMessage

◆ sharedStringBuilder

◆ stepConfigsAfter

Map<String, StepConfig> dev.filechampion.filechampion4j.FileValidator.stepConfigsAfter = new HashMap<>()
private

◆ stepConfigsBefore

Map<String, StepConfig> dev.filechampion.filechampion4j.FileValidator.stepConfigsBefore = new HashMap<>()
private

◆ supportedAlgorithms

final List<String> dev.filechampion.filechampion4j.FileValidator.supportedAlgorithms = Arrays.asList("MD5", "SHA-1", "SHA-256", "SHA-512")
staticprivate

◆ validationsHelper

ValidationsHelper dev.filechampion.filechampion4j.FileValidator.validationsHelper
private

The documentation for this class was generated from the following file: