diff --git a/jaxb/pom.xml b/jaxb/pom.xml
index c70b76d..d722325 100644
--- a/jaxb/pom.xml
+++ b/jaxb/pom.xml
@@ -84,7 +84,7 @@
nl.jqno.equalsverifier
equalsverifier
- 3.13.1
+ 3.14
test
@@ -102,13 +102,13 @@
org.slf4j
slf4j-api
- 2.0.5
+ 2.0.6
test
org.slf4j
slf4j-simple
- 2.0.5
+ 2.0.6
test
@@ -123,7 +123,7 @@
maven-javadoc-plugin
- 3.4.1
+ 3.5.0
no.digipost.signature.api.xml.*
false
diff --git a/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java b/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java
index 95f2010..dfcabe6 100644
--- a/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java
+++ b/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java
@@ -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;
@@ -182,23 +183,48 @@ public JaxbMarshaller(Set> 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 {
+ R apply(T t) throws Exception;
+ }
+
+ private 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 unmarshal(InputStream inputStream, Class type){
+ public T unmarshal(InputStream inputStream, Class 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);
}
}
diff --git a/jaxb/src/main/java/no/digipost/signature/jaxb/SignatureMarshalException.java b/jaxb/src/main/java/no/digipost/signature/jaxb/SignatureMarshalException.java
index 698deae..1b4c29c 100644
--- a/jaxb/src/main/java/no/digipost/signature/jaxb/SignatureMarshalException.java
+++ b/jaxb/src/main/java/no/digipost/signature/jaxb/SignatureMarshalException.java
@@ -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);
}
diff --git a/jaxb/src/test/java/no/digipost/signature/jaxb/spring/MarshallingMatchers.java b/jaxb/src/test/java/no/digipost/signature/jaxb/spring/MarshallingMatchers.java
index 5651a93..eba49ec 100644
--- a/jaxb/src/test/java/no/digipost/signature/jaxb/spring/MarshallingMatchers.java
+++ b/jaxb/src/test/java/no/digipost/signature/jaxb/spring/MarshallingMatchers.java
@@ -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;
@@ -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 ")
@@ -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)
diff --git a/pom.xml b/pom.xml
index c14b026..abf002b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -49,7 +49,7 @@
maven-compiler-plugin
- 3.10.1
+ 3.11.0
maven-enforcer-plugin
@@ -147,7 +147,7 @@
org.codehaus.mojo
versions-maven-plugin
- 2.14.2
+ 2.15.0