Search in sources :

Example 1 with SerializerException

use of net.petafuel.styx.core.xs2a.exceptions.SerializerException 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 SerializerException

use of net.petafuel.styx.core.xs2a.exceptions.SerializerException in project styx by petafuel.

the class PaymentInitiationRequest method createPeriodicPayment.

private Optional<String> createPeriodicPayment() {
    if (getPaymentProduct().isXml()) {
        PAIN00100303Document document = (new PaymentXMLSerializer()).serialize(UUID.randomUUID().toString(), (PeriodicPayment) payment);
        SEPAWriter writer = new SEPAWriter(document);
        String painXmlBody;
        try {
            painXmlBody = new String(writer.writeSEPA());
        } catch (SEPAWriteException e) {
            throw new SerializerException("Unable to create xml body for periodic payment initiation multipart http message");
        }
        Headers headersXml = new Headers.Builder().add("Content-Disposition", "form-data; name=\"xml_sct\"").build();
        MultipartBody.Part xmlPart = MultipartBody.Part.create(headersXml, RequestBody.create(painXmlBody, MediaType.get("application/xml; charset=utf-8")));
        Headers headersJson = new Headers.Builder().add("Content-Disposition", "form-data; name=\"json_standingorderType\"").build();
        MultipartBody.Part jsonPart;
        try (Jsonb jsonb = JsonbBuilder.create()) {
            jsonPart = MultipartBody.Part.create(headersJson, RequestBody.create(jsonb.toJson(payment), MediaType.get("application/json; charset=utf-8")));
        } catch (Exception e) {
            throw new SerializerException(e.getMessage());
        }
        // add a uuid as boundary to avoid boundary repetition in http message
        RequestBody requestBody = new MultipartBody.Builder(getMultipartBoundary()).addPart(xmlPart).addPart(jsonPart).setType(MultipartBody.FORM).build();
        final Buffer buffer = new Buffer();
        try {
            // build raw multipartBody with xml and json
            requestBody.writeTo(buffer);
            return Optional.of(buffer.readUtf8());
        } catch (IOException e) {
            LOG.warn("Error creating raw body for periodic payment initiation message={} cause={}", e.getMessage(), e.getCause().getCause());
            return Optional.empty();
        }
    } else {
        try (Jsonb jsonb = JsonbBuilder.create()) {
            return Optional.ofNullable(jsonb.toJson(payment));
        } catch (Exception e) {
            return Optional.empty();
        }
    }
}
Also used : Buffer(okio.Buffer) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) PAIN00100303Document(net.petafuel.jsepa.model.PAIN00100303Document) Headers(okhttp3.Headers) JsonbBuilder(javax.json.bind.JsonbBuilder) PaymentXMLSerializer(net.petafuel.styx.core.xs2a.utils.PaymentXMLSerializer) IOException(java.io.IOException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) IOException(java.io.IOException) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) Jsonb(javax.json.bind.Jsonb) SEPAWriter(net.petafuel.jsepa.SEPAWriter) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 3 with SerializerException

use of net.petafuel.styx.core.xs2a.exceptions.SerializerException in project styx by petafuel.

the class RefreshTokenRequest method getRawBody.

@Override
public Optional<String> getRawBody() {
    String rawBody;
    if (isJsonBody()) {
        try (Jsonb jsonb = JsonbBuilder.create()) {
            rawBody = jsonb.toJson(this);
        } catch (Exception e) {
            throw new SerializerException("Unable to create request body for RefreshTokenRequest");
        }
    } else {
        Map<String, String> params = new HashMap<>();
        params.put("grant_type", getGrantType());
        params.put("client_id", getClientId());
        params.put("refresh_token", getRefreshToken());
        rawBody = BasicService.httpBuildQuery(params).substring(1);
    }
    return Optional.ofNullable(rawBody);
}
Also used : Jsonb(javax.json.bind.Jsonb) HashMap(java.util.HashMap) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException)

Example 4 with SerializerException

use of net.petafuel.styx.core.xs2a.exceptions.SerializerException in project styx by petafuel.

the class SAD method parseImplementerOptions.

/**
 * Parsing the implementer options json and mapping to ImplementerOption List
 * within the aspsp
 *
 * @param aspsp Aspsp object that should be used and should be modified with the
 *              parsed implementer options
 */
