Skip to content

Commit 5ef157e

Browse files
committed
Merge remote-tracking branch 'origin/develop_2' into
issue/208_209_logging
2 parents a2e9c60 + dd08737 commit 5ef157e

File tree

18 files changed

+364
-105
lines changed

18 files changed

+364
-105
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import dev.dsf.bpe.v2.service.EndpointProvider;
1515
import dev.dsf.bpe.v2.service.FhirClientProvider;
1616
import dev.dsf.bpe.v2.service.MailService;
17-
import dev.dsf.bpe.v2.service.MimetypeService;
17+
import dev.dsf.bpe.v2.service.MimeTypeService;
1818
import dev.dsf.bpe.v2.service.OidcClientProvider;
1919
import dev.dsf.bpe.v2.service.OrganizationProvider;
2020
import dev.dsf.bpe.v2.service.QuestionnaireResponseHelper;
@@ -32,7 +32,7 @@ public class ProcessPluginApiImpl implements ProcessPluginApi, InitializingBean
3232
private final FhirClientProvider fhirClientProvider;
3333
private final OidcClientProvider oidcClientProvider;
3434
private final MailService mailService;
35-
private final MimetypeService mimetypeService;
35+
private final MimeTypeService mimeTypeService;
3636
private final ObjectMapper objectMapper;
3737
private final OrganizationProvider organizationProvider;
3838
private final ProcessAuthorizationHelper processAuthorizationHelper;
@@ -45,7 +45,7 @@ public class ProcessPluginApiImpl implements ProcessPluginApi, InitializingBean
4545

4646
public ProcessPluginApiImpl(ProxyConfig proxyConfig, EndpointProvider endpointProvider, FhirContext fhirContext,
4747
DsfClientProvider dsfClientProvider, FhirClientProvider fhirClientProvider,
48-
OidcClientProvider oidcClientProvider, MailService mailService, MimetypeService mimetypeService,
48+
OidcClientProvider oidcClientProvider, MailService mailService, MimeTypeService mimeTypeService,
4949
ObjectMapper objectMapper, OrganizationProvider organizationProvider,
5050
ProcessAuthorizationHelper processAuthorizationHelper,
5151
QuestionnaireResponseHelper questionnaireResponseHelper, ReadAccessHelper readAccessHelper,
@@ -58,7 +58,7 @@ public ProcessPluginApiImpl(ProxyConfig proxyConfig, EndpointProvider endpointPr
5858
this.fhirClientProvider = fhirClientProvider;
5959
this.oidcClientProvider = oidcClientProvider;
6060
this.mailService = mailService;
61-
this.mimetypeService = mimetypeService;
61+
this.mimeTypeService = mimeTypeService;
6262
this.objectMapper = objectMapper;
6363
this.organizationProvider = organizationProvider;
6464
this.processAuthorizationHelper = processAuthorizationHelper;
@@ -80,6 +80,7 @@ public void afterPropertiesSet() throws Exception
8080
Objects.requireNonNull(fhirClientProvider, "fhirClientProvider");
8181
Objects.requireNonNull(oidcClientProvider, "oidcClientProvider");
8282
Objects.requireNonNull(mailService, "mailService");
83+
Objects.requireNonNull(mimeTypeService, "mimeTypeService");
8384
Objects.requireNonNull(objectMapper, "objectMapper");
8485
Objects.requireNonNull(organizationProvider, "organizationProvider");
8586
Objects.requireNonNull(processAuthorizationHelper, "processAuthorizationHelper");
@@ -134,9 +135,9 @@ public MailService getMailService()
134135
}
135136

136137
@Override
137-
public MimetypeService getMimetypeService()
138+
public MimeTypeService getMimeTypeService()
138139
{
139-
return mimetypeService;
140+
return mimeTypeService;
140141
}
141142

142143
@Override
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
import org.apache.tika.io.TikaInputStream;
99
import org.apache.tika.metadata.Metadata;
1010
import org.apache.tika.mime.MediaType;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
1311
import org.springframework.beans.factory.InitializingBean;
1412

15-
public class MimetypeServiceImpl implements MimetypeService, InitializingBean
13+
public class MimeTypeServiceImpl implements MimeTypeService, InitializingBean
1614
{
17-
private static final Logger logger = LoggerFactory.getLogger(MimetypeServiceImpl.class);
18-
1915
private final Detector detector;
2016

21-
public MimetypeServiceImpl(Detector detector)
17+
public MimeTypeServiceImpl(Detector detector)
2218
{
2319
this.detector = detector;
2420
}
@@ -30,10 +26,9 @@ public void afterPropertiesSet() throws Exception
3026
}
3127

3228
@Override
33-
public void validate(InputStream stream, String declared)
29+
public ValidationResult validateWithResult(InputStream stream, String declared)
3430
{
3531
MediaType declaredMimeType = MediaType.parse(declared);
36-
MediaType detectedMimeType;
3732

3833
try
3934
{
@@ -44,21 +39,14 @@ public void validate(InputStream stream, String declared)
4439
Metadata metadata = new Metadata();
4540
metadata.add(Metadata.CONTENT_TYPE, declaredMimeType.toString());
4641

47-
detectedMimeType = detector.detect(input, metadata);
42+
MediaType detectedMimeType = detector.detect(input, metadata);
43+
44+
return new ValidationResult(declaredMimeType.getType(), declaredMimeType.getSubtype(),
45+
detectedMimeType.getType(), detectedMimeType.getSubtype());
4846
}
4947
catch (IOException exception)
5048
{
51-
throw new RuntimeException("Error while detecting mimetype", exception);
52-
}
53-
54-
if (!declaredMimeType.equals(detectedMimeType))
55-
logger.warn("Declared full mimetype {} does not match detected full mimetype {}",
56-
declaredMimeType.toString(), detectedMimeType.toString());
57-
58-
if (!declaredMimeType.getType().equals(detectedMimeType.getType()))
59-
{
60-
throw new RuntimeException("Declared base mimetype of '" + declaredMimeType.toString()
61-
+ "' does not match detected base mimetype of '" + detectedMimeType.toString() + "'");
49+
throw new RuntimeException("Error while detecting MIME type", exception);
6250
}
6351
}
6452
}

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
import dev.dsf.bpe.v2.service.FhirClientProviderWithEndpointSupport;
5151
import dev.dsf.bpe.v2.service.MailService;
5252
import dev.dsf.bpe.v2.service.MailServiceDelegate;
53-
import dev.dsf.bpe.v2.service.MimetypeService;
54-
import dev.dsf.bpe.v2.service.MimetypeServiceImpl;
53+
import dev.dsf.bpe.v2.service.MimeTypeService;
54+
import dev.dsf.bpe.v2.service.MimeTypeServiceImpl;
5555
import dev.dsf.bpe.v2.service.OidcClientProvider;
5656
import dev.dsf.bpe.v2.service.OidcClientProviderDelegate;
5757
import dev.dsf.bpe.v2.service.OrganizationProvider;
@@ -101,7 +101,7 @@ public class ApiServiceConfig
101101
public ProcessPluginApi processPluginApiV2()
102102
{
103103
return new ProcessPluginApiImpl(proxyConfigDelegate(), endpointProvider(), fhirContext(), dsfClientProvider(),
104-
fhirClientProvider(), oidcClientProvider(), mailService(), mimetypeService(), objectMapper(),
104+
fhirClientProvider(), oidcClientProvider(), mailService(), mimeTypeService(), objectMapper(),
105105
organizationProvider(), processAuthorizationHelper(), questionnaireResponseHelper(), readAccessHelper(),
106106
taskHelper(), cryptoService(), targetProvider(), dataLogger());
107107
}
@@ -162,10 +162,10 @@ public MailService mailService()
162162
}
163163

