Skip to content

Commit 8d08356

Browse files
authored
Merge pull request #274 from digipost/support-marshal-to-string
Support marshalling to string and byte array
2 parents a30ac5e + 372ea59 commit 8d08356

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

jaxb/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<dependency>
8585
<groupId>nl.jqno.equalsverifier</groupId>
8686
<artifactId>equalsverifier</artifactId>
87-
<version>3.13.1</version>
87+
<version>3.14</version>
8888
<scope>test</scope>
8989
</dependency>
9090
<dependency>
@@ -102,13 +102,13 @@
102102
<dependency>
103103
<groupId>org.slf4j</groupId>
104104
<artifactId>slf4j-api</artifactId>
105-
<version>2.0.5</version>
105+
<version>2.0.6</version>
106106
<scope>test</scope>
107107
</dependency>
108108
<dependency>
109109
<groupId>org.slf4j</groupId>
110110
<artifactId>slf4j-simple</artifactId>
111-
<version>2.0.5</version>
111+
<version>2.0.6</version>
112112
<scope>test</scope>
113113
</dependency>
114114
</dependencies>
@@ -123,7 +123,7 @@
123123
</plugin>
124124
<plugin>
125125
<artifactId>maven-javadoc-plugin</artifactId>
126-
<version>3.4.1</version>
126+
<version>3.5.0</version>
127127
<configuration>
128128
<excludePackageNames>no.digipost.signature.api.xml.*</excludePackageNames>
129129
<detectOfflineLinks>false</detectOfflineLinks>

jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Set;
4444
import java.util.stream.Stream;
4545

46+
import static java.nio.charset.StandardCharsets.UTF_8;
4647
import static java.util.Objects.requireNonNull;
4748
import static java.util.stream.Collectors.joining;
4849

@@ -182,23 +183,48 @@ public JaxbMarshaller(Set<Class<?>> classes) {
182183
this(classes, null);
183184
}
184185

185-
public void marshal(Object object, OutputStream outputStream){
186+
public String marshalToString(Object object) {
187+
return marshalToResult(object, xml -> xml.toString(UTF_8.name()));
188+
}
189+
190+
public byte[] marshalToBytes(Object object) {
191+
return marshalToResult(object, ByteArrayOutputStream::toByteArray);
192+
}
193+
194+
@FunctionalInterface
195+
private interface ThrowingFunction<T, R> {
196+
R apply(T t) throws Exception;
197+
}
198+
199+
private <R> R marshalToResult(Object object, ThrowingFunction<? super ByteArrayOutputStream, ? extends R> outputStreamMapper) {
200+
try (ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(128)) {
201+
marshal(object, xmlOutputStream);
202+
return outputStreamMapper.apply(xmlOutputStream);
203+
} catch (SignatureMarshalException marshalException) {
204+
throw marshalException;
205+
} catch (Exception e) {
206+
throw SignatureMarshalException.failedMarshal(object, e);
207+
}
208+
}
209+
210+
211+
public void marshal(Object object, OutputStream outputStream) {
186212
try {
187213
Marshaller marshaller = jaxbContext.createMarshaller();
188214
schema.ifPresent(marshaller::setSchema);
189215
marshaller.marshal(object, outputStream);
190216
} catch (Exception e) {
191-
throw new SignatureMarshalException("Failed marshalling " + (object != null ? object.getClass().getName() : "null") + " to XML", e);
217+
throw SignatureMarshalException.failedMarshal(object, e);
192218
}
193219
}
194220

195-
public <T> T unmarshal(InputStream inputStream, Class<T> type){
221+
public <T> T unmarshal(InputStream inputStream, Class<T> type) {
196222
try {
197223
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
198224
schema.ifPresent(unmarshaller::setSchema);
199225
return type.cast(unmarshaller.unmarshal(inputStream));
200226
} catch (Exception e) {
201-
throw new SignatureMarshalException("Failed unmarshalling XML to " + type.getName(), e);
227+
throw SignatureMarshalException.failedUnmarshal(type, e);
202228
}
203229
}
204230

jaxb/src/main/java/no/digipost/signature/jaxb/SignatureMarshalException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
public class SignatureMarshalException extends RuntimeException {
2323

24+
public static SignatureMarshalException failedUnmarshal(Class<?> target, Throwable cause) {
25+
return new SignatureMarshalException("Failed unmarshalling XML to " + target.getName(), cause);
26+
}
27+
28+
public static SignatureMarshalException failedMarshal(Object objectFailingToMarshal, Throwable cause) {
29+
return new SignatureMarshalException(
30+
"Failed marshalling " + (objectFailingToMarshal != null ? objectFailingToMarshal.getClass().getName() : "null") + " to XML",
31+
cause);
32+
}
33+
2434
public SignatureMarshalException(String message, Throwable cause) {
2535
super(message + (cause != null ? ", because " + messageIncludingCauses(cause) : ""), cause);
2636
}

jaxb/src/test/java/no/digipost/signature/jaxb/spring/MarshallingMatchers.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
import org.javers.core.Javers;
2424
import org.javers.core.diff.Diff;
2525

26-
import java.io.ByteArrayInputStream;
2726
import java.io.ByteArrayOutputStream;
2827
import java.io.IOException;
29-
import java.io.InputStream;
3028

29+
import static java.nio.charset.StandardCharsets.UTF_8;
3130
import static java.util.stream.Collectors.joining;
3231
import static no.digipost.DiggBase.friendlyName;
3332
import static no.digipost.DiggExceptions.causalChainOf;
@@ -55,8 +54,9 @@ public void describeTo(Description description) {
5554
@Override
5655
protected boolean matchesSafely(T item, Description mismatchDescription) {
5756
try (ByteArrayOutputStream xmlWriter = new ByteArrayOutputStream()) {
57+
String xml;
5858
try {
59-
marshaller.marshal(item, xmlWriter);
59+
xml = marshaller.marshalToString(item);
6060
} catch (Exception e) {
6161
mismatchDescription
6262
.appendText("Unable to marshall ").appendValue(item).appendText(" to XML, because ")
@@ -65,10 +65,9 @@ protected boolean matchesSafely(T item, Description mismatchDescription) {
6565
.collect(joining(", caused by ")));
6666
return false;
6767
}
68-
String xml = xmlWriter.toString();
6968
Object unmarshalled;
70-
try(InputStream in = new ByteArrayInputStream(xml.getBytes())) {
71-
unmarshalled = marshaller.unmarshal(in, item.getClass());
69+
try {
70+
unmarshalled = marshaller.unmarshal(xml.getBytes(UTF_8), item.getClass());
7271
} catch (Exception e) {
7372
mismatchDescription
7473
.appendValue(item).appendText(" marshalled successfully to XML:\n").appendText(xml)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</plugin>
5050
<plugin>
5151
<artifactId>maven-compiler-plugin</artifactId>
52-
<version>3.10.1</version>
52+
<version>3.11.0</version>
5353
</plugin>
5454
<plugin>
5555
<artifactId>maven-enforcer-plugin</artifactId>
@@ -147,7 +147,7 @@
147147
<plugin>
148148
<groupId>org.codehaus.mojo</groupId>
149149
<artifactId>versions-maven-plugin</artifactId>
150-
<version>2.14.2</version>
150+
<version>2.15.0</version>
151151
</plugin>
152152
</plugins>
153153
</pluginManagement>

0 commit comments

Comments
 (0)