From c93be745e1c62c1f3fb5995197d2e0f75df81525 Mon Sep 17 00:00:00 2001 From: Andrea De Rinaldis Date: Tue, 26 Mar 2024 20:16:29 +0100 Subject: [PATCH] [NOD-781] feat: aligned some classes wrongly merged --- .../service/ConverterService.java | 3 +- .../service/NAVGeneratorService.java | 19 ++- .../service/UnmarshallerService.java | 142 ------------------ .../wispconverter/util/JaxbElementUtil.java | 31 ++-- 4 files changed, 28 insertions(+), 167 deletions(-) delete mode 100644 src/main/java/it/gov/pagopa/wispconverter/service/UnmarshallerService.java diff --git a/src/main/java/it/gov/pagopa/wispconverter/service/ConverterService.java b/src/main/java/it/gov/pagopa/wispconverter/service/ConverterService.java index be53f2eb..57e93394 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/service/ConverterService.java +++ b/src/main/java/it/gov/pagopa/wispconverter/service/ConverterService.java @@ -10,6 +10,7 @@ import it.gov.digitpa.schemas._2011.pagamenti.CtRichiestaPagamentoTelematico; import it.gov.pagopa.wispconverter.entity.Primitive; import it.gov.pagopa.wispconverter.entity.RPTRequestEntity; +import it.gov.pagopa.wispconverter.exception.AppException; import it.gov.pagopa.wispconverter.exception.conversion.ConversionException; import it.gov.pagopa.wispconverter.model.client.gpd.MultiplePaymentPosition; import it.gov.pagopa.wispconverter.model.client.gpd.PaymentPosition; @@ -138,7 +139,7 @@ private RPTRequest extractRPTRequest(RPTRequestEntity rptRequestEntity) throws C default -> throw new ConversionException(String.format("Unable to unmarshall RPT header or body. No valid parsing process was defined for the primitive [%s].", primitive)); } - } catch (IOException e) { + } catch (IOException | AppException e) { throw new ConversionException("Unable to unmarshall Envelope content. ", e); } diff --git a/src/main/java/it/gov/pagopa/wispconverter/service/NAVGeneratorService.java b/src/main/java/it/gov/pagopa/wispconverter/service/NAVGeneratorService.java index 7bdf5bf8..97608c64 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/service/NAVGeneratorService.java +++ b/src/main/java/it/gov/pagopa/wispconverter/service/NAVGeneratorService.java @@ -5,21 +5,28 @@ import it.gov.pagopa.wispconverter.exception.conversion.ConversionException; import it.gov.pagopa.wispconverter.model.client.iuvgenerator.IUVGeneratorRequest; import it.gov.pagopa.wispconverter.model.client.iuvgenerator.IUVGeneratorResponse; -import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service -@RequiredArgsConstructor +@Slf4j public class NAVGeneratorService { private final IUVGeneratorClient iuvGeneratorClient; - @Value("${wisp-converter.aux-digit}") - private String auxDigit; + private final String auxDigit; - @Value("${wisp-converter.segregation-code}") - private String segregationCode; + private final String segregationCode; + + public NAVGeneratorService(@Autowired IUVGeneratorClient iuvGeneratorClient, + @Value("${wisp-converter.aux-digit}") String auxDigit, + @Value("${wisp-converter.segregation-code}") String segregationCode) { + this.iuvGeneratorClient = iuvGeneratorClient; + this.auxDigit = auxDigit; + this.segregationCode = segregationCode; + } public String getNAVCodeFromIUVGenerator(String creditorInstitutionCode) throws ConversionException { // generating request body diff --git a/src/main/java/it/gov/pagopa/wispconverter/service/UnmarshallerService.java b/src/main/java/it/gov/pagopa/wispconverter/service/UnmarshallerService.java deleted file mode 100644 index 311b802a..00000000 --- a/src/main/java/it/gov/pagopa/wispconverter/service/UnmarshallerService.java +++ /dev/null @@ -1,142 +0,0 @@ -package it.gov.pagopa.wispconverter.service; - -import gov.telematici.pagamenti.ws.NodoInviaCarrelloRPT; -import gov.telematici.pagamenti.ws.NodoInviaRPT; -import gov.telematici.pagamenti.ws.TipoElementoListaRPT; -import gov.telematici.pagamenti.ws.ppthead.IntestazioneCarrelloPPT; -import gov.telematici.pagamenti.ws.ppthead.IntestazionePPT; -import it.gov.digitpa.schemas._2011.pagamenti.CtRichiestaPagamentoTelematico; -import it.gov.pagopa.wispconverter.entity.Primitive; -import it.gov.pagopa.wispconverter.entity.RPTRequestEntity; -import it.gov.pagopa.wispconverter.exception.conversion.ConversionException; -import it.gov.pagopa.wispconverter.model.unmarshall.RPTContent; -import it.gov.pagopa.wispconverter.model.unmarshall.RPTRequest; -import jakarta.xml.bind.JAXBContext; -import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Unmarshaller; -import org.bouncycastle.util.encoders.Base64; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import javax.xml.parsers.DocumentBuilder; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.Arrays; - -@Service -public class UnmarshallerService { - - private final DocumentBuilder docBuilder; - private final JAXBContext nodoInviaRPTBodyContext; - private final JAXBContext nodoInviaCarrelloRPTBodyContext; - private final JAXBContext intestazionePPTContext; - private final JAXBContext intestazioneCarrelloPPTContext; - private final JAXBContext rptContext; - - - public UnmarshallerService(@Autowired DocumentBuilder documentBuilder) throws JAXBException { - this.intestazionePPTContext = JAXBContext.newInstance(IntestazionePPT.class); - this.intestazioneCarrelloPPTContext = JAXBContext.newInstance(IntestazioneCarrelloPPT.class); - this.nodoInviaRPTBodyContext = JAXBContext.newInstance(NodoInviaRPT.class); - this.nodoInviaCarrelloRPTBodyContext = JAXBContext.newInstance(NodoInviaCarrelloRPT.class); - this.rptContext = JAXBContext.newInstance(CtRichiestaPagamentoTelematico.class); - this.docBuilder = documentBuilder; - } - - @SuppressWarnings({"rawtypes"}) - public RPTRequest unmarshall(RPTRequestEntity entity) throws ConversionException { - Primitive primitive = Primitive.fromString(entity.getPrimitive()); - if (primitive == null) { - throw new ConversionException(String.format("Unable to unmarshall RPT header or body. The string object refers to a primitive that is not handled by this service. Use one of the following: [%s]", Arrays.asList(Primitive.values()))); - } - - RPTRequest response; - switch (primitive) { - case NODO_INVIA_RPT -> response = unmarshallNodoInviaRPT(entity); - case NODO_INVIA_CARRELLO_RPT -> response = unmarshallNodoInviaCarrelloRPT(entity); - default -> - throw new ConversionException(String.format("Unable to unmarshall RPT header or body. No valid parsing process was defined for the primitive [%s].", primitive)); - } - return response; - } - - @SuppressWarnings({"rawtypes"}) - public CtRichiestaPagamentoTelematico unmarshall(RPTContent rptContent) throws ConversionException { - CtRichiestaPagamentoTelematico rpt; - try { - // extracting byte array containing Base64 of RPT - byte[] base64Content; - Object wrapper = rptContent.getWrappedRPT(); - if (wrapper instanceof NodoInviaRPT nodoInviaRPT) { - base64Content = nodoInviaRPT.getRpt(); - } else if (wrapper instanceof TipoElementoListaRPT tipoElementoListaRPT) { - base64Content = tipoElementoListaRPT.getRpt(); - } else { - throw new ConversionException("Unable to unmarshall RPT content for CtRichiestaPagamentoTelematico. Invalid class for RPT wrapper."); - } - // converting Base64 to XML string - byte[] content = Base64.decode(new String(base64Content)); - // unmarshalling header content - Unmarshaller rptUnmarshaller = rptContext.createUnmarshaller(); - Document rptDoc = this.docBuilder.parse(new ByteArrayInputStream(content)); - rpt = rptUnmarshaller.unmarshal(rptDoc, CtRichiestaPagamentoTelematico.class).getValue(); - } catch (JAXBException | IOException | SAXException e) { - throw new ConversionException("Unable to unmarshall RPT content for CtRichiestaPagamentoTelematico. ", e); - } - return rpt; - } - - private RPTRequest unmarshallNodoInviaRPT(RPTRequestEntity entity) throws ConversionException { - RPTRequest rptRequest = null; - /* - try { - // unmarshalling header content - Unmarshaller intestazionePPTUnmarshaller = intestazionePPTContext.createUnmarshaller(); - Document intestazionePPTDoc = this.docBuilder.parse(new ByteArrayInputStream(entity.getHeader().getBytes())); - IntestazionePPT header = intestazionePPTUnmarshaller.unmarshal(intestazionePPTDoc, IntestazionePPT.class).getValue(); - - // unmarshalling body content - Unmarshaller nodoInviaRPTBodyUnmarshaller = nodoInviaRPTBodyContext.createUnmarshaller(); - Document nodoInviaRPTBodyDoc = this.docBuilder.parse(new ByteArrayInputStream(entity.getBody().getBytes())); - NodoInviaRPT body = nodoInviaRPTBodyUnmarshaller.unmarshal(nodoInviaRPTBodyDoc, NodoInviaRPT.class).getValue(); - - // generating complete response - rptRequest = RPTRequest.builder() - .body(body) - .header(header) - .build(); - } catch (JAXBException | IOException | SAXException e) { - throw new ConversionException("Unable to unmarshall RPT header or body for NodoInviaRPT. ", e); - } - */ - return rptRequest; - } - - private RPTRequest unmarshallNodoInviaCarrelloRPT(RPTRequestEntity entity) throws ConversionException { - RPTRequest rptRequest = null; - /* - try { - // unmarshalling header content - Unmarshaller intestazioneCarrelloPPTUnmarshaller = intestazioneCarrelloPPTContext.createUnmarshaller(); - Document intestazioneCarrelloPPTDoc = this.docBuilder.parse(new ByteArrayInputStream(entity.getHeader().getBytes())); - IntestazioneCarrelloPPT header = intestazioneCarrelloPPTUnmarshaller.unmarshal(intestazioneCarrelloPPTDoc, IntestazioneCarrelloPPT.class).getValue(); - - // unmarshalling body content - Unmarshaller nodoInviaCarrelloRPTBodyUnmarshaller = nodoInviaCarrelloRPTBodyContext.createUnmarshaller(); - Document nodoInviaCarrelloRPTBodyDoc = this.docBuilder.parse(new ByteArrayInputStream(entity.getBody().getBytes())); - NodoInviaCarrelloRPT body = nodoInviaCarrelloRPTBodyUnmarshaller.unmarshal(nodoInviaCarrelloRPTBodyDoc, NodoInviaCarrelloRPT.class).getValue(); - - // generating complete response - rptRequest = RPTRequest.builder() - .body(body) - .header(header) - .build(); - } catch (JAXBException | IOException | SAXException e) { - throw new ConversionException("Unable to unmarshall RPT header or body for NodoInviaCarrelloRPT. ", e); - } - */ - return rptRequest; - } -} diff --git a/src/main/java/it/gov/pagopa/wispconverter/util/JaxbElementUtil.java b/src/main/java/it/gov/pagopa/wispconverter/util/JaxbElementUtil.java index 04f491d8..28e7e305 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/util/JaxbElementUtil.java +++ b/src/main/java/it/gov/pagopa/wispconverter/util/JaxbElementUtil.java @@ -1,10 +1,7 @@ package it.gov.pagopa.wispconverter.util; -; -import it.gov.pagopa.wispconverter.entity.Primitive; import it.gov.pagopa.wispconverter.exception.AppError; import it.gov.pagopa.wispconverter.exception.AppException; -import it.gov.pagopa.wispconverter.exception.conversion.ConversionException; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBElement; import jakarta.xml.bind.JAXBException; @@ -21,13 +18,11 @@ import org.xmlsoap.schemas.soap.envelope.Envelope; import org.xmlsoap.schemas.soap.envelope.Header; -import javax.xml.XMLConstants; -import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import java.io.*; -import java.util.Arrays; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.util.List; @Slf4j @@ -42,7 +37,7 @@ public class JaxbElementUtil { private static final String RPT_NAMESPACE_URI = "http://www.digitpa.gov.it/schemas/2011/Pagamenti/"; private static final String RPT_LOCAL_NAME = "RPT"; - private final DocumentBuilderFactory documentBuilderFactory; + private final DocumentBuilderFactory documentBuilderFactory; private Element convertToElement(InputSource is, String nameSpaceUri, String localName) { try { @@ -52,8 +47,7 @@ private Element convertToElement(InputSource is, String nameSpaceUri, String loc if (nodeList.getLength() == 0) { throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, "NodeList is empty"); } - Element element = (Element) nodeList.item(0); - return element; + return (Element) nodeList.item(0); } catch (ParserConfigurationException | IOException | SAXException e) { log.error("Errore durante il parsing", e); throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, e, e.getMessage()); @@ -73,36 +67,37 @@ public T convertToBean(Element element, Class targetType) { } - public Element convertToEnvelopeElement(byte[] source){ + public Element convertToEnvelopeElement(byte[] source) { return convertToElement(new InputSource(new ByteArrayInputStream(source)), ENVELOPE_NAMESPACE_URI, ENVELOPE_LOCAL_NAME); } - public Element convertToRPTElement(byte[] source){ + + public Element convertToRPTElement(byte[] source) { return convertToElement(new InputSource(new ByteArrayInputStream(source)), RPT_NAMESPACE_URI, RPT_LOCAL_NAME); } - public T getSoapHeader(Envelope envelope, Class targetType){ + public T getSoapHeader(Envelope envelope, Class targetType) { Header header = envelope.getHeader(); - if(header == null){ + if (header == null) { throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, "header is null"); } List list = header.getAny(); - if(list == null || list.isEmpty()){ + if (list == null || list.isEmpty()) { throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, "headerValue is null or is empty"); } Element element = (Element) list.get(0); return convertToBean(element, targetType); } - public T getSoapBody(Envelope envelope, Class targetType){ + public T getSoapBody(Envelope envelope, Class targetType) { Body body = envelope.getBody(); - if(body == null){ + if (body == null) { throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, "body is null"); } List list = body.getAny(); - if(list == null || list.isEmpty()){ + if (list == null || list.isEmpty()) { throw new AppException(AppError.PAYLOAD_CONVERSION_ERROR, "bodyValue is null or is empty"); } Element element = (Element) list.get(0);