Skip to content

Commit

Permalink
#428 Add basic mimetype validation to XDS Documententry.mimetype
Browse files Browse the repository at this point in the history
attribute
  • Loading branch information
Thopap committed Aug 31, 2023
1 parent 6651d62 commit 31324ac
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.openehealth.ipf.commons.ihe.xds.core.validate;

import static org.apache.commons.lang3.Validate.notNull;
import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;

import java.util.regex.Pattern;

/**
* Validate the basic format of a the document mime type.
*
* Mime type must consist of type and subtype, separated by a /
*/
public class MimeTypeValidator implements ValueValidator {
private static final Pattern MIME_PATTERN = Pattern.compile("\\w+\\/[-+.\\w]+");

@Override
public void validate(String mimeType) throws XDSMetaDataException {
notNull(mimeType, "mimeType cannot be null");

metaDataAssert(MIME_PATTERN.matcher(mimeType).matches(), ValidationMessage.MIME_TYPE_FORMAT, mimeType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public enum ValidationMessage {
ON_DEMAND_DOC_ID_MUST_DIFFER("New ID of the document should differ from the On-Demand document entry ID"),
WRONG_DOCUMENT_ENTRY_TYPE("Wrong document entry type (stable/on-demand): %s"),
MIME_TYPE_MUST_BE_SPECIFIED("The document MIME type is missing"),
MIME_TYPE_FORMAT("The specified document MIME type %s is invalid"),
INVALID_STATUS_IN_RESPONSE("Invalid status in response"),
INVALID_ERROR_INFO_IN_RESPONSE("Invalid error info in response"),
INVALID_ERROR_CODE_IN_RESPONSE("Invalid error code in response"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ObjectContainerValidator implements Validator<EbXMLObjectContainer,
private final CXiValidator cxiValidator = new CXiValidator();
private final UUIDValidator uuidValidator = new UUIDValidator();
private final IdentifierValidator identifierValidator = new IdentifierValidator();
private final MimeTypeValidator mimeTypeValidator = new MimeTypeValidator();

private final SlotValueValidation[] authorValidations = new SlotValueValidation[] {
new SlotValueValidation(SLOT_NAME_AUTHOR_PERSON, xcnValidator, 0, 1),
Expand Down Expand Up @@ -281,6 +282,7 @@ private void validateDocumentEntries(EbXMLObjectContainer container, ValidationP

var mimeType = docEntry.getMimeType();
metaDataAssert(StringUtils.isNotEmpty(mimeType), MIME_TYPE_MUST_BE_SPECIFIED);
mimeTypeValidator.validate(mimeType);

if ((profile == XDS.Interactions.ITI_57) || (profile == RMU.Interactions.ITI_92)) {
validateUpdateObject(docEntry, container, profile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.openehealth.ipf.commons.ihe.xds.core.validate;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class MimeTypeValidatorTest {
private static final MimeTypeValidator validator = new MimeTypeValidator();

@ParameterizedTest
@ValueSource(strings = {"text/xml", "application/hl7-v3", "application/fhir+xml"})
public void testValidateGoodCases(String validMimeType) throws XDSMetaDataException {
validator.validate(validMimeType);
}

@ParameterizedTest
@ValueSource(strings = {"something", "application/test blank", "application/is/wrong"})
public void testValidateBadCases(String invalidMimeType) throws XDSMetaDataException {
assertThrows(XDSMetaDataException.class, () -> validator.validate(invalidMimeType));
}


}
5 changes: 4 additions & 1 deletion src/site/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
</properties>
<body>
<release version="4.x.x" description="IPF 4.x.x" date="tbd">
<action issue="428" dev="thopap" type="add">
Add basic mimetype validation to XDS Documententry.mimetype attribute
</action>
<action issue="427" dev="thopap" type="update">
Dependency updates (Groovy, HAPI-FHIR, Sprong Boot, etc.)
Dependency updates (Groovy, HAPI-FHIR, Spring Boot, etc.)
</action>
<action issue="426" dev="unixoid" type="fix">
Fixed Schematron validation of ITI-45 requests
Expand Down

0 comments on commit 31324ac

Please sign in to comment.