Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.util.ResourceReferenceInfo;
import com.google.common.io.CharSource;
import com.mchange.rmi.NotAuthorizedException;
import de.samply.share.client.control.ApplicationBean;
import de.samply.share.client.crypt.Crypt;
Expand All @@ -19,6 +18,7 @@
import de.samply.share.client.util.xml.XmlUtils;
import de.samply.share.common.utils.SamplyShareUtils;
import jakarta.ws.rs.NotFoundException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -87,11 +87,11 @@ public CtsConnector() throws MalformedURLException {
* upload endpoint.
*
* @param bundleString the patient bundle as String.
* @throws IOException IOException
* @throws NotFoundException NotFoundException
* @throws NotAuthorizedException NotAuthorizedException
* @throws IOException IOException
* @throws NotFoundException NotFoundException
* @throws NotAuthorizedException NotAuthorizedException
* @throws MainzellisteConnectorException MainzellisteConnectorException
* @throws CtsConnectorException CtsConnectorException
* @throws CtsConnectorException CtsConnectorException
*/
public Response postFhirToCts(String bundleString, MediaType mediaType)
throws IOException,
Expand Down Expand Up @@ -159,16 +159,17 @@ private Bundle pseudonymiseBundle(String bundleString, MediaType mediaType)
* Pseudonymize any patient data in the bundle.
*
* @param xmlString the patient bundle which should be pseudonimised
* @return the pseudonimised bundle
* @return the pseudonimised xml
* @throws IOException IOException
* @throws ConfigurationException ConfigurationException
* @throws DataFormatException DataFormatException
*/
private Document pseudonymiseXml(String xmlString)
throws IOException, NotFoundException,
NotAuthorizedException, MainzellisteConnectorException, XmlPareException {
InputStream xmlInputStream = CharSource.wrap(xmlString)
.asByteSource(StandardCharsets.UTF_8).openStream();
byte[] bytes = xmlString.getBytes(StandardCharsets.UTF_8);
InputStream xmlInputStream = new ByteArrayInputStream(bytes);

Document xmlDocument;
xmlDocument = xmlUtils.domBuilder(xmlInputStream);
MainzellisteConnector mainzellisteConnector = ApplicationBean.getMainzellisteConnector();
Expand Down Expand Up @@ -235,7 +236,7 @@ private String cryptIds(Bundle bundle, boolean encrypt) throws CtsConnectorExcep
* @throws GeneralSecurityException GeneralSecurityException
*/
public static IIdType getEncryptedId(IIdType idType, boolean encrypt, Crypt crypt)
throws GeneralSecurityException {
throws GeneralSecurityException {
String id = idType.getIdPart();
if (id == null || "".equals(id)) {
logger.error("Reference or URL does not contain an ID of a resource " + idType.getValue());
Expand Down Expand Up @@ -287,21 +288,20 @@ public void closeResponse(CloseableHttpResponse response) {

/**
* Takes a stringified XML file, assumed to be containing identifying patient data (IDAT),
* replaces the IDAT with a pseudonym, and then sends the pseudonymized xml to the CTS data
* upload endpoint.
* replaces the IDAT with a pseudonym, and then sends the pseudonymized xml to the CTS data upload
* endpoint.
*
* @param xmlString the patient bundle as String.
* @throws IOException IOException
* @throws NotFoundException NotFoundException
* @throws NotAuthorizedException NotAuthorizedException
* @throws IOException IOException
* @throws NotFoundException NotFoundException
* @throws NotAuthorizedException NotAuthorizedException
* @throws MainzellisteConnectorException MainzellisteConnectorException
* @throws CtsConnectorException CtsConnectorException
* @throws CtsConnectorException CtsConnectorException
*/
public Response postXmlToCts(String xmlString)
throws IOException,
NotFoundException, NotAuthorizedException, XmlPareException,
MainzellisteConnectorException, CtsConnectorException {

throws IOException,
NotFoundException, NotAuthorizedException, XmlPareException,
MainzellisteConnectorException, CtsConnectorException {
Document pseudonymXmlDocument = pseudonymiseXml(xmlString);
String pseudonymXmlAsString = xmlUtils.xmlDocToString(pseudonymXmlDocument);
HttpEntity entity = new StringEntity(pseudonymXmlAsString, Consts.UTF_8);
Expand All @@ -314,7 +314,7 @@ public Response postXmlToCts(String xmlString)
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
String message =
"CTS server response: statusCode:" + statusCode + "; response: " + response;
"CTS server response: statusCode:" + statusCode + "; response: " + response;
String responseBody = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
if (responseBody != null && !responseBody.isEmpty()) {
message += ";body: " + responseBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public Bundle getPatientFhirPseudonym(Bundle bundle)
/**
* prepare xml transaction to be sent to the edc system.
*
* @param xmlDoc xml tree.
* @param xmlDoc xml tree
* @return xmlDoc
* @throws NotFoundException Not Found Exception
* @throws NotAuthorizedException Not Authorized Exception
Expand Down Expand Up @@ -226,14 +226,26 @@ public Document getPatientXmlPseudonym(Document xmlDoc)
tokenNode.setTextContent(encryptedId.get(MAINZELLISTE_IDTYPE_ENC_ID).getAsString());
identifyingDataNode.appendChild(tokenNode);
} catch (ConflictException | MandatoryAttributeException
| IllegalArgumentException | XmlPareException e) {
| IllegalArgumentException | XmlPareException e) {
throw new MainzellisteConnectorException(e);
}
return xmlDoc;
}

private String concatenateDate(String year, String month, String day) {
return year + "-" + month + "-" + day;
protected String concatenateDate(String year, String month, String day) {
StringBuilder stringBuilder = new StringBuilder(10);
stringBuilder.append(year);
stringBuilder.append('-');
if (month.length() == 1) {
stringBuilder.append('0');
}
stringBuilder.append(month);
stringBuilder.append('-');
if (day.length() == 1) {
stringBuilder.append('0');
}
stringBuilder.append(day);
return stringBuilder.toString();
}


Expand Down Expand Up @@ -859,7 +871,7 @@ HumanName getPatientName(List<HumanName> nameList, HumanName.NameUse nameUse) {
/**
* validate a date.
*
* @param date date as String
* @param date date as String
* @param dateFormatter Date Time Formatter
* @return boolean
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/samply/share/client/util/xml/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class XmlUtils {
public static final String GEBURTSMONAT_ELEMENT = "geburtsmonat";
public static final String GEBURTSJAHR_ELEMENT = "geburtsjahr";
public static final String VERSICHERUNGSNUMMER_ELEMENT = "versicherungsnummer";
public static final String ADRESSEPLZ_ELEMENT = "adresseplz";
public static final String ADRESSESTADT_ELEMENT = "adressestadt";
public static final String ADRESSESTRASSE_ELEMENT = "adressestrasse";
public static final String ADRESSEPLZ_ELEMENT = "adresse.plz";
public static final String ADRESSESTADT_ELEMENT = "adresse.stadt";
public static final String ADRESSESTRASSE_ELEMENT = "adresse.strasse";
public static final String MEDICAL_DATA_ELEMENT = "medical-data";
public static final String TOKEN_ELEMENT = "token";
public static final String BIRTHDATE_ELEMENT = "birthdate";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.samply.share.client.util.connector;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import de.samply.share.client.util.connector.MainzellisteConnector;
import org.junit.jupiter.api.Test;

class MainzellisteConnectorTest {

@Test
void testConcatenateDate() {
MainzellisteConnector connectorMock = mock(
de.samply.share.client.util.connector.MainzellisteConnector.class);
String year = "2023";
String month = "4";
String day = "20";

String expectedDate = "2023-04-20";
when(connectorMock.concatenateDate(year, month, day)).thenCallRealMethod();
String actualDate = connectorMock.concatenateDate(year, month, day);

assertEquals(expectedDate, actualDate);

}
}