From 6f450a1d3ff60557b1081f9e8ef6bd5f375db842 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Wed, 8 May 2019 13:41:27 +0200 Subject: Refactor: Consolidate Schema-Related Components --- src/main/java/at/gv/egiz/moazs/Marshaller.java | 33 --- .../java/at/gv/egiz/moazs/MoaZSPrefixMapper.java | 54 ---- .../java/at/gv/egiz/moazs/Mzs2MsgConverter.java | 321 --------------------- .../at/gv/egiz/moazs/config/MarshallerConfig.java | 5 +- .../moazs/pipeline/SameThreadDeliveryPipeline.java | 2 +- .../moazs/repository/RedisDeliveryRepository.java | 2 +- .../java/at/gv/egiz/moazs/scheme/Marshaller.java | 33 +++ .../at/gv/egiz/moazs/scheme/MoaZSPrefixMapper.java | 54 ++++ .../at/gv/egiz/moazs/scheme/Mzs2MsgConverter.java | 321 +++++++++++++++++++++ 9 files changed, 412 insertions(+), 413 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/Marshaller.java delete mode 100644 src/main/java/at/gv/egiz/moazs/MoaZSPrefixMapper.java delete mode 100644 src/main/java/at/gv/egiz/moazs/Mzs2MsgConverter.java create mode 100644 src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java create mode 100644 src/main/java/at/gv/egiz/moazs/scheme/MoaZSPrefixMapper.java create mode 100644 src/main/java/at/gv/egiz/moazs/scheme/Mzs2MsgConverter.java (limited to 'src/main/java/at/gv') diff --git a/src/main/java/at/gv/egiz/moazs/Marshaller.java b/src/main/java/at/gv/egiz/moazs/Marshaller.java deleted file mode 100644 index 7e2ab17..0000000 --- a/src/main/java/at/gv/egiz/moazs/Marshaller.java +++ /dev/null @@ -1,33 +0,0 @@ -package at.gv.egiz.moazs; - -import org.springframework.oxm.jaxb.Jaxb2Marshaller; - -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 - */ -public class Marshaller { - - private final Jaxb2Marshaller marshaller; - - public Marshaller(Jaxb2Marshaller marshaller) { - this.marshaller = 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/MoaZSPrefixMapper.java b/src/main/java/at/gv/egiz/moazs/MoaZSPrefixMapper.java deleted file mode 100644 index ef85989..0000000 --- a/src/main/java/at/gv/egiz/moazs/MoaZSPrefixMapper.java +++ /dev/null @@ -1,54 +0,0 @@ -package at.gv.egiz.moazs; - -import com.sun.xml.bind.marshaller.NamespacePrefixMapper; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; - -@Component -public class MoaZSPrefixMapper extends NamespacePrefixMapper { - - private final Map map = new HashMap<>(); - - public MoaZSPrefixMapper() { - map.put(getMsgNamespaceUri(), "msg"); - map.put(getMsgPNamespaceUri(), "msgp"); - map.put(getMzsNamespaceUri(), "mzs"); - map.put(getMzsPNamespaceUri(), "mzsp"); - map.put(getDsigNamespaceUri(), "dsig"); - } - - public MoaZSPrefixMapper(boolean isMzs) { - this(); - map.put((isMzs) ? getMzsPNamespaceUri() : getMsgPNamespaceUri(), "p"); - map.put((isMzs) ? getMzsNamespaceUri() : getMsgNamespaceUri(), ""); - } - - @Override - public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { - return map.getOrDefault(namespaceUri, suggestion); - } - - private String getMsgNamespaceUri() { - return new at.gv.zustellung.msg.xsd.ObjectFactory().createDeliveryRequest(null).getName().getNamespaceURI(); - } - - private String getMsgPNamespaceUri() { - return new at.gv.zustellung.msg.xsd.persondata.ObjectFactory().createPerson(null).getName().getNamespaceURI(); - } - - private String getMzsNamespaceUri() { - return new at.gv.zustellung.app2mzs.xsd.ObjectFactory().createDeliveryRequest(null).getName().getNamespaceURI(); - } - - private String getMzsPNamespaceUri() { - return new at.gv.zustellung.app2mzs.xsd.persondata.ObjectFactory().createAbstractPersonData(null).getName().getNamespaceURI(); - } - - private String getDsigNamespaceUri() { - return new org.w3._2000._09.xmldsig_.ObjectFactory().createCanonicalizationMethod(null).getName().getNamespaceURI(); - } - - -} diff --git a/src/main/java/at/gv/egiz/moazs/Mzs2MsgConverter.java b/src/main/java/at/gv/egiz/moazs/Mzs2MsgConverter.java deleted file mode 100644 index b517f39..0000000 --- a/src/main/java/at/gv/egiz/moazs/Mzs2MsgConverter.java +++ /dev/null @@ -1,321 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.zustellung.msg.xsd.*; -import at.gv.zustellung.msg.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; -import at.gv.zustellung.msg.xsd.persondata.*; -import org.springframework.stereotype.Component; -import org.springframework.util.Assert; - -import javax.xml.bind.JAXBElement; -import java.util.List; - -import static at.gv.zustellung.msg.xsd.AttachmentType.attachmentTypeBuilder; -import static at.gv.zustellung.msg.xsd.AttachmentsType.attachmentsTypeBuilder; -import static at.gv.zustellung.msg.xsd.ConfirmationAddress.confirmationAddressBuilder; -import static at.gv.zustellung.msg.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; -import static at.gv.zustellung.msg.xsd.Receiver.receiverBuilder; -import static at.gv.zustellung.msg.xsd.Sender.senderBuilder; -import static at.gv.zustellung.msg.xsd.SenderCorporateBodyType.senderCorporateBodyTypeBuilder; -import static at.gv.zustellung.msg.xsd.WebserviceURL.webserviceURLBuilder; -import static at.gv.zustellung.msg.xsd.persondata.CorporateBodyType.corporateBodyTypeBuilder; -import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.Value.valueBuilder; -import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.identificationTypeBuilder; -import static at.gv.zustellung.msg.xsd.persondata.InternetAddressType.internetAddressTypeBuilder; -import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.*; -import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.Affix.affixBuilder; -import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.FamilyName.familyNameBuilder; -import static at.gv.zustellung.msg.xsd.persondata.PhysicalPersonType.physicalPersonTypeBuilder; -import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.DeliveryAddress.deliveryAddressBuilder; -import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.postalAddressTypeBuilder; -import static at.gv.zustellung.msg.xsd.persondata.TelephoneAddressType.telephoneAddressTypeBuilder; -import static java.lang.String.format; -import static java.util.stream.Collectors.toList; - - -@Component -public class Mzs2MsgConverter { - - private final at.gv.zustellung.msg.xsd.ObjectFactory msgFactory = new at.gv.zustellung.msg.xsd.ObjectFactory(); - private final at.gv.zustellung.msg.xsd.persondata.ObjectFactory personFactory = new at.gv.zustellung.msg.xsd.persondata.ObjectFactory(); - - public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { - return setupBuilder(mzsRequest) - .withReceiver(convert(mzsRequest.getReceiver())) - .build(); - } - - public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest, - IdentificationType identification) { - return setupBuilder(mzsRequest) - .withReceiver(convert(mzsRequest.getReceiver(), identification)) - .build(); - } - - private DeliveryRequestTypeBuilder setupBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { - return deliveryRequestTypeBuilder() - .withVersion("2.0.0") - .withSender(convert(mzsRequest.getSender())) - .withMetaData(mzsRequest.getMetaData()) - .withAttachments(convertPayloads(mzsRequest.getPayload())); - } - - //------------- SENDER ------------------ - - private Sender convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { - return senderBuilder() - .withSenderPerson(extractSenderPerson(sender)) - .withLogo(sender.getLogo()) - .withAdditionalCriteria(sender.getAdditionalCriteria()) - .withConfirmationAddress(convertConfirmationAddress(sender)) - .build(); - } - - private JAXBElement extractSenderPerson(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { - - Assert.isTrue(sender.getSenderProfile() != null || sender.getCorporateBody() != null, - "Neither SenderProfile nor CorporateBody are given"); - - var builder = senderCorporateBodyTypeBuilder(); - - if(sender.getSenderProfile() != null) { - builder.withSenderProfile(sender.getSenderProfile()); - } else { - var identification = sender.getCorporateBody().getIdentification().get(0); - builder.withCorporateBody(convert(sender.getCorporateBody())) - .withIdentification(convert(identification)); - } - - builder.withId(sender.getId()); - - return msgFactory.createSenderCorporateBody(builder.build()); - - } - - private ConfirmationAddress convertConfirmationAddress(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { - - var builder = confirmationAddressBuilder(); - - if(sender.getEMailAddress() != null) { - builder.withEmail(convert(sender.getEMailAddress())) - .withType(sender.getEMailAddress().getType()); - } else { - builder.withWebserviceURL(convert(sender.getWebserviceURL())) - .withType(sender.getWebserviceURL().getType()); - } - - return builder.build(); - } - - private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.EMailAddress eMailAddress) { - return internetAddressTypeBuilder() - .withAddress(eMailAddress.getValue()) - .build(); - } - - private WebserviceURL convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.WebserviceURL webserviceURL) { - return webserviceURLBuilder() - .withAddress(webserviceURL.getValue()) - .withAlternativeEmail(webserviceURL.getAlternativeEmail()) - .build(); - } - - - //------------- RECEIVER ------------------ - - private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { - return setupReceiverBuilder(receiver).build(); - } - - private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver, - IdentificationType identificationType) { - return setupReceiverBuilder(receiver) - .withIdentification(identificationType) - .build(); - } - - private Receiver.ReceiverBuilder setupReceiverBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { - var builder = receiverBuilder() - .withPreAdviceNote(receiver.getPreAdviceNote()) - .withAdditionalCriteria(receiver.getAdditionalCriteria()) - .withPerson(convert(receiver.getPerson())) - .withAddress(convertAddresses(receiver.getAddress())); - - if (receiver.getIdentification() != null) builder.withIdentification(convert(receiver.getIdentification())); - - return builder; - - } - - //------------- ADDRESSES ------------------ - - private List> convertAddresses( - List> addresses) { - return addresses.stream() - .map(JAXBElement::getValue) - .map(this::convert) - .map(personFactory::createAddress) - .collect(toList()); - } - - private AbstractAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.AbstractAddressType address) { - if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType) address); - } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType) address); - } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) address); - } else { - throw new IllegalArgumentException(format("No conversion strategy of address of type {0}", address.getClass().toGenericString())); - } - } - - private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType address) { - return internetAddressTypeBuilder() - .withAddress(address.getAddress()) - .withId(address.getId()) - .build(); - } - - private PostalAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType address) { - return postalAddressTypeBuilder() - .withCountryCode(address.getCountryCode()) - .withDeliveryAddress(convert(address.getDeliveryAddress())) - .withId(address.getId()) - .withMunicipality(address.getMunicipality()) - .withMunicipalityNumber(address.getMunicipalityNumber()) - .withPostalCode(address.getPostalCode()) - .withType(address.getType()) - .build(); - } - - private PostalAddressType.DeliveryAddress convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType.DeliveryAddress deliveryAddress) { - return deliveryAddressBuilder() - .withBuildingNumber(deliveryAddress.getBuildingNumber()) - .withDoorNumber(deliveryAddress.getDoorNumber()) - .withStreetName(deliveryAddress.getStreetName()) - .withUnit(deliveryAddress.getUnit()) - .build(); - } - - private TelephoneAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType address) { - return telephoneAddressTypeBuilder() - .withId(address.getId()) - .withMessengerService(address.getMessengerService()) - .withNumber(address.getNumber().getFormattedNumber()) - .build(); - } - - - //------------- ATTACHMENTS ------------------ - - private AttachmentsType convertPayloads(List payloadList) { - var builder = attachmentsTypeBuilder(); - - payloadList.stream() - .map(this::convert) - .forEach(builder::addAttachment); - - return builder.build(); - } - - private AttachmentType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload payload) { - - return attachmentTypeBuilder() - .withFileName(payload.getFileName()) - .withContent(payload.getBinaryDocument()) - .withCheckSum(payload.getCheckSum()) - .withURL(payload.getDocumentReference()) - .withDocumentClass(payload.getDocumentClass()) - .withMimeType(payload.getMIMEType()) - .withSize(payload.getSize()) - .build(); - } - - //------------- IDENTIFICATION TYPE ------------- - - private IdentificationType convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType identification) { - return identificationTypeBuilder() - .withId(identification.getId()) - .withType(identification.getType()) - .withValue(convert(identification.getValue())) - .build(); - } - - private IdentificationType.Value convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.Value value) { - return valueBuilder() - .withId(value.getId()) - .withValue(value.getValue()) - .build(); - } - - //------------ PERSONS -------------------- - - private JAXBElement convert( - JAXBElement jaxbPerson) { - - var mzsPerson = jaxbPerson.getValue(); - - Assert.isTrue(personIsPhysical(mzsPerson) || personIsCorporateBody(mzsPerson), - "Person is neither a PhysicalPerson nor a CorporateBody."); - - var msgPerson = (personIsPhysical(mzsPerson)) - ? convert((at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType) mzsPerson) - : convert((at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType) mzsPerson); - - return personFactory.createPerson(msgPerson); - - } - - private boolean personIsPhysical(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { - return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType; - } - - private boolean personIsCorporateBody(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { - return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType; - } - - private CorporateBodyType convert(at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType corporateBody) { - return corporateBodyTypeBuilder() - .withFullName(corporateBody.getFullName()) - .withId(corporateBody.getId()) - .withOrganization(corporateBody.getOrganization()) - .withTarget(corporateBody.getTarget()) - .build(); - } - - private PhysicalPersonType convert(at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType physicalPerson) { - return physicalPersonTypeBuilder() - .withDateOfBirth(physicalPerson.getDateOfBirth()) - .withId(physicalPerson.getId()) - .withName(convert(physicalPerson.getName())) - .build(); - } - - private PersonNameType convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType name) { - return personNameTypeBuilder() - .withGivenName(name.getGivenName()) - .withFamilyName(convert(name.getFamilyName())) - .withAffix(convertAffixes(name.getAffix())) - .build(); - } - - private FamilyName convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.FamilyName familyName) { - return familyNameBuilder() - .withPrefix(familyName.getPrefix()) - .withPrimary(familyName.getPrimary()) - .withValue(familyName.getValue()) - .build(); - } - - private List convertAffixes(List affix) { - return affix.stream() - .map(mzsAffix -> convert(mzsAffix)) - .collect(toList()); - } - - private Affix convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.Affix affix) { - return affixBuilder() - .withPosition(affix.getPosition()) - .withType(affix.getType()) - .withValue(affix.getValue()) - .build(); - } - -} diff --git a/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java b/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java index 6fdd128..62e2f83 100644 --- a/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java @@ -1,9 +1,8 @@ package at.gv.egiz.moazs.config; -import at.gv.egiz.moazs.Marshaller; -import at.gv.egiz.moazs.MoaZSPrefixMapper; +import at.gv.egiz.moazs.scheme.Marshaller; +import at.gv.egiz.moazs.scheme.MoaZSPrefixMapper; import com.sun.xml.bind.marshaller.NamespacePrefixMapper; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; diff --git a/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java index e2b219a..b93bb35 100644 --- a/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java +++ b/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java @@ -2,7 +2,7 @@ package at.gv.egiz.moazs.pipeline; import at.gv.egiz.moazs.MsgClient; -import at.gv.egiz.moazs.Mzs2MsgConverter; +import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.egiz.moazs.TnvzClient; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; diff --git a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java index 7384b12..c514210 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java @@ -1,6 +1,6 @@ package at.gv.egiz.moazs.repository; -import at.gv.egiz.moazs.Marshaller; +import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.app2mzs.xsd.ObjectFactory; diff --git a/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java new file mode 100644 index 0000000..2dbb6a0 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java @@ -0,0 +1,33 @@ +package at.gv.egiz.moazs.scheme; + +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +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 + */ +public class Marshaller { + + private final Jaxb2Marshaller marshaller; + + public Marshaller(Jaxb2Marshaller marshaller) { + this.marshaller = 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/scheme/MoaZSPrefixMapper.java b/src/main/java/at/gv/egiz/moazs/scheme/MoaZSPrefixMapper.java new file mode 100644 index 0000000..faee8a5 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/scheme/MoaZSPrefixMapper.java @@ -0,0 +1,54 @@ +package at.gv.egiz.moazs.scheme; + +import com.sun.xml.bind.marshaller.NamespacePrefixMapper; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class MoaZSPrefixMapper extends NamespacePrefixMapper { + + private final Map map = new HashMap<>(); + + public MoaZSPrefixMapper() { + map.put(getMsgNamespaceUri(), "msg"); + map.put(getMsgPNamespaceUri(), "msgp"); + map.put(getMzsNamespaceUri(), "mzs"); + map.put(getMzsPNamespaceUri(), "mzsp"); + map.put(getDsigNamespaceUri(), "dsig"); + } + + public MoaZSPrefixMapper(boolean isMzs) { + this(); + map.put((isMzs) ? getMzsPNamespaceUri() : getMsgPNamespaceUri(), "p"); + map.put((isMzs) ? getMzsNamespaceUri() : getMsgNamespaceUri(), ""); + } + + @Override + public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { + return map.getOrDefault(namespaceUri, suggestion); + } + + private String getMsgNamespaceUri() { + return new at.gv.zustellung.msg.xsd.ObjectFactory().createDeliveryRequest(null).getName().getNamespaceURI(); + } + + private String getMsgPNamespaceUri() { + return new at.gv.zustellung.msg.xsd.persondata.ObjectFactory().createPerson(null).getName().getNamespaceURI(); + } + + private String getMzsNamespaceUri() { + return new at.gv.zustellung.app2mzs.xsd.ObjectFactory().createDeliveryRequest(null).getName().getNamespaceURI(); + } + + private String getMzsPNamespaceUri() { + return new at.gv.zustellung.app2mzs.xsd.persondata.ObjectFactory().createAbstractPersonData(null).getName().getNamespaceURI(); + } + + private String getDsigNamespaceUri() { + return new org.w3._2000._09.xmldsig_.ObjectFactory().createCanonicalizationMethod(null).getName().getNamespaceURI(); + } + + +} diff --git a/src/main/java/at/gv/egiz/moazs/scheme/Mzs2MsgConverter.java b/src/main/java/at/gv/egiz/moazs/scheme/Mzs2MsgConverter.java new file mode 100644 index 0000000..912ee4f --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/scheme/Mzs2MsgConverter.java @@ -0,0 +1,321 @@ +package at.gv.egiz.moazs.scheme; + +import at.gv.zustellung.msg.xsd.*; +import at.gv.zustellung.msg.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; +import at.gv.zustellung.msg.xsd.persondata.*; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; + +import javax.xml.bind.JAXBElement; +import java.util.List; + +import static at.gv.zustellung.msg.xsd.AttachmentType.attachmentTypeBuilder; +import static at.gv.zustellung.msg.xsd.AttachmentsType.attachmentsTypeBuilder; +import static at.gv.zustellung.msg.xsd.ConfirmationAddress.confirmationAddressBuilder; +import static at.gv.zustellung.msg.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; +import static at.gv.zustellung.msg.xsd.Receiver.receiverBuilder; +import static at.gv.zustellung.msg.xsd.Sender.senderBuilder; +import static at.gv.zustellung.msg.xsd.SenderCorporateBodyType.senderCorporateBodyTypeBuilder; +import static at.gv.zustellung.msg.xsd.WebserviceURL.webserviceURLBuilder; +import static at.gv.zustellung.msg.xsd.persondata.CorporateBodyType.corporateBodyTypeBuilder; +import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.Value.valueBuilder; +import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.identificationTypeBuilder; +import static at.gv.zustellung.msg.xsd.persondata.InternetAddressType.internetAddressTypeBuilder; +import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.*; +import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.Affix.affixBuilder; +import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.FamilyName.familyNameBuilder; +import static at.gv.zustellung.msg.xsd.persondata.PhysicalPersonType.physicalPersonTypeBuilder; +import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.DeliveryAddress.deliveryAddressBuilder; +import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.postalAddressTypeBuilder; +import static at.gv.zustellung.msg.xsd.persondata.TelephoneAddressType.telephoneAddressTypeBuilder; +import static java.lang.String.format; +import static java.util.stream.Collectors.toList; + + +@Component +public class Mzs2MsgConverter { + + private final at.gv.zustellung.msg.xsd.ObjectFactory msgFactory = new at.gv.zustellung.msg.xsd.ObjectFactory(); + private final at.gv.zustellung.msg.xsd.persondata.ObjectFactory personFactory = new at.gv.zustellung.msg.xsd.persondata.ObjectFactory(); + + public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { + return setupBuilder(mzsRequest) + .withReceiver(convert(mzsRequest.getReceiver())) + .build(); + } + + public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest, + IdentificationType identification) { + return setupBuilder(mzsRequest) + .withReceiver(convert(mzsRequest.getReceiver(), identification)) + .build(); + } + + private DeliveryRequestTypeBuilder setupBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { + return deliveryRequestTypeBuilder() + .withVersion("2.0.0") + .withSender(convert(mzsRequest.getSender())) + .withMetaData(mzsRequest.getMetaData()) + .withAttachments(convertPayloads(mzsRequest.getPayload())); + } + + //------------- SENDER ------------------ + + private Sender convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { + return senderBuilder() + .withSenderPerson(extractSenderPerson(sender)) + .withLogo(sender.getLogo()) + .withAdditionalCriteria(sender.getAdditionalCriteria()) + .withConfirmationAddress(convertConfirmationAddress(sender)) + .build(); + } + + private JAXBElement extractSenderPerson(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { + + Assert.isTrue(sender.getSenderProfile() != null || sender.getCorporateBody() != null, + "Neither SenderProfile nor CorporateBody are given"); + + var builder = senderCorporateBodyTypeBuilder(); + + if(sender.getSenderProfile() != null) { + builder.withSenderProfile(sender.getSenderProfile()); + } else { + var identification = sender.getCorporateBody().getIdentification().get(0); + builder.withCorporateBody(convert(sender.getCorporateBody())) + .withIdentification(convert(identification)); + } + + builder.withId(sender.getId()); + + return msgFactory.createSenderCorporateBody(builder.build()); + + } + + private ConfirmationAddress convertConfirmationAddress(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { + + var builder = confirmationAddressBuilder(); + + if(sender.getEMailAddress() != null) { + builder.withEmail(convert(sender.getEMailAddress())) + .withType(sender.getEMailAddress().getType()); + } else { + builder.withWebserviceURL(convert(sender.getWebserviceURL())) + .withType(sender.getWebserviceURL().getType()); + } + + return builder.build(); + } + + private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.EMailAddress eMailAddress) { + return internetAddressTypeBuilder() + .withAddress(eMailAddress.getValue()) + .build(); + } + + private WebserviceURL convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.WebserviceURL webserviceURL) { + return webserviceURLBuilder() + .withAddress(webserviceURL.getValue()) + .withAlternativeEmail(webserviceURL.getAlternativeEmail()) + .build(); + } + + + //------------- RECEIVER ------------------ + + private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { + return setupReceiverBuilder(receiver).build(); + } + + private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver, + IdentificationType identificationType) { + return setupReceiverBuilder(receiver) + .withIdentification(identificationType) + .build(); + } + + private Receiver.ReceiverBuilder setupReceiverBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { + var builder = receiverBuilder() + .withPreAdviceNote(receiver.getPreAdviceNote()) + .withAdditionalCriteria(receiver.getAdditionalCriteria()) + .withPerson(convert(receiver.getPerson())) + .withAddress(convertAddresses(receiver.getAddress())); + + if (receiver.getIdentification() != null) builder.withIdentification(convert(receiver.getIdentification())); + + return builder; + + } + + //------------- ADDRESSES ------------------ + + private List> convertAddresses( + List> addresses) { + return addresses.stream() + .map(JAXBElement::getValue) + .map(this::convert) + .map(personFactory::createAddress) + .collect(toList()); + } + + private AbstractAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.AbstractAddressType address) { + if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType) address); + } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType) address); + } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) address); + } else { + throw new IllegalArgumentException(format("No conversion strategy of address of type {0}", address.getClass().toGenericString())); + } + } + + private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType address) { + return internetAddressTypeBuilder() + .withAddress(address.getAddress()) + .withId(address.getId()) + .build(); + } + + private PostalAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType address) { + return postalAddressTypeBuilder() + .withCountryCode(address.getCountryCode()) + .withDeliveryAddress(convert(address.getDeliveryAddress())) + .withId(address.getId()) + .withMunicipality(address.getMunicipality()) + .withMunicipalityNumber(address.getMunicipalityNumber()) + .withPostalCode(address.getPostalCode()) + .withType(address.getType()) + .build(); + } + + private PostalAddressType.DeliveryAddress convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType.DeliveryAddress deliveryAddress) { + return deliveryAddressBuilder() + .withBuildingNumber(deliveryAddress.getBuildingNumber()) + .withDoorNumber(deliveryAddress.getDoorNumber()) + .withStreetName(deliveryAddress.getStreetName()) + .withUnit(deliveryAddress.getUnit()) + .build(); + } + + private TelephoneAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType address) { + return telephoneAddressTypeBuilder() + .withId(address.getId()) + .withMessengerService(address.getMessengerService()) + .withNumber(address.getNumber().getFormattedNumber()) + .build(); + } + + + //------------- ATTACHMENTS ------------------ + + private AttachmentsType convertPayloads(List payloadList) { + var builder = attachmentsTypeBuilder(); + + payloadList.stream() + .map(this::convert) + .forEach(builder::addAttachment); + + return builder.build(); + } + + private AttachmentType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload payload) { + + return attachmentTypeBuilder() + .withFileName(payload.getFileName()) + .withContent(payload.getBinaryDocument()) + .withCheckSum(payload.getCheckSum()) + .withURL(payload.getDocumentReference()) + .withDocumentClass(payload.getDocumentClass()) + .withMimeType(payload.getMIMEType()) + .withSize(payload.getSize()) + .build(); + } + + //------------- IDENTIFICATION TYPE ------------- + + private IdentificationType convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType identification) { + return identificationTypeBuilder() + .withId(identification.getId()) + .withType(identification.getType()) + .withValue(convert(identification.getValue())) + .build(); + } + + private IdentificationType.Value convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.Value value) { + return valueBuilder() + .withId(value.getId()) + .withValue(value.getValue()) + .build(); + } + + //------------ PERSONS -------------------- + + private JAXBElement convert( + JAXBElement jaxbPerson) { + + var mzsPerson = jaxbPerson.getValue(); + + Assert.isTrue(personIsPhysical(mzsPerson) || personIsCorporateBody(mzsPerson), + "Person is neither a PhysicalPerson nor a CorporateBody."); + + var msgPerson = (personIsPhysical(mzsPerson)) + ? convert((at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType) mzsPerson) + : convert((at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType) mzsPerson); + + return personFactory.createPerson(msgPerson); + + } + + private boolean personIsPhysical(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { + return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType; + } + + private boolean personIsCorporateBody(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { + return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType; + } + + private CorporateBodyType convert(at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType corporateBody) { + return corporateBodyTypeBuilder() + .withFullName(corporateBody.getFullName()) + .withId(corporateBody.getId()) + .withOrganization(corporateBody.getOrganization()) + .withTarget(corporateBody.getTarget()) + .build(); + } + + private PhysicalPersonType convert(at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType physicalPerson) { + return physicalPersonTypeBuilder() + .withDateOfBirth(physicalPerson.getDateOfBirth()) + .withId(physicalPerson.getId()) + .withName(convert(physicalPerson.getName())) + .build(); + } + + private PersonNameType convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType name) { + return personNameTypeBuilder() + .withGivenName(name.getGivenName()) + .withFamilyName(convert(name.getFamilyName())) + .withAffix(convertAffixes(name.getAffix())) + .build(); + } + + private FamilyName convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.FamilyName familyName) { + return familyNameBuilder() + .withPrefix(familyName.getPrefix()) + .withPrimary(familyName.getPrimary()) + .withValue(familyName.getValue()) + .build(); + } + + private List convertAffixes(List affix) { + return affix.stream() + .map(mzsAffix -> convert(mzsAffix)) + .collect(toList()); + } + + private Affix convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.Affix affix) { + return affixBuilder() + .withPosition(affix.getPosition()) + .withType(affix.getType()) + .withValue(affix.getValue()) + .build(); + } + +} -- cgit v1.2.3