164164
@Bean
165-
public MimetypeService mimetypeService()
165+
public MimeTypeService mimeTypeService()
166166
{
167167
Detector detector = CombinedDetectors.withDefaultAndNdJson(NdJsonDetector.DEFAULT_LINES_TO_CHECK);
168-
return new MimetypeServiceImpl(detector);
168+
return new MimeTypeServiceImpl(detector);
169169
}
170170

171171
@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
@@ -13,7 +13,7 @@
1313
import dev.dsf.bpe.v2.service.EndpointProvider;
1414
import dev.dsf.bpe.v2.service.FhirClientProvider;
1515
import dev.dsf.bpe.v2.service.MailService;
16-
import dev.dsf.bpe.v2.service.MimetypeService;
16+
import dev.dsf.bpe.v2.service.MimeTypeService;
1717
import dev.dsf.bpe.v2.service.OidcClientProvider;
1818
import dev.dsf.bpe.v2.service.OrganizationProvider;
1919
import dev.dsf.bpe.v2.service.QuestionnaireResponseHelper;
@@ -45,7 +45,7 @@ public interface ProcessPluginApi
4545

4646
MailService getMailService();
4747

48-
MimetypeService getMimetypeService();
48+
MimeTypeService getMimeTypeService();
4949

5050
ObjectMapper getObjectMapper();
5151

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 & 38 deletions
This file was deleted.

dsf-bpe/dsf-bpe-server/src/test/java/dev/dsf/bpe/integration/PluginV2IntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ private void deleteFile(Path path)
210210
}
211211

212212
@Test
213-
public void startMimetypeServiceTest() throws Exception
213+
public void startMimeTypeServiceTest() throws Exception
214214
{
215-
executePluginTest(createTestTask("MimetypeServiceTest"));
215+
executePluginTest(createTestTask("MimeTypeServiceTest"));
216216
}
217217

218218
@Test

dsf-bpe/dsf-bpe-test-plugin-v2/src/main/java/dev/dsf/bpe/test/service/ApiTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public void apiGetMailServiceNotNull(ProcessPluginApi api) throws Exception
6060
}
6161

6262
@PluginTest
63-
public void apiGetMimetypeService(ProcessPluginApi api) throws Exception
63+
public void apiGetMimeTypeService(ProcessPluginApi api) throws Exception
6464
{
65-
expectNotNull(api.getMimetypeService());
65+
expectNotNull(api.getMimeTypeService());
6666
}
6767

6868
@PluginTest

0 commit comments

Comments
 (0)