From 9a038cf18d6ebd4c68218c20ebc174b779afa7fa Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 29 Apr 2019 14:28:48 +0200 Subject: Make JAXB Objects Immutable and Add Builder Utility - Use Immutable-XJC from https://github.com/sabomichal/immutable-xjc - Update existing JAXB-object manipulations to use the Builder - Remove jaxbMarshallerTest (as it relies on mutable JAXB interfaces) Reason for making JAXB objects immutable: I put JAXB objects in the repository. Depending on the configuration, the repository can be a simple hashmap; in that case I might end up sharing JAXB objects between threads. Immutable shared object should make life a little simpler. --- pom.xml | 14 ++- src/main/java/at/gv/egiz/moazs/App2MzsService.java | 43 +++++---- .../java/at/gv/egiz/moazs/App2MzsServiceTest.java | 18 ++-- .../java/at/gv/egiz/moazs/JaxbMarshallerTest.java | 101 --------------------- 4 files changed, 49 insertions(+), 127 deletions(-) delete mode 100644 src/test/java/at/gv/egiz/moazs/JaxbMarshallerTest.java diff --git a/pom.xml b/pom.xml index 6b27f3b..9c963c9 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,7 @@ 1.1 2.9.3 27.1-jre + 1.5 @@ -96,6 +97,13 @@ org.apache.cxf cxf-codegen-plugin ${cxf.version} + + + com.github.sabomichal + immutable-xjc-plugin + ${immutable-xjc-plugin.version} + + generate-sources @@ -106,8 +114,12 @@ ${project.basedir}/src/main/resources/mzs/app2mzs.wsdl classpath:wsdl/app2mzs.wsdl + + -xjc-immutable + -xjc-imm-builder + - + ${project.basedir}/src/main/resources/zusemsg/app2zuse_p2.wsdl classpath:wsdl/app2zuse_p2.wsdl diff --git a/src/main/java/at/gv/egiz/moazs/App2MzsService.java b/src/main/java/at/gv/egiz/moazs/App2MzsService.java index 8626c24..66ab795 100644 --- a/src/main/java/at/gv/egiz/moazs/App2MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/App2MzsService.java @@ -1,9 +1,16 @@ package at.gv.egiz.moazs; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryAnswerType; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryAnswerType.DeliveryAnswerTypeBuilder; import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType.DeliveryRequestStatusTypeBuilder; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType.Error.ErrorBuilder; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ErrorInfoType; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ErrorInfoType.ErrorInfoTypeBuilder; import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs.App2MzsPortType; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType.DeliveryRequestTypeBuilder; import at.gv.egiz.moazs.pipeline.DeliveryPipeline; import at.gv.egiz.moazs.repository.DeliveryRepository; import org.slf4j.Logger; @@ -66,32 +73,30 @@ public class App2MzsService implements App2MzsPortType { private DeliveryRequestStatusType generatePartialSuccessResponse(String appDeliveryId, String message) { - var partialSuccess = objectFactory.createDeliveryAnswerType(); - partialSuccess.setAppDeliveryID(appDeliveryId); + var answer = new DeliveryAnswerTypeBuilder() + .withAppDeliveryID(appDeliveryId) + .build(); - var response = objectFactory.createDeliveryRequestStatusType(); - response.setPartialSuccess(partialSuccess); - - response.setSignature(null); - - return response; + return new DeliveryRequestStatusTypeBuilder() + .withPartialSuccess(answer) + .build(); } private DeliveryRequestStatusType generateErrorResponse(String appDeliveryId, String message) { - var error = objectFactory.createDeliveryRequestStatusTypeError(); - error.setAppDeliveryID(appDeliveryId); - - var info = objectFactory.createErrorInfoType(); - info.setCode("500"); - info.setText(message); - error.setErrorInfo(info); + var info = new ErrorInfoTypeBuilder() + .withCode("500") + .withText(message) + .build(); - var response = objectFactory.createDeliveryRequestStatusType(); - response.setError(error); - response.setSignature(null); + var error = new ErrorBuilder() + .withAppDeliveryID(appDeliveryId) + .withErrorInfo(info) + .build(); - return response; + return new DeliveryRequestStatusTypeBuilder() + .withError(error) + .build(); } private DeliveryRequestStatusType process(DeliveryRequestType deliveryRequest) throws RuntimeException { diff --git a/src/test/java/at/gv/egiz/moazs/App2MzsServiceTest.java b/src/test/java/at/gv/egiz/moazs/App2MzsServiceTest.java index 01b7745..5579f06 100644 --- a/src/test/java/at/gv/egiz/moazs/App2MzsServiceTest.java +++ b/src/test/java/at/gv/egiz/moazs/App2MzsServiceTest.java @@ -1,5 +1,8 @@ package at.gv.egiz.moazs; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType.DeliveryRequestStatusTypeBuilder; +import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.DeliveryRequestStatusType.Success.SuccessBuilder; import at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory; import at.gv.egiz.moazs.pipeline.DeliveryPipeline; import at.gv.egiz.moazs.repository.DeliveryRepository; @@ -42,16 +45,19 @@ public class App2MzsServiceTest { @Autowired private DeliveryRepository repository; - private final ObjectFactory factory = new ObjectFactory(); - @Bean @Primary public DeliveryPipeline deliveryPipelineThatAlwaysSucceeds() { return appDeliveryId -> { - var status = factory.createDeliveryRequestStatusType(); - var success = factory.createDeliveryRequestStatusTypeSuccess(); - success.setAppDeliveryID(appDeliveryId); - status.setSuccess(success); + + var success = new SuccessBuilder() + .withAppDeliveryID(appDeliveryId) + .build(); + + var status = new DeliveryRequestStatusTypeBuilder() + .withSuccess(success) + .build(); + repository.add(status); }; } diff --git a/src/test/java/at/gv/egiz/moazs/JaxbMarshallerTest.java b/src/test/java/at/gv/egiz/moazs/JaxbMarshallerTest.java deleted file mode 100644 index e85a5ba..0000000 --- a/src/test/java/at/gv/egiz/moazs/JaxbMarshallerTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import java.math.BigInteger; -import java.nio.charset.Charset; -import java.util.GregorianCalendar; - -public class JaxbMarshallerTest { - - private static final Logger logger = LoggerFactory.getLogger(JaxbMarshallerTest.class); - - private Marshaller createMarshaller() throws JAXBException { - JAXBContext context = 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); - - var m = context.createMarshaller(); - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - return m; - - } - - private JAXBElement generateSampleRequest() throws DatatypeConfigurationException { - var pof = new at.gv.e_government.reference.namespace.zustellung.mzs.persondata_.ObjectFactory(); - var mzsof = new at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory(); - var msgof = new at.gv.e_government.reference.namespace.zustellung.msg.phase2._20181206_.ObjectFactory(); - - var deliveryRequest = new DeliveryRequestType(); - - var sender = mzsof.createDeliveryRequestTypeSender(); - var corporateBody = pof.createCorporateBodyType(); - corporateBody.setFullName("Bundesministerium für Testzwecke"); - sender.setPerson(pof.createCorporateBody(corporateBody)); - deliveryRequest.setSender(sender); - - var receiver = mzsof.createDeliveryRequestTypeReceiver(); - var physicalPerson = pof.createPhysicalPersonType(); - var name = pof.createPersonNameType(); - name.setGivenName("Maxi"); - var familyName = pof.createPersonNameTypeFamilyName(); - familyName.setValue("Mustermann1"); - name.setFamilyName(familyName); - physicalPerson.setName(name); - var cal = new GregorianCalendar(1984, 1, 24); - var xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); - physicalPerson.setDateOfBirth(pof.createDateOfBirth(xmlDate).getValue()); - receiver.setPerson(pof.createPhysicalPerson(physicalPerson)); - var postal = pof.createPostalAddressType(); - postal.setCountryCode("AT"); - postal.setPostalCode("1010"); - postal.setMunicipality("Wien"); - var deliveryAddress = pof.createPostalAddressTypeDeliveryAddress(); - deliveryAddress.setStreetName("Musterstraße"); - deliveryAddress.setBuildingNumber("10"); - postal.setDeliveryAddress(deliveryAddress); - receiver.getAddress().add(pof.createAddress(postal)); - deliveryRequest.setReceiver(receiver); - - var metadata = msgof.createMetaData(); - metadata.setAppDeliveryID("asd"); - metadata.setSubject("Wichtige Mitteilung!"); - metadata.setDeliveryQuality("RSa"); - deliveryRequest.setMetaData(metadata); - - var payload = mzsof.createDeliveryRequestTypePayload(); - payload.setDocumentReference("https://authority.gv.at/files/73bdf969781ba41fa07df1ff8439cf685c0db1c3"); - payload.setFileName("brief.xml"); - payload.setMIMEType("text/xml"); - payload.setSize(BigInteger.valueOf(123401)); - var checksum = msgof.createCheckSumType(); - checksum.setAlgorithmID("SHA1"); - checksum.setValue("random".getBytes(Charset.forName("UTF-8"))); - payload.setCheckSum(checksum); - deliveryRequest.getPayload().add(payload); - - return mzsof.createDeliveryRequest(deliveryRequest); - } - - @Test - public void testMarshalling() throws JAXBException, DatatypeConfigurationException { - - logger.info("test marshalling...."); - var request = generateSampleRequest(); - var m = createMarshaller(); - m.marshal(request, System.out); - - } - -} -- cgit v1.2.3