Class FileValidatorUtil
java.lang.Object
com.evolveum.midpoint.web.component.input.validator.FileValidatorUtil
Contains utility methods for validating uploaded file content.
Validation compares the content type detected from the file bytes
with both the declared content type and the configured allowed MIME types.
- Author:
- matisovaa
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdetectContentType(byte[] bytes) Detects the MIME type of uploaded content from its bytes.static voidvalidateUploadContent(byte[] bytes, String declaredContentType, List<String> allowedContentTypes) Validates uploaded file content against its declared content type and the configured allowed MIME types.
-
Constructor Details
-
FileValidatorUtil
public FileValidatorUtil()
-
-
Method Details
-
validateUploadContent
public static void validateUploadContent(byte[] bytes, String declaredContentType, List<String> allowedContentTypes) throws FileUploadContentValidationException Validates uploaded file content against its declared content type and the configured allowed MIME types. The actual content type is detected from the file bytes. Configured allowed types support MIME wildcards, for exampleimage/*.- Parameters:
bytes- uploaded file contentdeclaredContentType- content type declared by the upload request; may benullor blankallowedContentTypes- MIME types allowed by the effective upload policy- Throws:
FileUploadContentValidationException- if the content type cannot be recognized, a MIME type is malformed, the detected type is not allowed, or the declared type does not match the detected type
-
detectContentType
Detects the MIME type of uploaded content from its bytes. The method uses Apache Tika detection without relying on a file name. Container detectors identify Office and OpenDocument formats precisely (e.g. docx, xlsx, doc, xls, odt, ods) in addition to magic-byte detection.- Parameters:
bytes- uploaded file content- Returns:
- detected MIME type, or
nullif the content type is not recognized
-