diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-08 13:41:27 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-08 13:41:27 +0200 |
commit | 6f450a1d3ff60557b1081f9e8ef6bd5f375db842 (patch) | |
tree | 336068552025782ec3f9716999fb3c391fbed9ea /src/main/java/at/gv/egiz/moazs/scheme | |
parent | 36fd031d1f0218e323b50e6d63eff4a7cee99cd7 (diff) | |
download | moa-zs-6f450a1d3ff60557b1081f9e8ef6bd5f375db842.tar.gz moa-zs-6f450a1d3ff60557b1081f9e8ef6bd5f375db842.tar.bz2 moa-zs-6f450a1d3ff60557b1081f9e8ef6bd5f375db842.zip |
Refactor: Consolidate Schema-Related Components
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/scheme')
3 files changed, 408 insertions, 0 deletions
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 <T> String marshallXml(final T obj) { + StringWriter sw = new StringWriter(); + Result result = new StreamResult(sw); + marshaller.marshal(obj, result); + return sw.toString(); + } + + public <T> 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<String, String> 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<? extends AbstractSenderPersonType> 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<JAXBElement<? extends AbstractAddressType>> convertAddresses( + List<JAXBElement<? extends at.gv.zustellung.app2mzs.xsd.persondata.AbstractAddressType>> 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<at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload> 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<? extends AbstractPersonType> convert( + JAXBElement<? extends at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType> 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<Affix> convertAffixes(List<at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.Affix> 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(); + } + +} |