fix: improve the portal config parsing

This commit is contained in:
Kevin Yue 2022-05-26 11:48:55 +08:00
parent ffa99d3783
commit d5cd90373b
2 changed files with 34 additions and 42 deletions

View File

@ -78,10 +78,7 @@ QList<GPGateway> PortalConfigResponse::parseGateways(QXmlStreamReader &xmlReader
// Parse the gateways -> external -> list -> entry
if (xmlReader.name() == "entry" && xmlReader.isStartElement()) {
GPGateway g;
QString address = xmlReader.attributes().value("name").toString();
g.setAddress(address);
g.setPriorityRules(parsePriorityRules(xmlReader));
g.setName(parseGatewayName(xmlReader));
parseGateway(xmlReader, g);
gateways.append(g);
}
}
@ -91,45 +88,44 @@ QList<GPGateway> PortalConfigResponse::parseGateways(QXmlStreamReader &xmlReader
return gateways;
}
QMap<QString, int> PortalConfigResponse::parsePriorityRules(QXmlStreamReader &xmlReader)
{
PLOGI << "Start parsing the priority rules...";
void PortalConfigResponse::parseGateway(QXmlStreamReader &reader, GPGateway &gateway) {
PLOGI << "Start parsing gateway...";
auto finished = false;
while (!finished) {
if (reader.name() == "entry") {
auto address = reader.attributes().value("name").toString();
gateway.setAddress(address);
} else if (reader.name() == "description") { // gateway name
gateway.setName(reader.readElementText());
} else if (reader.name() == "priority-rule") { // priority rules
parsePriorityRule(reader, gateway);
}
finished = !reader.readNextStartElement();
}
}
void PortalConfigResponse::parsePriorityRule(QXmlStreamReader &reader, GPGateway &gateway) {
PLOGI << "Start parsing priority rule...";
QMap<QString, int> priorityRules;
auto finished = false;
while ((xmlReader.name() != "priority-rule" || !xmlReader.isEndElement()) && !xmlReader.hasError()) {
xmlReader.readNext();
if (xmlReader.name() == "entry" && xmlReader.isStartElement()) {
QString ruleName = xmlReader.attributes().value("name").toString();
// Read the priority tag
while (xmlReader.name() != "priority"){
xmlReader.readNext();
while (!finished) {
// Parse the priority-rule -> entry
if (reader.name() == "entry") {
auto ruleName = reader.attributes().value("name").toString();
// move to the priority value
while (reader.name() != "priority") {
reader.readNextStartElement();
}
int ruleValue = xmlReader.readElementText().toUInt();
priorityRules.insert(ruleName, ruleValue);
auto priority = reader.readElementText().toInt();
priorityRules.insert(ruleName, priority);
}
finished = !reader.readNextStartElement();
}
PLOGI << "Finished parsing the priority rules.";
return priorityRules;
}
QString PortalConfigResponse::parseGatewayName(QXmlStreamReader &xmlReader)
{
PLOGI << "Start parsing the gateway name...";
while (xmlReader.name() != "description" || !xmlReader.isEndElement()) {
xmlReader.readNext();
if (xmlReader.name() == "description" && xmlReader.tokenType() == xmlReader.StartElement) {
PLOGI << "Finished parsing the gateway name";
return xmlReader.readElementText();
}
}
PLOGE << "Error: <description> tag not found";
return "";
gateway.setPriorityRules(priorityRules);
}
QString PortalConfigResponse::userAuthCookie() const
@ -137,11 +133,6 @@ QString PortalConfigResponse::userAuthCookie() const
return m_userAuthCookie;
}
QString PortalConfigResponse::prelogonUserAuthCookie() const
{
return m_prelogonAuthCookie;
}
QList<GPGateway> PortalConfigResponse::allGateways() const
{
return m_gateways;
@ -176,3 +167,4 @@ void PortalConfigResponse::setPrelogonUserAuthCookie(const QString cookie)
{
m_prelogonAuthCookie = cookie;
}

View File

@ -19,7 +19,6 @@ public:
const QString &username() const;
QString password() const;
QString userAuthCookie() const;
QString prelogonUserAuthCookie() const;
QList<GPGateway> allGateways() const;
void setAllGateways(QList<GPGateway> gateways);
@ -44,8 +43,9 @@ private:
void setPrelogonUserAuthCookie(const QString cookie);
static QList<GPGateway> parseGateways(QXmlStreamReader &xmlReader);
static QMap<QString, int> parsePriorityRules(QXmlStreamReader &xmlReader);
static QString parseGatewayName(QXmlStreamReader &xmlReader);
static void parseGateway(QXmlStreamReader &reader, GPGateway &gateway);
static void parsePriorityRule(QXmlStreamReader &reader, GPGateway &gateway);
};
#endif // PORTALCONFIGRESPONSE_H