3
3
import java .io .ByteArrayInputStream ;
4
4
import java .io .InputStream ;
5
5
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+
6
9
public interface MimetypeService
7
10
{
11
+ Logger logger = LoggerFactory .getLogger (MimetypeService .class );
12
+
13
+ record ValidationResult (String declaredBaseType , String declaredSubType , String detectedBaseType ,
14
+ String detectedSubType )
15
+ {
16
+ public String declared ()
17
+ {
18
+ return declaredBaseType + "/" + declaredSubType ;
19
+ }
20
+
21
+ public String detected ()
22
+ {
23
+ return detectedBaseType + "/" + detectedSubType ;
24
+ }
25
+
26
+ public boolean mimetypesMatch ()
27
+ {
28
+ return declared ().equals (detected ());
29
+ }
30
+ }
31
+
8
32
/**
9
33
* Detects the mimetype of the provided byte array and validates if the detected mimetype equals the declared
10
- * mimetype. Logs a warning if the full mimetypes do not match, throws a {@link RuntimeException} if the base
11
- * mimetypes do not match.
34
+ * mimetype. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result
35
+ * can be used to drive custom logic based on whether the detected type matches the declared type.
36
+ *
37
+ * @param stream
38
+ * input stream of which the mimetype should be detected
39
+ * @param declared
40
+ * the declared mimetype of the data
41
+ * @return {@link ValidationResult} containing the declared and detected mimetypes.
42
+ */
43
+ ValidationResult validateWithResult (InputStream stream , String declared );
44
+
45
+ /**
46
+ * Detects the mimetype of the provided byte array and validates if the detected mimetype equals the declared
47
+ * mimetype. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result
48
+ * can be used to drive custom logic based on whether the detected type matches the declared type.
12
49
*
13
50
* @param data
14
51
* byte array of which the mimetype should be detected
15
52
* @param declared
16
53
* the declared mimetype of the data
17
- * @throws RuntimeException
18
- * if the detected and the declared base mimetype do not match
54
+ * @return {@link ValidationResult} containing the declared and detected mimetypes.
55
+ */
56
+ default ValidationResult validateWithResult (byte [] data , String declared )
57
+ {
58
+ return validateWithResult (new ByteArrayInputStream (data ), declared );
59
+ }
60
+
61
+ /**
62
+ * Detects the mimetype of the provided byte array and validates if the detected mimetype equals the declared
63
+ * mimetype. Returns <code>true</code> if the full mimetype matches, <code>false</code> otherwise.
64
+ *
65
+ * @param stream
66
+ * input stream of which the mimetype should be detected
67
+ * @param declared
68
+ * the declared mimetype of the data
69
+ * @return <code>true</code> if the full mimetype matches, <code>false</code> otherwise
19
70
*/
20
- default void validate ( byte [] data , String declared )
71
+ default boolean validateWithBoolean ( InputStream stream , String declared )
21
72
{
22
- validate (new ByteArrayInputStream (data ), declared );
73
+ return validateWithResult (stream , declared ).mimetypesMatch ();
74
+ }
75
+
76
+ /**
77
+ * Detects the mimetype of the provided byte array and validates if the detected mimetype equals the declared
78
+ * mimetype. Returns <code>true</code> if the full mimetype matches, <code>false</code> otherwise.
79
+ *
80
+ * @param data
81
+ * byte array of which the mimetype should be detected
82
+ * @param declared
83
+ * the declared mimetype of the data
84
+ * @return <code>true</code> if the full mimetype matches, <code>false</code> otherwise
85
+ */
86
+ default boolean validateWithBoolean (byte [] data , String declared )
87
+ {
88
+ return validateWithResult (new ByteArrayInputStream (data ), declared ).mimetypesMatch ();
23
89
}
24
90
25
91
/**
@@ -34,5 +100,35 @@ default void validate(byte[] data, String declared)
34
100
* @throws RuntimeException
35
101
* if the detected and the declared base mimetype do not match
36
102
*/
37
- void validate (InputStream stream , String declared );
103
+ default void validateWithException (InputStream stream , String declared )
104
+ {
105
+ ValidationResult result = validateWithResult (stream , declared );
106
+
107
+ if (!result .mimetypesMatch ())
108
+ logger .warn ("Declared full mimetype {} does not match detected full mimetype {}" , result .declared (),
109
+ result .detected ());
110
+
111
+ if (!result .declaredBaseType ().equals (result .detectedBaseType ()))
112
+ {
113
+ throw new RuntimeException ("Declared base mimetype of '" + result .declared ()
114
+ + "' does not match detected base mimetype of '" + result .detected () + "'" );
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Detects the mimetype of the provided byte array and validates if the detected mimetype equals the declared
120
+ * mimetype. Logs a warning if the full mimetypes do not match, throws a {@link RuntimeException} if the base
121
+ * mimetypes do not match.
122
+ *
123
+ * @param data
124
+ * byte array of which the mimetype should be detected
125
+ * @param declared
126
+ * the declared mimetype of the data
127
+ * @throws RuntimeException
128
+ * if the detected and the declared base mimetype do not match
129
+ */
130
+ default void validateWithException (byte [] data , String declared )
131
+ {
132
+ validateWithException (new ByteArrayInputStream (data ), declared );
133
+ }
38
134
}
0 commit comments