Skip to content

Commit

Permalink
feat: [PAGOPA-2032] sendRTV2 log (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: aacitelli <[email protected]>
  • Loading branch information
alessio-acitelli and aacitelli authored Sep 30, 2024
1 parent db54d60 commit a3cb7bd
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@
import it.gov.pagopa.wispconverter.service.model.ReceiptDto;
import it.gov.pagopa.wispconverter.util.Constants;
import it.gov.pagopa.wispconverter.util.ErrorUtil;
import it.gov.pagopa.wispconverter.util.ReceiptRequestHandler;
import it.gov.pagopa.wispconverter.util.Trace;
import it.gov.pagopa.wispconverter.util.ReceiptRequestHandler.PaSendRTV2Request;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/receipt")
Expand All @@ -56,6 +59,10 @@ public class ReceiptController {
private final ObjectMapper mapper;

private final ErrorUtil errorUtil;

private final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

private final ReceiptRequestHandler receiptRequestHandler;

@Operation(summary = "", description = "", security = {@SecurityRequirement(name = "ApiKey")}, tags = {"Receipt"})
@ApiResponses(value = {
Expand Down Expand Up @@ -124,7 +131,7 @@ public void receiptKo(@RequestBody String request) throws Exception {
public void receiptOk(@RequestBody ReceiptRequest request) throws IOException {

try {
log.info("Invoking API operation receiptOk - args: {}", request.toString());
log.info("Invoking API operation receiptOk - args: {}", this.getReceiptRequestInfoToLog(request.getContent()));
receiptService.sendOkPaaInviaRtToCreditorInstitution(request.getContent());
log.info("Successful API operation receiptOk");
} catch (Exception ex) {
Expand All @@ -137,4 +144,24 @@ public void receiptOk(@RequestBody ReceiptRequest request) throws IOException {
throw ex;
}
}

private String getReceiptRequestInfoToLog(String xml) {
String args = "n/a";
try {
if (StringUtils.isNotEmpty(xml)) {
// fix for sonar issue XML external entity in user-controlled data
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

saxParserFactory.newSAXParser().parse(new InputSource(new StringReader(xml)), receiptRequestHandler);

PaSendRTV2Request result = receiptRequestHandler.getPaSendRTV2Request();
args = "noticeNumber="+result.getNoticeNumber()+", fiscalCode="+result.getFiscalCode()+", creditorReferenceId="+result.getCreditorReferenceId();
}
} catch (SAXException | IOException | ParserConfigurationException e) {
return args;
}
return args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package it.gov.pagopa.wispconverter.util;

import org.springframework.stereotype.Component;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper=false)
@Component
public class ReceiptRequestHandler extends DefaultHandler {

private static final String NOTICE_NUMBER = "noticeNumber";
private static final String FISCAL_CODE = "fiscalCode";
private static final String CREDITOR_REFERENCE_ID = "creditorReferenceId";

private PaSendRTV2Request paSendRTV2Request;
private StringBuilder elementValue;

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (elementValue == null) {
elementValue = new StringBuilder();
} else {
elementValue.append(ch, start, length);
}
}

@Override
public void startDocument() throws SAXException {
paSendRTV2Request = new PaSendRTV2Request();
}

@Override
public void startElement(String uri, String lName, String qName, Attributes attr) throws SAXException {
switch (qName) {
case NOTICE_NUMBER:
elementValue = new StringBuilder();
break;
case FISCAL_CODE:
elementValue = new StringBuilder();
break;
case CREDITOR_REFERENCE_ID:
elementValue = new StringBuilder();
break;
default:
break;
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case NOTICE_NUMBER:
paSendRTV2Request.setNoticeNumber(elementValue.toString());
break;
case FISCAL_CODE:
paSendRTV2Request.setFiscalCode(elementValue.toString());
break;
case CREDITOR_REFERENCE_ID:
paSendRTV2Request.setCreditorReferenceId(elementValue.toString());
break;
default:
break;
}
}

@Data
public class PaSendRTV2Request{
private String noticeNumber;
private String fiscalCode;
private String creditorReferenceId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testClientLogger2() {
public void testServerLogger() {

Method method = ReceiptController.class.getMethod("receiptOk", ReceiptRequest.class);
HandlerMethod handlerMethod = new HandlerMethod(new ReceiptController(null, null, null, null), method);
HandlerMethod handlerMethod = new HandlerMethod(new ReceiptController(null, null, null, null, null), method);

HttpServletRequest httpRequest = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
Expand Down Expand Up @@ -131,7 +131,7 @@ public void testServerLogger() {
public void testServerLogger2() {

Method method = ReceiptController.class.getMethod("receiptOk", ReceiptRequest.class);
HandlerMethod handlerMethod = new HandlerMethod(new ReceiptController(null, null, null, null), method);
HandlerMethod handlerMethod = new HandlerMethod(new ReceiptController(null, null, null, null, null), method);

HttpServletRequest httpRequest = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.wispconverter.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

import it.gov.pagopa.wispconverter.util.ReceiptRequestHandler;
import it.gov.pagopa.wispconverter.util.ReceiptRequestHandler.PaSendRTV2Request;

class ReceiptRequestHandlerTest {

@Test
void parsePaSendRTV2Request() throws ParserConfigurationException, SAXException, IOException {
ReceiptRequestHandler receiptRequestHandler = new ReceiptRequestHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("src/test/resources/requests/paSendRTV2Request.xml", receiptRequestHandler);

PaSendRTV2Request result = receiptRequestHandler.getPaSendRTV2Request();
assertEquals("348172725623804858", result.getNoticeNumber());
assertEquals("15376371009", result.getFiscalCode());
assertEquals("863965926210520", result.getCreditorReferenceId());
}
}
67 changes: 67 additions & 0 deletions src/test/resources/requests/paSendRTV2Request.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<soapenv:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://pagopa-api.pagopa.gov.it/paForNode"
xmlns:common="http://pagopa-api.pagopa.gov.it/xsd/common-types/v1.0.0/"
xmlns:pafn="http://pagopa-api.pagopa.gov.it/pa/paForNode.xsd">
<soapenv:Body>
<pafn:paSendRTV2Request>
<idPA>15376371009</idPA>
<idBrokerPA>15376371009</idBrokerPA>
<idStation>15376371009_48</idStation>
<receipt>
<receiptId>a0fffc676950472c94ca8e1456dc7b03</receiptId>
<noticeNumber>348172725623804858</noticeNumber>
<fiscalCode>15376371009</fiscalCode>
<outcome>OK</outcome>
<creditorReferenceId>863965926210520</creditorReferenceId>
<paymentAmount>16.00</paymentAmount>
<description>/RFB/863965926210520/16.00/TXT/DEBITORE/JHNDOE00A01F205N</description>
<companyName>PagoPA S.p.A.</companyName>
<debtor>
<uniqueIdentifier>
<entityUniqueIdentifierType>M</entityUniqueIdentifierType>
<entityUniqueIdentifierValue>JHNDOE00A01F205N</entityUniqueIdentifierValue>
</uniqueIdentifier>
<fullName>John Doe</fullName>
<streetName>Via qualsiasi</streetName>
<civicNumber>1</civicNumber>
<postalCode>00100</postalCode>
<city>Roma</city>
<stateProvinceRegion>RM</stateProvinceRegion>
<country>IT</country>
<e-mail>[email protected]</e-mail>
</debtor>
<transferList>
<transfer>
<idTransfer>1</idTransfer>
<transferAmount>16.00</transferAmount>
<fiscalCodePA>77777777777</fiscalCodePA>
<companyName></companyName>
<MBDAttachment>attachment</MBDAttachment>
<remittanceInformation>/RFB/863965926210520/16.00/TXT/DEBITORE/JHNDOE00A01F205N</remittanceInformation>
<transferCategory>9/0301116TS</transferCategory>
<metadata>
<mapEntry>
<key>DatiSpecificiRiscossione</key>
<value>9/0301116TS/9/11111111111</value>
</mapEntry>
</metadata>
</transfer>
</transferList>
<idPSP>BCITITMM</idPSP>
<pspFiscalCode>88888888888</pspFiscalCode>
<PSPCompanyName>Intesa Sanpaolo S.p.A</PSPCompanyName>
<idChannel>88888888888_01</idChannel>
<channelDescription>app</channelDescription>
<paymentMethod>creditCard</paymentMethod>
<paymentNote>734b9b88-6186-4034-a79e-813efacf36a0</paymentNote>
<fee>0.35</fee>
<paymentDateTime>2024-09-25T11:24:01</paymentDateTime>
<applicationDate>2024-09-25</applicationDate>
<transferDate>2024-09-26</transferDate>
</receipt>
</pafn:paSendRTV2Request>
</soapenv:Body>
</soapenv:Envelope>

0 comments on commit a3cb7bd

Please sign in to comment.