Skip to content

Commit 035fa68

Browse files
committed
renamed MimetypeService classes to MimeTypeService, add example MIME types to javadoc
1 parent 2cf63e9 commit 035fa68

File tree

11 files changed

+161
-161
lines changed

11 files changed

+161
-161
lines changed

dsf-bpe/dsf-bpe-process-api-v2-impl/src/main/java/dev/dsf/bpe/v2/ProcessPluginApiImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import dev.dsf.bpe.v2.service.EndpointProvider;
1313
import dev.dsf.bpe.v2.service.FhirClientProvider;
1414
import dev.dsf.bpe.v2.service.MailService;
15-
import dev.dsf.bpe.v2.service.MimetypeService;
15+
import dev.dsf.bpe.v2.service.MimeTypeService;
1616
import dev.dsf.bpe.v2.service.OidcClientProvider;
1717
import dev.dsf.bpe.v2.service.OrganizationProvider;
1818
import dev.dsf.bpe.v2.service.QuestionnaireResponseHelper;
@@ -30,7 +30,7 @@ public class ProcessPluginApiImpl implements ProcessPluginApi, InitializingBean
3030
private final OidcClientProvider oidcClientProvider;
3131
private final MailService mailService;
3232

33-
private final MimetypeService mimetypeService;
33+
private final MimeTypeService mimetypeService;
3434
private final ObjectMapper objectMapper;
3535
private final OrganizationProvider organizationProvider;
3636
private final ProcessAuthorizationHelper processAuthorizationHelper;
@@ -40,7 +40,7 @@ public class ProcessPluginApiImpl implements ProcessPluginApi, InitializingBean
4040

4141
public ProcessPluginApiImpl(ProxyConfig proxyConfig, EndpointProvider endpointProvider, FhirContext fhirContext,
4242
DsfClientProvider dsfClientProvider, FhirClientProvider fhirClientProvider,
43-
OidcClientProvider oidcClientProvider, MailService mailService, MimetypeService mimetypeService,
43+
OidcClientProvider oidcClientProvider, MailService mailService, MimeTypeService mimetypeService,
4444
ObjectMapper objectMapper, OrganizationProvider organizationProvider,
4545
ProcessAuthorizationHelper processAuthorizationHelper,
4646
QuestionnaireResponseHelper questionnaireResponseHelper, ReadAccessHelper readAccessHelper,
@@ -123,7 +123,7 @@ public MailService getMailService()
123123
}
124124

125125
@Override
126-
public MimetypeService getMimetypeService()
126+
public MimeTypeService getMimetypeService()
127127
{
128128
return mimetypeService;
129129
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import org.apache.tika.mime.MediaType;
1111
import org.springframework.beans.factory.InitializingBean;
1212

13-
public class MimetypeServiceImpl implements MimetypeService, InitializingBean
13+
public class MimeTypeServiceImpl implements MimeTypeService, InitializingBean
1414
{
1515
private final Detector detector;
1616

17-
public MimetypeServiceImpl(Detector detector)
17+
public MimeTypeServiceImpl(Detector detector)
1818
{
1919
this.detector = detector;
2020
}

dsf-bpe/dsf-bpe-process-api-v2-impl/src/main/java/dev/dsf/bpe/v2/service/detector/CombinedDetectors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private Function<Detector, MediaType> doDetect(InputStream input, Metadata metad
6767
}
6868
catch (IOException exception)
6969
{
70-
throw new RuntimeException("Error while detecting mimetype", exception);
70+
throw new RuntimeException("Error while detecting MIME type", exception);
7171
}
7272
};
7373
}

dsf-bpe/dsf-bpe-process-api-v2-impl/src/main/java/dev/dsf/bpe/v2/service/detector/NdJsonDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MediaType detect(InputStream inputStream, Metadata metadata) throws IOExc
5050
// Using own metadata as provided metadata should not be changed (see method definition in interface)
5151
Metadata internalMetadata = new Metadata();
5252

53-
// Gives only a hint to the possible mimetype, this is needed because application/json
53+
// Gives only a hint to the possible MIME type, this is needed because application/json
5454
// cannot be detected without any hint and would resolve to text/plain.
5555
// As we are checking line by line for JSON to detect if the content is application/x-ndjson, we have to reset
5656
// the hint to application/json.

dsf-bpe/dsf-bpe-process-api-v2-impl/src/main/java/dev/dsf/bpe/v2/spring/ApiServiceConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
import dev.dsf.bpe.v2.service.FhirClientProviderWithEndpointSupport;
4444
import dev.dsf.bpe.v2.service.MailService;
4545
import dev.dsf.bpe.v2.service.MailServiceDelegate;
46-
import dev.dsf.bpe.v2.service.MimetypeService;
47-
import dev.dsf.bpe.v2.service.MimetypeServiceImpl;
46+
import dev.dsf.bpe.v2.service.MimeTypeService;
47+
import dev.dsf.bpe.v2.service.MimeTypeServiceImpl;
4848
import dev.dsf.bpe.v2.service.OidcClientProvider;
4949
import dev.dsf.bpe.v2.service.OidcClientProviderDelegate;
5050
import dev.dsf.bpe.v2.service.OrganizationProvider;
@@ -154,10 +154,10 @@ public MailService mailService()
154154
}
155155