private void parseImplementerOptions(Aspsp aspsp) {
    String rawDefaultConfig;
    // template if not present for aspsp
    if ((rawDefaultConfig = aspsp.getConfig().getConfiguration()) == null) {
        rawDefaultConfig = aspsp.getConfig().getStandard().getConfigTemplate();
    }
    JsonObject defaultConfig = null;
    try (Jsonb jsonb = JsonbBuilder.create()) {
        defaultConfig = jsonb.fromJson(rawDefaultConfig, JsonObject.class);
    } catch (Exception e) {
        throw new SerializerException("unable to deserialize implementer-options json on SAD Service usage", e);
    }
    // Check if there is a styx config on aspsp level
    // If not present on aspsp level use styx config template of related standard
    String rawStyxConfig;
    if ((rawStyxConfig = aspsp.getConfig().getStyxConfiguration()) == null) {
        rawStyxConfig = aspsp.getConfig().getStandard().getStyxConfigTemplate();
    }
    // Merge the styx config options into the defaultConfig
    JsonObject styxConfig = null;
    try (Jsonb jsonb = JsonbBuilder.create()) {
        styxConfig = jsonb.fromJson(rawStyxConfig, JsonObject.class);
    } catch (Exception e) {
        throw new SerializerException("unable to deserialize styx-options json on SAD Service usage", e);
    }
    JsonObjectBuilder defaultConfigJsonBuilder = Json.createObjectBuilder(defaultConfig);
    defaultConfigJsonBuilder.addAll(Json.createObjectBuilder(styxConfig));
    defaultConfig = defaultConfigJsonBuilder.build();
    defaultConfig.entrySet().stream().forEach(entry -> {
        JsonObject currentOption = entry.getValue().asJsonObject();
        ImplementerOption implementerOption = new ImplementerOption();
        implementerOption.setId(entry.getKey());
        implementerOption.setDescription(currentOption.getString("description", "default"));
        currentOption.get("options").asJsonObject().entrySet().stream().forEach(option -> implementerOption.addOption(option.getKey(), Boolean.valueOf(option.getValue().toString())));
        aspsp.getConfig().getImplementerOptions().put(implementerOption.getId(), implementerOption);
    });
}
Also used : Jsonb(javax.json.bind.Jsonb) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) ImplementerOption(net.petafuel.styx.core.banklookup.sad.entities.ImplementerOption) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankNotFoundException(net.petafuel.styx.core.banklookup.exceptions.BankNotFoundException) SADException(net.petafuel.styx.core.xs2a.exceptions.SADException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BankLookupFailedException(net.petafuel.styx.core.banklookup.exceptions.BankLookupFailedException)

Example 5 with SerializerException

use of net.petafuel.styx.core.xs2a.exceptions.SerializerException in project styx by petafuel.

the class PersistentClientApp method formatRestrictions.

public static void formatRestrictions(String restrictions, MasterToken model) {
    if (restrictions != null) {
        try (Jsonb jsonb = JsonbBuilder.create()) {
            Type type = new GenericType<Map<String, MasterTokenRestriction>>() {
            }.getType();
            model.setRestrictions(jsonb.fromJson(restrictions, type));
        } catch (Exception e) {
            throw new SerializerException(e.getMessage(), e);
        }
    }
}
Also used : Jsonb(javax.json.bind.Jsonb) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) Map(java.util.Map) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) PersistenceException(net.petafuel.styx.core.persistence.PersistenceException) SQLException(java.sql.SQLException)

Aggregations

SerializerException (net.petafuel.styx.core.xs2a.exceptions.SerializerException)11 Jsonb (javax.json.bind.Jsonb)9 SQLException (java.sql.SQLException)3 PersistenceException (net.petafuel.styx.core.persistence.PersistenceException)3 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)2 Response (okhttp3.Response)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1 JsonObject (javax.json.JsonObject)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonbBuilder (javax.json.bind.JsonbBuilder)1 JsonbTransient (javax.json.bind.annotation.JsonbTransient)1 GenericType (javax.ws.rs.core.GenericType)1 SEPAWriter (net.petafuel.jsepa.SEPAWriter)1 SEPAParsingException (net.petafuel.jsepa.exception.SEPAParsingException)1