From ef9a86858a5e05b4dd9bff3992c477f49b0b20f2 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 16 Apr 2019 13:29:48 +0200 Subject: Add Getter for Repository + Refactor Marshalling - mv MyMarshaller Marshaller - Remove jaxbcontext bean - Move Marshaller config to MarshallerConfig class --- src/main/java/at/gv/egiz/moazs/Marshaller.java | 33 ++++++++++++++++++++++ .../java/at/gv/egiz/moazs/MarshallerConfig.java | 30 ++++++++++++++++++++ src/main/java/at/gv/egiz/moazs/MyMarshaller.java | 33 ---------------------- .../java/at/gv/egiz/moazs/MyMarshallerConfig.java | 22 --------------- src/main/java/at/gv/egiz/moazs/RedisConfig.java | 17 ----------- .../java/at/gv/egiz/moazs/RedisRepository.java | 20 ++++++++----- 6 files changed, 76 insertions(+), 79 deletions(-) create mode 100644 src/main/java/at/gv/egiz/moazs/Marshaller.java create mode 100644 src/main/java/at/gv/egiz/moazs/MarshallerConfig.java delete mode 100644 src/main/java/at/gv/egiz/moazs/MyMarshaller.java delete mode 100644 src/main/java/at/gv/egiz/moazs/MyMarshallerConfig.java (limited to 'src/main') diff --git a/src/main/java/at/gv/egiz/moazs/Marshaller.java b/src/main/java/at/gv/egiz/moazs/Marshaller.java new file mode 100644 index 0000000..c126f03 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/Marshaller.java @@ -0,0 +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.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 Marshaller { + + @Autowired + private Jaxb2Marshaller marshaller; + + public String marshallXml(final T obj) { + StringWriter sw = new StringWriter(); + Result result = new StreamResult(sw); + marshaller.marshal(obj, result); + return sw.toString(); + } + + public T unmarshallXml(final InputStream xml) { + return (T) marshaller.unmarshal(new StreamSource(xml)); + } +} diff --git a/src/main/java/at/gv/egiz/moazs/MarshallerConfig.java b/src/main/java/at/gv/egiz/moazs/MarshallerConfig.java new file mode 100644 index 0000000..9106dd0 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/MarshallerConfig.java @@ -0,0 +1,30 @@ +package at.gv.egiz.moazs; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import java.util.HashMap; + +@Configuration +public class MarshallerConfig { + + @Bean + public Jaxb2Marshaller jaxb2Marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound( + at.gv.e_government.reference.namespace.zustellung.mzs.persondata_.ObjectFactory.class, + at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory.class, + at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory.class, + at.gv.e_government.reference.namespace.persondata.phase2._20181206_.ObjectFactory.class); + + marshaller.setMarshallerProperties(new HashMap() {{ + put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); + }}); + + return marshaller; + } + +} diff --git a/src/main/java/at/gv/egiz/moazs/MyMarshaller.java b/src/main/java/at/gv/egiz/moazs/MyMarshaller.java deleted file mode 100644 index ea14a0f..0000000 --- a/src/main/java/at/gv/egiz/moazs/MyMarshaller.java +++ /dev/null @@ -1,33 +0,0 @@ -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.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 { - - @Autowired - private Jaxb2Marshaller marshaller; - - public String marshallXml(final T obj) { - StringWriter sw = new StringWriter(); - Result result = new StreamResult(sw); - marshaller.marshal(obj, result); - return sw.toString(); - } - - public T unmarshallXml(final InputStream xml) { - return (T) marshaller.unmarshal(new StreamSource(xml)); - } -} diff --git a/src/main/java/at/gv/egiz/moazs/MyMarshallerConfig.java b/src/main/java/at/gv/egiz/moazs/MyMarshallerConfig.java deleted file mode 100644 index 9842d49..0000000 --- a/src/main/java/at/gv/egiz/moazs/MyMarshallerConfig.java +++ /dev/null @@ -1,22 +0,0 @@ -package at.gv.egiz.moazs; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; - -@Configuration -public class MyMarshallerConfig { - - @Bean - public JAXBContext jaxbcontext() throws JAXBException { - return JAXBContext.newInstance( - at.gv.e_government.reference.namespace.zustellung.mzs.persondata_.ObjectFactory.class, - at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory.class, - at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory.class, - at.gv.e_government.reference.namespace.persondata.phase2._20181206_.ObjectFactory.class); - - } - -} diff --git a/src/main/java/at/gv/egiz/moazs/RedisConfig.java b/src/main/java/at/gv/egiz/moazs/RedisConfig.java index 3487eb5..758fbfd 100644 --- a/src/main/java/at/gv/egiz/moazs/RedisConfig.java +++ b/src/main/java/at/gv/egiz/moazs/RedisConfig.java @@ -44,21 +44,4 @@ public class RedisConfig { return template; } - @Bean - public Jaxb2Marshaller jaxb2Marshaller() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound( - at.gv.e_government.reference.namespace.zustellung.mzs.persondata_.ObjectFactory.class, - at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory.class, - at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory.class, - at.gv.e_government.reference.namespace.persondata.phase2._20181206_.ObjectFactory.class); //"alternatively" setContextPath(), - - marshaller.setMarshallerProperties(new HashMap() {{ - put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); - }}); - - return marshaller; - } - - } diff --git a/src/main/java/at/gv/egiz/moazs/RedisRepository.java b/src/main/java/at/gv/egiz/moazs/RedisRepository.java index dfc68ea..db49b23 100644 --- a/src/main/java/at/gv/egiz/moazs/RedisRepository.java +++ b/src/main/java/at/gv/egiz/moazs/RedisRepository.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Repository; import javax.annotation.PostConstruct; import javax.xml.bind.JAXBElement; +import java.util.Optional; @Repository public class RedisRepository { @@ -22,14 +23,14 @@ public class RedisRepository { private final RedisTemplate redisTemplate; - private final MyMarshaller marshaller; + private final Marshaller marshaller; private HashOperations hashOperations; private final ObjectFactory factory = new ObjectFactory(); @Autowired - public RedisRepository(RedisTemplate redisTemplate, MyMarshaller marshaller){ + public RedisRepository(RedisTemplate redisTemplate, Marshaller marshaller){ this.redisTemplate = redisTemplate; this.marshaller = marshaller; } @@ -43,15 +44,20 @@ public class RedisRepository { var hashKey = request.getMetaData().getAppDeliveryID(); var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); - logger.info("Adding DeliveryRequest to repository: AppDeliveryId = {}, request = {}", hashKey, serializedRequest); + logger.info("Save DeliveryRequest with AppDeliveryId = {}", hashKey); hashOperations.put(KEY, hashKey, serializedRequest); + } - var drs = (String) hashOperations.get(KEY, hashKey); - var requestFromRedis = (JAXBElement) marshaller.unmarshallXml(new StringInputStream(drs)); - String appDeliveryID = requestFromRedis.getValue().getMetaData().getAppDeliveryID(); - logger.info("here's what i got out of redis: appDeliveryId = {}, request = {}", appDeliveryID, drs); + public Optional getDeliveryRequest(String appDeliveryID) { + var serializedRequest = (String) hashOperations.get(KEY, appDeliveryID); + if (serializedRequest == null) { + return Optional.empty(); + } + var stream = new StringInputStream(serializedRequest); + var request = (JAXBElement) marshaller.unmarshallXml(stream); + return Optional.of(request.getValue()); } } -- cgit v1.2.3