156156
@Bean
157-
public MimetypeService mimetypeService()
157+
public MimeTypeService mimetypeService()
158158
{
159159
Detector detector = CombinedDetectors.withDefaultAndNdJson(NdJsonDetector.DEFAULT_LINES_TO_CHECK);
160-
return new MimetypeServiceImpl(detector);
160+
return new MimeTypeServiceImpl(detector);
161161
}
162162

163163
@Bean

dsf-bpe/dsf-bpe-process-api-v2/src/main/java/dev/dsf/bpe/v2/ProcessPluginApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import dev.dsf.bpe.v2.service.EndpointProvider;
1212
import dev.dsf.bpe.v2.service.FhirClientProvider;
1313
import dev.dsf.bpe.v2.service.MailService;
14-
import dev.dsf.bpe.v2.service.MimetypeService;
14+
import dev.dsf.bpe.v2.service.MimeTypeService;
1515
import dev.dsf.bpe.v2.service.OidcClientProvider;
1616
import dev.dsf.bpe.v2.service.OrganizationProvider;
1717
import dev.dsf.bpe.v2.service.QuestionnaireResponseHelper;
@@ -42,7 +42,7 @@ public interface ProcessPluginApi
4242

4343
MailService getMailService();
4444

45-
MimetypeService getMimetypeService();
45+
MimeTypeService getMimetypeService();
4646

4747
ObjectMapper getObjectMapper();
4848

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package dev.dsf.bpe.v2.service;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.InputStream;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
public interface MimeTypeService
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+
32+
/**
33+
* Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME
34+
* type. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result can be
35+
* 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 MIME type should be detected
39+
* @param declared
40+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
41+
* @return {@link ValidationResult} containing the declared and detected MIME types.
42+
*/
43+
ValidationResult validateWithResult(InputStream stream, String declared);
44+
45+
/**
46+
* Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME
47+
* type. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result can be
48+
* used to drive custom logic based on whether the detected type matches the declared type.
49+
*
50+
* @param data
51+
* byte array of which the MIME type should be detected
52+
* @param declared
53+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
54+
* @return {@link ValidationResult} containing the declared and detected MIME types.
55+
*/
56+
default ValidationResult validateWithResult(byte[] data, String declared)
57+
{
58+
return validateWithResult(new ByteArrayInputStream(data), declared);
59+
}
60+
61+
/**
62+
* Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME
63+
* type. Returns <code>true</code> if the full MIME type matches, <code>false</code> otherwise.
64+
*
65+
* @param stream
66+
* input stream of which the MIME type should be detected
67+
* @param declared
68+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
69+
* @return <code>true</code> if the full MIME type matches, <code>false</code> otherwise
70+
*/
71+
default boolean validateWithBoolean(InputStream stream, String declared)
72+
{
73+
return validateWithResult(stream, declared).mimetypesMatch();
74+
}
75+
76+
/**
77+
* Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME
78+
* type. Returns <code>true</code> if the full MIME type matches, <code>false</code> otherwise.
79+
*
80+
* @param data
81+
* byte array of which the MIME type should be detected
82+
* @param declared
83+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
84+
* @return <code>true</code> if the full MIME type matches, <code>false</code> otherwise
85+
*/
86+
default boolean validateWithBoolean(byte[] data, String declared)
87+
{
88+
return validateWithResult(new ByteArrayInputStream(data), declared).mimetypesMatch();
89+
}
90+
91+
/**
92+
* Detects the MIME type of the provided input stream and validates if the detected MIME type equals the declared
93+
* MIME type. Logs a warning if the full MIME types do not match, throws a {@link RuntimeException} if the base MIME
94+
* types do not match.
95+
*
96+
* @param stream
97+
* input stream of which the MIME type should be detected
98+
* @param declared
99+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
100+
* @throws RuntimeException
101+
* if the detected and the declared base MIME type do not match
102+
*/
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 MIME type {} does not match detected full MIME type {}", result.declared(),
109+
result.detected());
110+
111+
if (!result.declaredBaseType().equals(result.detectedBaseType()))
112+
{
113+
throw new RuntimeException("Declared base MIME type of '" + result.declared()
114+
+ "' does not match detected base MIME type of '" + result.detected() + "'");
115+
}
116+
}
117+
118+
/**
119+
* Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME
120+
* type. Logs a warning if the full MIME types do not match, throws a {@link RuntimeException} if the base MIME
121+
* types do not match.
122+
*
123+
* @param data
124+
* byte array of which the MIME type should be detected
125+
* @param declared
126+
* the declared MIME type of the data, e.g. <code>"application/pdf"</code>
127+
* @throws RuntimeException
128+
* if the detected and the declared base MIME type do not match
129+
*/
130+
default void validateWithException(byte[] data, String declared)
131+
{
132+
validateWithException(new ByteArrayInputStream(data), declared);
133+
}
134+
}

dsf-bpe/dsf-bpe-process-api-v2/src/main/java/dev/dsf/bpe/v2/service/MimetypeService.java

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)