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

Public Member Functions

 Extensions (JSONObject jsonObject)
 
Object getValidationValue (String category, String extension, String validationKey)
 

Private Member Functions

void logInfo (String message)
 
void logWarn (String message)
 
void logFine (String message)
 
void mapConfiguredExtensions ()
 
void mapExtensionValidations ()
 
void setString (String validation, Object value)
 
void setBoolean (String validation, Object value)
 
void setArrayList (String validation, Object value)
 
void checkInputs (String category, String extension, String validationKey)
 

Private Attributes

StringBuilder sbLogMessage = new StringBuilder()
 
Map< String, Map< String, Object > > categoriesMap
 
Map< String, Map< String, Object > > extensionsMap
 
Map< String, Object > validationsMap
 
Map< String, Object > validationCache = new HashMap<>()
 
String sharedMessage1 = "Unsupported value type: "
 
String sharedMessage2 = " for key: "
 
List< String > allowedKeyValues
 
List< String > stringKeyValues
 
List< String > boolKeyValues = Arrays.asList("change_ownership", "name_encoding", "add_checksum", "fail_fast")
 

Static Private Attributes

static final Logger LOGGER = Logger.getLogger(Extensions.class.getName())
 

Detailed Description

This class is used to load the Validations json objectand provide the validation values for a given extension.

Definition at line 16 of file Extensions.java.

Constructor & Destructor Documentation

◆ Extensions()

dev.filechampion.filechampion4j.Extensions.Extensions ( JSONObject  jsonObject)

Constructor for Extensions class

Parameters
jsonObject(JSONObject) - The json object containing the validations
Exceptions
IllegalArgumentExceptionif any of the json objects is invalid

Definition at line 55 of file Extensions.java.

55 {
56
57 categoriesMap = new HashMap<>();
58 extensionsMap = new HashMap<>();
59 validationsMap = new HashMap<>();
60
61 if (jsonObject == null) {
62 throw new IllegalArgumentException("jsonObject cannot be null");
63 }
64
65 for (String category : jsonObject.keySet()) {
66 categoriesMap.put(category, jsonObject.getJSONObject(category).toMap());
67 sbLogMessage.replace(0, sbLogMessage.length(), category).append(" ")
68 .append(jsonObject.getJSONObject(category).toString());
69 logFine(sbLogMessage.toString());
70 }
71 if( categoriesMap.isEmpty()) {
72 throw new IllegalArgumentException("Validations must contain categories objects");
73 }
75 }
Map< String, Map< String, Object > > extensionsMap
Definition: Extensions.java:36
Map< String, Map< String, Object > > categoriesMap
Definition: Extensions.java:35

