From bd4fb7789df85d8b14fd63ef3e0291943d1dbc4d Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 21 Feb 2017 21:03:49 +0100 Subject: [PATCH] Improved error handling. --- libEet/eetresult.h | 3 +- libEet/eetsender.cpp | 124 ++++++++++++++++++++++++++++--------------- libEet/eetsender.h | 8 ++- 3 files changed, 91 insertions(+), 44 deletions(-) diff --git a/libEet/eetresult.h b/libEet/eetresult.h index 12ba172..0899fa1 100644 --- a/libEet/eetresult.h +++ b/libEet/eetresult.h @@ -40,7 +40,8 @@ public: SERVICE_CERT_ERROR, INVALID_SIGNATURE, SSL_ERROR, - TIMEDOUT + TIMEDOUT, + OFFLINE }; explicit EetResult(QObject *parent = 0); diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index 9132a98..4384a25 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -7,6 +7,7 @@ #include #include #include +#include const QString EetSender::ms_nsDef = "declare namespace eet = \"http://fs.mfcr.cz/eet/schema/v3\";\n" "declare namespace ds = \"http://www.w3.org/2000/09/xmldsig#\";\n" @@ -15,6 +16,8 @@ const QString EetSender::ms_nsDef = "declare namespace eet = \"http://fs.mfcr.cz "declare namespace senc = \"http://schemas.xmlsoap.org/soap/encoding/\";\n" "declare namespace senv = \"http://schemas.xmlsoap.org/soap/envelope/\";\n"; +bool EetSender::m_online = true; + EetSender::EetSender(QObject *parent) : QObject(parent) { m_signer = nullptr; @@ -27,11 +30,14 @@ EetSender::EetSender(QObject *parent) : QObject(parent) connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); connect(m_manager, &QNetworkAccessManager::sslErrors, [this](QNetworkReply *rep, QList){ m_resut->setStatus(EetResult::SSL_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); rep->deleteLater(); }); m_serviceUrl = PRODUCTION_URL; + m_timer = new QTimer(this); + m_timer->setInterval(1000); + m_timer->setSingleShot(true); } void EetSender::sendRequest(EetRequest *request) @@ -39,7 +45,7 @@ void EetSender::sendRequest(EetRequest *request) if (m_signer == nullptr) { m_resut->setStatus(EetResult::USER_CERT_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); return; } @@ -65,7 +71,24 @@ void EetSender::sendRequest(EetRequest *request) QString strRequest = tempRequest.fillTemplate(val); QNetworkRequest req(QUrl(m_serviceUrl.toStdString().c_str())); - m_manager->post(req, strRequest.toUtf8()); + + qDebug() << strRequest; + + if (m_online) + { + QNetworkReply *rep = m_manager->post(req, strRequest.toUtf8()); + m_timer->start(); + connect(m_timer, &QTimer::timeout, [this, rep](){ + rep->abort(); + m_resut->setStatus(EetResult::TIMEDOUT); + emit sendFinished(); + }); + } + else + { + m_resut->setStatus(EetResult::OFFLINE); + emit sendFinished(); + } } void EetSender::setupSigner(const QString &certPath, const QString &passwd) @@ -91,7 +114,7 @@ bool EetSender::checkSignature() const void EetSender::setPlayground(bool pg) { - m_serviceUrl = pg ? PLAYGROUND_URL : PRODUCTION_URL; + m_serviceUrl = (pg ? PLAYGROUND_URL : PRODUCTION_URL); } EetResult *EetSender::resut() const @@ -99,6 +122,11 @@ EetResult *EetSender::resut() const return m_resut; } +void EetSender::setTimeout(int timeout) +{ + m_timer->setInterval(timeout); +} + bool EetSender::verifySignature(const QByteArray &repData) { QString queryString("//senv:Envelope/senv:Header/wsse:Security/ds:Signature/ds:SignedInfo"); @@ -159,56 +187,34 @@ bool EetSender::verifySignature(const QByteArray &repData) void EetSender::replyFinished(QNetworkReply *reply) { + m_timer->stop(); if (reply->error() != QNetworkReply::NoError) { m_resut->setStatus(EetResult::SERVER_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); reply->deleteLater(); return; } QByteArray repData = reply->readAll(); - if (m_checkSignature && !verifySignature(repData)) - { - emit sendFinished(m_resut); - return; - } + qDebug() << QString(repData); QXmlQuery q; q.setFocus(QString(repData)); - QString result; - QString queryString("//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@uuid_zpravy/data(.)"); - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - - m_resut->setUuid(QUuid(result)); - - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@dat_prij/data(.)"; - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - m_resut->setReciveDate(QDateTime::fromString(result)); - - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Potvrzeni/@fik/data(.)"; - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - m_resut->setFik(result); - QXmlResultItems items; - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Varovani"; + QString result; + QString queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Chyba"; q.setQuery(ms_nsDef + queryString); q.evaluateTo(&items); - EetMessageList warnings; + EetMessageList errors; QXmlItem item = items.next(); while (!item.isNull()) { EetMessage *mesg = new EetMessage(m_resut); - queryString = "./@kod_varov/data(.)"; + queryString = "./@kod/data(.)"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result); @@ -223,23 +229,48 @@ void EetSender::replyFinished(QNetworkReply *reply) result = result.trimmed(); mesg->setMessage(result); - warnings.append(mesg); + errors.append(mesg); item = items.next(); } - m_resut->setWarnings(warnings); + m_resut->setErrors(errors); + + if (m_checkSignature && errors.isEmpty() && !verifySignature(repData)) + { + emit sendFinished(); + return; + } + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@uuid_zpravy/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Chyba"; + m_resut->setUuid(QUuid(result)); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@dat_prij/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); + m_resut->setReciveDate(QDateTime::fromString(result)); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Potvrzeni/@fik/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); + m_resut->setFik(result); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Varovani"; q.setQuery(ms_nsDef + queryString); q.evaluateTo(&items); - EetMessageList errors; + EetMessageList warnings; item = items.next(); while (!item.isNull()) { EetMessage *mesg = new EetMessage(m_resut); - queryString = "./@kod/data(.)"; + queryString = "./@kod_varov/data(.)"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result); @@ -254,14 +285,23 @@ void EetSender::replyFinished(QNetworkReply *reply) result = result.trimmed(); mesg->setMessage(result); - errors.append(mesg); + warnings.append(mesg); item = items.next(); } - m_resut->setErrors(errors); - m_resut->setReciveDate(QDateTime::currentDateTime()); - emit sendFinished(m_resut); + m_resut->setWarnings(warnings); + + if (errors.isEmpty()) + { + m_resut->setReciveDate(QDateTime::currentDateTime()); + } + else + { + m_resut->setStatus(EetResult::DATA_ERROR); + } + + emit sendFinished(); reply->deleteLater(); } diff --git a/libEet/eetsender.h b/libEet/eetsender.h index 708afac..c520509 100644 --- a/libEet/eetsender.h +++ b/libEet/eetsender.h @@ -2,6 +2,7 @@ #define EETSENDER_H #include +#include #include "eetcpp_global.h" #include "eetrequest.h" @@ -58,10 +59,15 @@ public: */ EetResult *resut() const; + void setTimeout(int timeout); + + static bool m_online; + private: static const QString ms_nsDef; EetSigner *m_signer; QNetworkAccessManager *m_manager; + QTimer *m_timer; bool m_checkSignature; EetResult *m_resut; QString m_serviceUrl; @@ -72,7 +78,7 @@ private slots: void replyFinished(QNetworkReply *reply); signals: - void sendFinished(EetResult *result); + void sendFinished(); public slots: };