Skip to content
Merged
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
8 changes: 4 additions & 4 deletions jaxb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.13.1</version>
<version>3.14</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -102,13 +102,13 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -123,7 +123,7 @@
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<configuration>
<excludePackageNames>no.digipost.signature.api.xml.*</excludePackageNames>
<detectOfflineLinks>false</detectOfflineLinks>
Expand Down
34 changes: 30 additions & 4 deletions jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Set;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -182,23 +183,48 @@ public JaxbMarshaller(Set<Class<?>> classes) {
this(classes, null);
}

public void marshal(Object object, OutputStream outputStream){
public String marshalToString(Object object) {
return marshalToResult(object, xml -> xml.toString(UTF_8.name()));
}

public byte[] marshalToBytes(Object object) {
return marshalToResult(object, ByteArrayOutputStream::toByteArray);
}

@FunctionalInterface
private interface ThrowingFunction<T, R> {
R apply(T t) throws Exception;
}

private <R> R marshalToResult(Object object, ThrowingFunction<? super ByteArrayOutputStream, ? extends R> outputStreamMapper) {
try (ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(128)) {
marshal(object, xmlOutputStream);
return outputStreamMapper.apply(xmlOutputStream);
} catch (SignatureMarshalException marshalException) {
throw marshalException;
} catch (Exception e) {
throw SignatureMarshalException.failedMarshal(object, e);
}
}


public void marshal(Object object, OutputStream outputStream) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
schema.ifPresent(marshaller::setSchema);
marshaller.marshal(object, outputStream);
} catch (Exception e) {
throw new SignatureMarshalException("Failed marshalling " + (object != null ? object.getClass().getName() : "null") + " to XML", e);
throw SignatureMarshalException.failedMarshal(object, e);
}
}

public <T> T unmarshal(InputStream inputStream, Class<T> type){
public <T> T unmarshal(InputStream inputStream, Class<T> type) {
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
schema.ifPresent(unmarshaller::setSchema);
return type.cast(unmarshaller.unmarshal(inputStream));
} catch (Exception e) {
throw new SignatureMarshalException("Failed unmarshalling XML to " + type.getName(), e);
throw SignatureMarshalException.failedUnmarshal(type, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

public class SignatureMarshalException extends RuntimeException {

public static SignatureMarshalException failedUnmarshal(Class<?> target, Throwable cause) {
return new SignatureMarshalException("Failed unmarshalling XML to " + target.getName(), cause);
}

public static SignatureMarshalException failedMarshal(Object objectFailingToMarshal, Throwable cause) {
return new SignatureMarshalException(
"Failed marshalling " + (objectFailingToMarshal != null ? objectFailingToMarshal.getClass().getName() : "null") + " to XML",
cause);
}

public SignatureMarshalException(String message, Throwable cause) {
super(message + (cause != null ? ", because " + messageIncludingCauses(cause) : ""), cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.javers.core.Javers;
import org.javers.core.diff.Diff;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import static no.digipost.DiggBase.friendlyName;
import static no.digipost.DiggExceptions.causalChainOf;
Expand Down Expand Up @@ -55,8 +54,9 @@ public void describeTo(Description description) {
@Override
protected boolean matchesSafely(T item, Description mismatchDescription) {
try (ByteArrayOutputStream xmlWriter = new ByteArrayOutputStream()) {
String xml;
try {
marshaller.marshal(item, xmlWriter);
xml = marshaller.marshalToString(item);
} catch (Exception e) {
mismatchDescription
.appendText("Unable to marshall ").appendValue(item).appendText(" to XML, because ")
Expand All @@ -65,10 +65,9 @@ protected boolean matchesSafely(T item, Description mismatchDescription) {
.collect(joining(", caused by ")));
return false;
}
String xml = xmlWriter.toString();
Object unmarshalled;
try(InputStream in = new ByteArrayInputStream(xml.getBytes())) {
unmarshalled = marshaller.unmarshal(in, item.getClass());
try {
unmarshalled = marshaller.unmarshal(xml.getBytes(UTF_8), item.getClass());
} catch (Exception e) {
mismatchDescription
.appendValue(item).appendText(" marshalled successfully to XML:\n").appendText(xml)
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down Expand Up @@ -147,7 +147,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.14.2</version>
<version>2.15.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down