References dev.filechampion.filechampion4j.Extensions.categoriesMap, dev.filechampion.filechampion4j.Extensions.extensionsMap, dev.filechampion.filechampion4j.Extensions.logFine(), dev.filechampion.filechampion4j.Extensions.mapConfiguredExtensions(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, and dev.filechampion.filechampion4j.Extensions.validationsMap.

Here is the call graph for this function:

Member Function Documentation

◆ checkInputs()

void dev.filechampion.filechampion4j.Extensions.checkInputs ( String  category,
String  extension,
String  validationKey 
)
private

Checks input parameters of getValidationValue method

Parameters
category(String) - The category of the extension
extension(String) - The extension to get the validation value for
validationKey(String) - The validation key to get the value for

Definition at line 201 of file Extensions.java.

201 {
202 if (!categoriesMap.containsKey(category)) {
203 sbLogMessage.replace(0, sbLogMessage.length(), "category ").append(category)
204 .append(" not found");
205 logWarn(sbLogMessage.toString());
206 throw new IllegalArgumentException(sbLogMessage.toString());
207 }
208 if (!categoriesMap.get(category).containsKey(extension)) {
209 sbLogMessage.replace(0, sbLogMessage.length(), "extension ").append(extension)
210 .append(" not found");
211 logWarn(sbLogMessage.toString());
212 throw new IllegalArgumentException(sbLogMessage.toString());
213 }
214 if (validationKey == null || validationKey.isEmpty()) {
215 sbLogMessage.replace(0, sbLogMessage.length(), "validationKey cannot be null or empty");
216 logWarn(sbLogMessage.toString());
217 throw new IllegalArgumentException("validationKey cannot be null or empty");
218 }
219 }

References dev.filechampion.filechampion4j.Extensions.categoriesMap, dev.filechampion.filechampion4j.Extensions.logWarn(), and dev.filechampion.filechampion4j.Extensions.sbLogMessage.

Referenced by dev.filechampion.filechampion4j.Extensions.getValidationValue().

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

◆ getValidationValue()

Object dev.filechampion.filechampion4j.Extensions.getValidationValue ( String  category,
String  extension,
String  validationKey 
)

Returns the validation value for a given extension

Parameters
category(String) - The category of the extension
extension(String) - The file extension (without the dot)
validationKey(String) - The validation key
Returns
(Object) - The validation value or null if the validation key is not found/allowed
Exceptions
IllegalArgumentExceptionif any of the parameters are invalid

Definition at line 229 of file Extensions.java.

229 {
230 checkInputs(category, extension, validationKey);
231 String cacheKey = category + "|" + extension + "|" + validationKey;
232 if (validationCache.containsKey(cacheKey)) {
233 sbLogMessage.replace(0, sbLogMessage.length(), "Returning ").append(validationKey)
234 .append(" for ").append(extension).append(" (cached)");
235 logFine(sbLogMessage.toString());
236 return validationCache.get(cacheKey);
237 }
238
239 Object value = ((HashMap<?,?>) categoriesMap.get(category).get(extension)).get(validationKey);
240 if (value != null) {
241 validationCache.put(cacheKey, value);
242 sbLogMessage.replace(0, sbLogMessage.length(), "Returning ").append(validationKey)
243 .append(" for ").append(extension);
244 logFine(sbLogMessage.toString());
245 return value;
246 } else {
247 sbLogMessage.replace(0, sbLogMessage.length(), "Returning null for ")
248 .append(validationKey).append(" for ").append(extension);
249 logFine(sbLogMessage.toString());
250 return null;
251 }
252 }
void checkInputs(String category, String extension, String validationKey)

References dev.filechampion.filechampion4j.Extensions.categoriesMap, dev.filechampion.filechampion4j.Extensions.checkInputs(), dev.filechampion.filechampion4j.Extensions.logFine(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, and dev.filechampion.filechampion4j.Extensions.validationCache.

Referenced by dev.filechampion.filechampion4j.ValidationsHelper.checkFileSize(), dev.filechampion.filechampion4j.ValidationsHelper.checkMimeType(), dev.filechampion.filechampion4j.ValidationsHelper.containsFooterSignatures(), dev.filechampion.filechampion4j.ValidationsHelper.containsHeaderSignatures(), dev.filechampion.filechampion4j.ValidationsHelper.containsMagicBytes(), dev.filechampion.filechampion4j.FileValidator.doValidations(), dev.filechampion.filechampion4j.FileValidator.executeAfterPlugins(), dev.filechampion.filechampion4j.FileValidator.executeAfterPluginsProcess(), dev.filechampion.filechampion4j.FileValidator.executeBeforePlugins(), dev.filechampion.filechampion4j.ValidationsHelper.getValidationResults(), dev.filechampion.filechampion4j.FileValidator.saveFileToOutputDir(), and dev.filechampion.filechampion4j.FileValidator.validateFileMain().

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

◆ logFine()

void dev.filechampion.filechampion4j.Extensions.logFine ( String  message)
private

◆ logInfo()

void dev.filechampion.filechampion4j.Extensions.logInfo ( String  message)
private

Definition at line 18 of file Extensions.java.

18 {
19 if (LOGGER.isLoggable(Level.INFO)) {
20 LOGGER.info(message);
21 }
22 }

References dev.filechampion.filechampion4j.Extensions.LOGGER.

Referenced by dev.filechampion.filechampion4j.Extensions.mapExtensionValidations().

Here is the caller graph for this function:

◆ logWarn()

◆ mapConfiguredExtensions()

void dev.filechampion.filechampion4j.Extensions.mapConfiguredExtensions ( )
private

Load extensions from json object into extensionsMap

Definition at line 80 of file Extensions.java.

80 {
81 for (Map.Entry<String, Object> extensionEntry : categoriesMap.entrySet().iterator().next().getValue().entrySet()) {
82 String extension = extensionEntry.getKey();
83 if (extensionEntry.getValue() instanceof HashMap) {
84 extensionsMap.put(extensionEntry.getKey(), (HashMap) extensionEntry.getValue());
85 sbLogMessage.replace(0, sbLogMessage.length(), extension).append(" ")
86 .append(extensionEntry.getValue().toString());
87 logFine(sbLogMessage.toString());
88 } else {
89 sbLogMessage.replace(0, sbLogMessage.length(), sharedMessage1)
90 .append(extensionEntry.getValue().getClass().getName())
91 .append(sharedMessage2)
92 .append(extension);
93 logWarn(sbLogMessage.toString());
94 throw new IllegalArgumentException(sbLogMessage.toString());
95 }
96 }
97 if (extensionsMap.entrySet().iterator().next().getValue().entrySet().isEmpty()) {
98 sbLogMessage.replace(0, sbLogMessage.length(), "At least one validation must be configured");
99 logWarn(sbLogMessage.toString());
100 throw new IllegalArgumentException(sbLogMessage.toString());
101 }
103 }

References dev.filechampion.filechampion4j.Extensions.categoriesMap, dev.filechampion.filechampion4j.Extensions.extensionsMap, dev.filechampion.filechampion4j.Extensions.logFine(), dev.filechampion.filechampion4j.Extensions.logWarn(), dev.filechampion.filechampion4j.Extensions.mapExtensionValidations(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, dev.filechampion.filechampion4j.Extensions.sharedMessage1, and dev.filechampion.filechampion4j.Extensions.sharedMessage2.

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

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

◆ mapExtensionValidations()

void dev.filechampion.filechampion4j.Extensions.mapExtensionValidations ( )
private

Load extension validations into validationsMap

Definition at line 108 of file Extensions.java.

108 {
109 for (Map.Entry<String, Object> validationEntry : extensionsMap.entrySet().iterator().next().getValue().entrySet() ) {
110 String validation = validationEntry.getKey();
111 Object value = validationEntry.getValue();
112 if (allowedKeyValues.contains(validation)) {
113 if (value instanceof String || value instanceof Integer) {
114 setString(validation, value);
115 } else if (value instanceof Boolean) {
116 setBoolean(validation, value);
117 } else if (value instanceof ArrayList ) {
118 setArrayList(validation, value);
119 } else {
120 sbLogMessage.replace(0, sbLogMessage.length(), sharedMessage1)
121 .append(value.getClass().getName())
122 .append(sharedMessage2)
123 .append(validation);
124 throw new IllegalArgumentException(sbLogMessage.toString());
125 }
126 } else {
127 sbLogMessage.replace(0, sbLogMessage.length(), "Unsupported key: ")
128 .append(validation);
129 logWarn(sbLogMessage.toString());
130 throw new IllegalArgumentException(sbLogMessage.toString());
131 }
132 }
133 sbLogMessage.replace(0, sbLogMessage.length(), "Loaded ").append(categoriesMap.size()).append(" categories, ")
134 .append(extensionsMap.size()).append(" extensions, ")
135 .append(validationsMap.size()).append(" validations");
136 logFine(sbLogMessage.toString());
137 logInfo("Loaded Validations Configurtion");
138
139 }
void setArrayList(String validation, Object value)
void setString(String validation, Object value)
void setBoolean(String validation, Object value)

References dev.filechampion.filechampion4j.Extensions.allowedKeyValues, dev.filechampion.filechampion4j.Extensions.categoriesMap, dev.filechampion.filechampion4j.Extensions.extensionsMap, dev.filechampion.filechampion4j.Extensions.logFine(), dev.filechampion.filechampion4j.Extensions.logInfo(), dev.filechampion.filechampion4j.Extensions.logWarn(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, dev.filechampion.filechampion4j.Extensions.setArrayList(), dev.filechampion.filechampion4j.Extensions.setBoolean(), dev.filechampion.filechampion4j.Extensions.setString(), dev.filechampion.filechampion4j.Extensions.sharedMessage1, dev.filechampion.filechampion4j.Extensions.sharedMessage2, and dev.filechampion.filechampion4j.Extensions.validationsMap.

Referenced by dev.filechampion.filechampion4j.Extensions.mapConfiguredExtensions().

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

◆ setArrayList()

void dev.filechampion.filechampion4j.Extensions.setArrayList ( String  validation,
Object  value 
)
private

Sets the value of ArrayList validation key

Parameters
validation(String) - The validation key
value(Object) - The value of the validation key

Definition at line 182 of file Extensions.java.

182 {
183 if (validation.equals("extension_plugins")) {
184 validationsMap.put(validation, (ArrayList) value);
185 } else {
186 sbLogMessage.replace(0, sbLogMessage.length(), sharedMessage1)
187 .append(value.getClass().getName())
188 .append(sharedMessage2)
189 .append(validation);
190 logWarn(sbLogMessage.toString());
191 throw new IllegalArgumentException(sbLogMessage.toString());
192 }
193 }

References dev.filechampion.filechampion4j.Extensions.logWarn(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, dev.filechampion.filechampion4j.Extensions.sharedMessage1, dev.filechampion.filechampion4j.Extensions.sharedMessage2, and dev.filechampion.filechampion4j.Extensions.validationsMap.

Referenced by dev.filechampion.filechampion4j.Extensions.mapExtensionValidations().

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

◆ setBoolean()

void dev.filechampion.filechampion4j.Extensions.setBoolean ( String  validation,
Object  value 
)
private

Sets the value of Boolean validation key

Parameters
validation(String) - The validation key
value(Object) - The value of the validation key

Definition at line 164 of file Extensions.java.

164 {
165 if (boolKeyValues.contains(validation)) {
166 validationsMap.put(validation, (boolean) value);
167 } else {
168 sbLogMessage.replace(0, sbLogMessage.length(), sharedMessage1)
169 .append(value.getClass().getName())
170 .append(sharedMessage2)
171 .append(validation);
172 logWarn(sbLogMessage.toString());
173 throw new IllegalArgumentException(sbLogMessage.toString());
174 }
175 }

References dev.filechampion.filechampion4j.Extensions.boolKeyValues, dev.filechampion.filechampion4j.Extensions.logWarn(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, dev.filechampion.filechampion4j.Extensions.sharedMessage1, dev.filechampion.filechampion4j.Extensions.sharedMessage2, and dev.filechampion.filechampion4j.Extensions.validationsMap.

Referenced by dev.filechampion.filechampion4j.Extensions.mapExtensionValidations().

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

◆ setString()

void dev.filechampion.filechampion4j.Extensions.setString ( String  validation,
Object  value 
)
private

Sets the value of String validation key

Parameters
validation(String) - The validation key
value(Object) - The value of the validation key

Definition at line 146 of file Extensions.java.

146 {
147 if (stringKeyValues.contains(validation)) {
148 validationsMap.put(validation, value.toString());
149 } else {
150 sbLogMessage.replace(0, sbLogMessage.length(), sharedMessage1)
151 .append(value.getClass().getName())
152 .append(sharedMessage2)
153 .append(validation);
154 logWarn(sbLogMessage.toString());
155 throw new IllegalArgumentException(sbLogMessage.toString());
156 }
157 }

References dev.filechampion.filechampion4j.Extensions.logWarn(), dev.filechampion.filechampion4j.Extensions.sbLogMessage, dev.filechampion.filechampion4j.Extensions.sharedMessage1, dev.filechampion.filechampion4j.Extensions.sharedMessage2, dev.filechampion.filechampion4j.Extensions.stringKeyValues, and dev.filechampion.filechampion4j.Extensions.validationsMap.

Referenced by dev.filechampion.filechampion4j.Extensions.mapExtensionValidations().

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

Member Data Documentation

◆ allowedKeyValues

List<String> dev.filechampion.filechampion4j.Extensions.allowedKeyValues
private
Initial value:
= Arrays.asList("mime_type", "magic_bytes", "header_signatures",
"footer_signatures", "change_ownership", "change_ownership_user", "change_ownership_mode",
"name_encoding", "max_size", "extension_plugins", "add_checksum", "fail_fast")

Definition at line 42 of file Extensions.java.

Referenced by dev.filechampion.filechampion4j.Extensions.mapExtensionValidations().

◆ boolKeyValues

List<String> dev.filechampion.filechampion4j.Extensions.boolKeyValues = Arrays.asList("change_ownership", "name_encoding", "add_checksum", "fail_fast")
private

◆ categoriesMap

◆ extensionsMap

Map<String, Map<String, Object> > dev.filechampion.filechampion4j.Extensions.extensionsMap
private

◆ LOGGER

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

◆ sbLogMessage

◆ sharedMessage1

◆ sharedMessage2

◆ stringKeyValues

List<String> dev.filechampion.filechampion4j.Extensions.stringKeyValues
private
Initial value:
= Arrays.asList("mime_type", "magic_bytes", "header_signatures",
"footer_signatures", "change_ownership_user", "change_ownership_mode",
"max_size", "extension_plugins")

Definition at line 45 of file Extensions.java.

Referenced by dev.filechampion.filechampion4j.Extensions.setString().

◆ validationCache

Map<String, Object> dev.filechampion.filechampion4j.Extensions.validationCache = new HashMap<>()
private

◆ validationsMap


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