Search in sources :

Example 1 with SEPAParsingException

use of net.petafuel.jsepa.exception.SEPAParsingException in project styx by petafuel.

the class BerlinGroupPIS method getPaymentStatus.

@Override
public PaymentStatus getPaymentStatus(PISRequest xs2AGetRequest) throws BankRequestFailedException {
    this.setUrl(this.url + xs2AGetRequest.getServicePath());
    this.createBody(RequestType.GET);
    if (xs2AGetRequest.getPaymentProduct().isXml()) {
        xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, XML.toString());
    } else {
        xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, JSON.toString());
    }
    this.createHeaders(xs2AGetRequest);
    try (Response response = this.execute();
        Jsonb jsonb = JsonbBuilder.create()) {
        String contentType;
        if ((contentType = response.headers().get("content-type")) == null) {
            throw new BankRequestFailedException("Content-Type Header is not set, parsing of json or xml is not possible", response.code());
        }
        String responseBody = extractResponseBody(response, 200);
        if (contentType.contains(MediaType.APPLICATION_JSON)) {
            return jsonb.fromJson(responseBody, PaymentStatus.class);
        } else {
            ReportConverter converter = new ReportConverter();
            TransactionReport report = converter.processReport(responseBody);
            return new PaymentStatus(TransactionStatus.valueOf(report.getStatus()), null);
        }
    } catch (IOException | SEPAParsingException e) {
        throw new BankRequestFailedException(e.getMessage(), e);
    } catch (Exception e) {
        throw new SerializerException("Unable to deserialize json to PaymentStatus", e);
    }
}
Also used : Response(okhttp3.Response) Jsonb(javax.json.bind.Jsonb) ReportConverter(net.petafuel.jsepa.facades.ReportConverter) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) TransactionReport(net.petafuel.jsepa.model.pain002.TransactionReport) IOException(java.io.IOException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) PaymentStatus(net.petafuel.styx.core.xs2a.entities.PaymentStatus) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) IOException(java.io.IOException)

Example 2 with SEPAParsingException

use of net.petafuel.jsepa.exception.SEPAParsingException in project styx by petafuel.

the class Camt052Converter method parseReport.

private BankToCustomerAccountReportV02 parseReport(String xmlData) throws SEPAParsingException {
    try {
        // schema factory is required to initialize the validator object (XSD based validator)
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // extract XSD file from the JAR bundle
        URL resource = getClass().getClassLoader().getResource("schema/camt.052.001.02.xsd");
        // schema object will be initialized for validation
        Schema schema = sf.newSchema(resource);
        // jaxb context is required to initialize the unmarshaller, here we have to pass the GENERATED ObjectFactory class
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        // now, the unmarshaller (make java object out of XML string) itself gets initialized
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        // connect the validator and unmarshaller. from here, the validation is activated
        unmarshaller.setSchema(schema);
        // now, validate and unmarshal the XML string
        Document document = ((JAXBElement<Document>) unmarshaller.unmarshal(new StringReader(xmlData))).getValue();
        LOG.info("camt052 xml parsing successful");
        // extract relevant element from the whole XML. this steps is specific to the XML format we work with
        return document.getBkToCstmrAcctRpt();
    } catch (JAXBException | SAXException error) {
        LOG.error("parseReport failed, invalid input", error);
        throw new SEPAParsingException("Camt052 XML unmarshalling failed!", error);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) Document(net.petafuel.styx.core.xs2a.utils.sepa.camt052.Document) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Aggregations

SEPAParsingException (net.petafuel.jsepa.exception.SEPAParsingException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 Jsonb (javax.json.bind.Jsonb)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBElement (javax.xml.bind.JAXBElement)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 ReportConverter (net.petafuel.jsepa.facades.ReportConverter)1 TransactionReport (net.petafuel.jsepa.model.pain002.TransactionReport)1 PaymentStatus (net.petafuel.styx.core.xs2a.entities.PaymentStatus)1 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)1 SerializerException (net.petafuel.styx.core.xs2a.exceptions.SerializerException)1 Document (net.petafuel.styx.core.xs2a.utils.sepa.camt052.Document)1 Response (okhttp3.Response)1 SAXException (org.xml.sax.SAXException)1