aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/moazs/MyMarshaller.java
diff options
context:
space:
mode:
authorChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-04-16 11:49:56 +0200
committerChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-04-16 12:43:06 +0200
commit5ee2fdd40732aa8eca29e89b14fa5238385868e8 (patch)
tree40d62cdb51232d07375048cd0850e0e806407b6c /src/main/java/at/gv/egiz/moazs/MyMarshaller.java
parentc8271e5684e26b57880de7f1b8a3b0195ad6f68e (diff)
downloadmoa-zs-5ee2fdd40732aa8eca29e89b14fa5238385868e8.tar.gz
moa-zs-5ee2fdd40732aa8eca29e89b14fa5238385868e8.tar.bz2
moa-zs-5ee2fdd40732aa8eca29e89b14fa5238385868e8.zip
Store incoming delivery request on redis server
- Connect to Redis server and implement RedisRepository - Add redis dependencies (spring-boot-starter, jedis, apache commons io). Latter dependencies are apparently needed and not included in the sprint-boot-starter; See https://github.com/spring-projects/spring-boot/issues/5718 and https://www.concretepage.com/questions/599 - Connect DeliveryRequestHandler to RedisRepository - Rewrote Marshalling: replace JaxbContext with spring-oxm JaxbMarshaller - Catch and log all exceptions in App2MZSService; Former: certain exceptions would go unnoticed, e.g. ConnectionRefused
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/MyMarshaller.java')
-rw-r--r--src/main/java/at/gv/egiz/moazs/MyMarshaller.java54
1 files changed, 18 insertions, 36 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/MyMarshaller.java b/src/main/java/at/gv/egiz/moazs/MyMarshaller.java
index 609a3c8..ea14a0f 100644
--- a/src/main/java/at/gv/egiz/moazs/MyMarshaller.java
+++ b/src/main/java/at/gv/egiz/moazs/MyMarshaller.java
@@ -1,51 +1,33 @@
package at.gv.egiz.moazs;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;
-import javax.xml.bind.*;
-import java.io.ByteArrayInputStream;
+import javax.xml.transform.Result;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import java.io.InputStream;
import java.io.StringWriter;
+/**
+ * @author xerx593
+ * Source: https://stackoverflow.com/questions/44676532/how-to-use-spring-to-marshal-and-unmarshal-xml
+ */
@Component
public class MyMarshaller {
-// @Value("classpath:mzs/app2mzs.xsd")
-// private Resource mzsSchema;
-//
-// @Value("classpath:mzs/mzs_mypersondata_en.xsd")
-// private Resource mzsPersonSchema;
-//
-// @Value("classpath:zusemsg/zuse_p2.xsd")
-// private Resource msgSchema;
-//
-// @Value("classpath:zusemsg/zuse_mypersondata_en_p2.xsd")
-// private Resource msgPersonSchema;
-
-
@Autowired
- private JAXBContext context;
-
- public byte[] toBytes(JAXBElement element) {
- try {
- Marshaller marshaller = context.createMarshaller();
- StringWriter sw = new StringWriter();
- marshaller.marshal(element, sw);
- return sw.toString().getBytes();
- } catch (JAXBException e) {
- throw new RuntimeException(e);
- }
- }
-
- public JAXBElement toJAXBElement(byte[] bytes) {
- try {
- Unmarshaller unmarshaller = context.createUnmarshaller();
+ private Jaxb2Marshaller marshaller;
- var stream = new ByteArrayInputStream(bytes);
- return (JAXBElement) unmarshaller.unmarshal(stream);
+ public <T> String marshallXml(final T obj) {
+ StringWriter sw = new StringWriter();
+ Result result = new StreamResult(sw);
+ marshaller.marshal(obj, result);
+ return sw.toString();
+ }
- } catch (JAXBException e) {
- throw new RuntimeException(e);
- }
+ public <T> T unmarshallXml(final InputStream xml) {
+ return (T) marshaller.unmarshal(new StreamSource(xml));
}
}