From c0f137897c8761ffe21e73f009913e08de8e9a71 Mon Sep 17 00:00:00 2001 From: Tobias Kellner Date: Fri, 26 Feb 2016 09:33:17 +0100 Subject: More flexible HTML parsing for mobile BKU Way more lenient towards slight page changes now --- .../asit/pdfover/gui/bku/mobile/ATrustHandler.java | 46 ++++----- .../asit/pdfover/gui/bku/mobile/IAIKHandler.java | 22 ++--- .../pdfover/gui/bku/mobile/MobileBKUHelper.java | 108 +++++++++++++++++++-- 3 files changed, 136 insertions(+), 40 deletions(-) (limited to 'pdf-over-gui') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/ATrustHandler.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/ATrustHandler.java index b7e457c3..a493d109 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/ATrustHandler.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/ATrustHandler.java @@ -79,23 +79,23 @@ public class ATrustHandler extends MobileBKUHandler { ATrustStatus status = getStatus(); if (responseData.contains("", ""); //$NON-NLS-1$ //$NON-NLS-2$ - String errorMsg = MobileBKUHelper.extractTag(responseData, + String errorMsg = MobileBKUHelper.extractSubstring(responseData, "", ""); //$NON-NLS-1$ //$NON-NLS-2$ throw new Exception("Error from mobile BKU: " + //$NON-NLS-1$ errorCode + " - " + errorMsg); //$NON-NLS-1$ } // Extract infos: - String sessionID = MobileBKUHelper.extractTag(responseData, + String sessionID = MobileBKUHelper.extractSubstring(responseData, "identification.aspx?sid=", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - String viewState = MobileBKUHelper.extractTag(responseData, - "id=\"__VIEWSTATE\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + String viewState = MobileBKUHelper.extractValueFromTagWithParam( + responseData, "", "id", "__VIEWSTATE", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - String eventValidation = MobileBKUHelper.extractTag(responseData, - "id=\"__EVENTVALIDATION\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + String eventValidation = MobileBKUHelper.extractValueFromTagWithParam( + responseData, "", "id", "__EVENTVALIDATION", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ log.info("sessionID: " + sessionID); //$NON-NLS-1$ log.info("viewState: " + viewState); //$NON-NLS-1$ @@ -117,7 +117,7 @@ public class ATrustHandler extends MobileBKUHandler { MobileBKUHelper.registerTrustedSocketFactory(); HttpClient client = BKUHelper.getHttpClient(); - + PostMethod post = new PostMethod(status.getBaseURL() + "/identification.aspx?sid=" + status.getSessionID()); //$NON-NLS-1$ post.getParams().setContentCharset("utf-8"); //$NON-NLS-1$ post.addParameter("__VIEWSTATE", status.getViewstate()); //$NON-NLS-1$ @@ -125,7 +125,7 @@ public class ATrustHandler extends MobileBKUHandler { post.addParameter("handynummer", status.getPhoneNumber()); //$NON-NLS-1$ post.addParameter("signaturpasswort", status.getMobilePassword()); //$NON-NLS-1$ post.addParameter("Button_Identification", "Identifizieren"); //$NON-NLS-1$ //$NON-NLS-2$ - + return executePost(client, post); } @@ -147,7 +147,7 @@ public class ATrustHandler extends MobileBKUHandler { if (responseData.contains("ExpiresInfo.aspx?sid=")) { //$NON-NLS-1$ // Certification expiration interstitial - skip String notice = Messages.getString("mobileBKU.notice") + " " + //$NON-NLS-1$ //$NON-NLS-2$ - StringEscapeUtils.unescapeHtml4(MobileBKUHelper.extractTag(responseData, "", "")) //$NON-NLS-1$ //$NON-NLS-2$ + StringEscapeUtils.unescapeHtml4(MobileBKUHelper.extractContentFromTagWithParam(responseData, "span", "id", "Label2")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ .replaceAll("\\<.*?\\>", ""); //$NON-NLS-1$ //$NON-NLS-2$ log.info(notice); @@ -174,9 +174,9 @@ public class ATrustHandler extends MobileBKUHandler { expiryNoticeDisplayed = true; } - String t_sessionID = MobileBKUHelper.extractTag(responseData, "ExpiresInfo.aspx?sid=", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - String t_viewState = MobileBKUHelper.extractTag(responseData, "id=\"__VIEWSTATE\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - String t_eventValidation = MobileBKUHelper.extractTag(responseData, "id=\"__EVENTVALIDATION\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + String t_sessionID = MobileBKUHelper.extractSubstring(responseData, "ExpiresInfo.aspx?sid=", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + String t_viewState = MobileBKUHelper.extractValueFromTagWithParam(responseData, "", "id", "__VIEWSTATE", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + String t_eventValidation = MobileBKUHelper.extractValueFromTagWithParam(responseData, "", "id", "__EVENTVALIDATION", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ // Post again to skip MobileBKUHelper.registerTrustedSocketFactory(); @@ -195,14 +195,14 @@ public class ATrustHandler extends MobileBKUHandler { if (responseData.contains("signature.aspx?sid=")) { //$NON-NLS-1$ // credentials ok! TAN entry log.debug("Credentials accepted - TAN required"); //$NON-NLS-1$ - sessionID = MobileBKUHelper.extractTag(responseData, "signature.aspx?sid=", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - viewState = MobileBKUHelper.extractTag(responseData, "id=\"__VIEWSTATE\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - eventValidation = MobileBKUHelper.extractTag(responseData, "id=\"__EVENTVALIDATION\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ - refVal = MobileBKUHelper.extractTag(responseData, "id='vergleichswert'>Vergleichswert:", ""); //$NON-NLS-1$ //$NON-NLS-2$ + sessionID = MobileBKUHelper.extractSubstring(responseData, "signature.aspx?sid=", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + viewState = MobileBKUHelper.extractValueFromTagWithParam(responseData, "", "id", "__VIEWSTATE", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + eventValidation = MobileBKUHelper.extractValueFromTagWithParam(responseData, "", "id", "__EVENTVALIDATION", "value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + refVal = MobileBKUHelper.extractSubstring(responseData, "id='vergleichswert'>Vergleichswert:", ""); //$NON-NLS-1$ //$NON-NLS-2$ signatureDataURL = status.getBaseURL() + "/ShowSigobj.aspx" + //$NON-NLS-1$ - MobileBKUHelper.extractTag(responseData, "ShowSigobj.aspx", "'"); //$NON-NLS-1$ //$NON-NLS-2$ + MobileBKUHelper.extractSubstring(responseData, "ShowSigobj.aspx", "'"); //$NON-NLS-1$ //$NON-NLS-2$ try { - qrCode = MobileBKUHelper.extractTag(responseData, "", ""); //$NON-NLS-1$ //$NON-NLS-2$ + String errorMessage = MobileBKUHelper.extractContentFromTagWithParam(responseData, "span", "class", "ErrorClass"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ status.setErrorMessage(errorMessage); } catch (Exception e) { - throw new SignatureException(MobileBKUHelper.extractTag(responseData, "", "") + ": " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - MobileBKUHelper.extractTag(responseData, "", "")); //$NON-NLS-1$ //$NON-NLS-2$ + throw new SignatureException(MobileBKUHelper.extractSubstring(responseData, "", "") + ": " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + MobileBKUHelper.extractSubstring(responseData, "", "")); //$NON-NLS-1$ //$NON-NLS-2$ } // force UI again! @@ -279,7 +279,7 @@ public class ATrustHandler extends MobileBKUHandler { new SLResponse(responseData, getStatus().getServer(), null, null)); } else { try { - String tries = MobileBKUHelper.extractTag( + String tries = MobileBKUHelper.extractSubstring( responseData, "Sie haben noch", "Versuch"); //$NON-NLS-1$ //$NON-NLS-2$ getStatus().setTanTries(Integer.parseInt(tries.trim())); getStatus().setErrorMessage("mobileBKU.wrong_tan"); //$NON-NLS-1$ diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/IAIKHandler.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/IAIKHandler.java index 0904f045..b282811f 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/IAIKHandler.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/IAIKHandler.java @@ -68,7 +68,7 @@ public class IAIKHandler extends MobileBKUHandler { IAIKStatus status = getStatus(); // Extract infos: - String credentialURL = MobileBKUHelper.extractTag(responseData, + String credentialURL = MobileBKUHelper.extractSubstring(responseData, "name=\"userCredLogon\" method=\"post\" action=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ URL baseURL = new URL(status.getBaseURL()); int p = baseURL.getPort(); @@ -76,7 +76,7 @@ public class IAIKHandler extends MobileBKUHandler { credentialURL = baseURL.getProtocol() + "://" + baseURL.getHost() + port + //$NON-NLS-1$ (credentialURL.startsWith("/") ? "" : "/") + credentialURL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - String viewState = MobileBKUHelper.extractTag(responseData, + String viewState = MobileBKUHelper.extractSubstring(responseData, "id=\"javax.faces.ViewState\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ String sessionID = credentialURL.substring(credentialURL.indexOf("jsessionid=") + 11); //$NON-NLS-1$ @@ -133,7 +133,7 @@ public class IAIKHandler extends MobileBKUHandler { String errorMessage; try { - errorMessage = MobileBKUHelper.extractTag(responseData, ":errorMessage\">", ""); //$NON-NLS-1$ //$NON-NLS-2$ + errorMessage = MobileBKUHelper.extractSubstring(responseData, ":errorMessage\">", ""); //$NON-NLS-1$ //$NON-NLS-2$ } catch (Exception e) { errorMessage = Messages.getString("error.Unexpected"); //$NON-NLS-1$ } @@ -149,16 +149,16 @@ public class IAIKHandler extends MobileBKUHandler { String redirectURL = status.getBaseURL().substring(0, status.getBaseURL().lastIndexOf('/', status.getBaseURL().lastIndexOf('/') - 1) + 1); //Cut off last directory - redirectURL += MobileBKUHelper.extractTag(responseData, + redirectURL += MobileBKUHelper.extractSubstring(responseData, "redirection_url\":\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ redirectURL = status.ensureSessionID(redirectURL); responseData = getRedirect(client, redirectURL); - refVal = MobileBKUHelper.extractTag(responseData, + refVal = MobileBKUHelper.extractSubstring(responseData, "id=\"j_idt6:refValue\" class=\"strong\">", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ getStatus().setErrorMessage(errorMessage); } catch (Exception e) { diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/MobileBKUHelper.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/MobileBKUHelper.java index 05746c7c..b9975167 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/MobileBKUHelper.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/MobileBKUHelper.java @@ -56,10 +56,11 @@ public class MobileBKUHelper { * the start marker * @param end * the end marker - * @return the substring + * @return the substring * @throws Exception + * not found */ - public static String extractTag(String data, String start, String end) + public static String extractSubstring(String data, String start, String end) throws Exception { int startidx = data.indexOf(start); if (startidx > 0) { @@ -68,11 +69,106 @@ public class MobileBKUHelper { if (endidx > startidx) { return data.substring(startidx, endidx); } - log.error("extracting Tag: end tag not valid!: " + start + " ... " + end); //$NON-NLS-1$//$NON-NLS-2$ - throw new Exception("end tag not available! Mobile BKU site changed?"); //$NON-NLS-1$ + log.error("extracting substring: end not valid!: " + start + " ... " + end); //$NON-NLS-1$//$NON-NLS-2$ + throw new Exception("End string not available! Mobile BKU site changed?"); //$NON-NLS-1$ } - log.error("extracting Tag: start tag not valid!: " + start + " ... " + end); //$NON-NLS-1$//$NON-NLS-2$ - throw new Exception("start tag not available! Mobile BKU site changed?"); //$NON-NLS-1$ + log.error("extracting substring: start not valid!: " + start + " ... " + end); //$NON-NLS-1$//$NON-NLS-2$ + throw new Exception("Start string not available! Mobile BKU site changed?"); //$NON-NLS-1$ + } + + /** + * Extracts an XML tag from data with the given param="value" + * + * @param data + * the whole data string + * @param tag + * the tag name (empty string to match all tags) + * @param param + * the parameter to look for + * @param value + * the parameter value to look for + * @return the found tag + * @throws Exception + * not found + */ + public static String extractTagWithParam(String data, String tag, + String param, String value) throws Exception { + String start = '<' + tag; + int startidx, endidx = 0; + while ((startidx = data.indexOf(start, endidx)) != -1) { + endidx = data.indexOf('>', startidx); + if (endidx == -1) { + log.error("extracting tag: unterminated tag! " + tag + " (" + param + "=" + value + ")"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + throw new Exception("Tag not found! Mobile BKU site changed?"); //$NON-NLS-1$ + } + String found = data.substring(startidx, endidx + 1); + if (found.contains(param + "='" + value + "'") || //$NON-NLS-1$ //$NON-NLS-2$ + found.contains(param + "=\"" + value + "\"")) //$NON-NLS-1$ //$NON-NLS-2$ + return found; + } + log.error("extracting tag: not found!: " + tag + " (" + param + "='" + value + "')"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + throw new Exception("Tag not found! Mobile BKU site changed?"); //$NON-NLS-1$ + } + + /** + * Extracts a parameter value from an XML tag from data with the given param="value" + * + * @param data + * the whole data string + * @param tag + * the tag name (empty string to match all tags) + * @param param + * the parameter to look for + * @param value + * the parameter value to look for + * @param returnparam + * the parameter whose value to return + * @return the found tag + * @throws Exception + * not found + */ + public static String extractValueFromTagWithParam(String data, String tag, + String param, String value, String returnparam) throws Exception { + String found = extractTagWithParam(data, tag, param, value); + int startidx = found.indexOf(returnparam + '='); + if (startidx == -1) { + log.error("extracting tag: param not found! " + tag + " (" + param + "=" + value + ") - " + returnparam); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + throw new Exception("Tag not found! Mobile BKU site changed?"); //$NON-NLS-1$ + } + startidx += returnparam.length() + 1; + int endidx = found.indexOf(found.charAt(startidx), startidx + 1); + if (endidx == -1) { + log.error("extracting tag: unterminated param value! " + tag + " (" + param + "=" + value + ") - " + returnparam); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + throw new Exception("Tag not found! Mobile BKU site changed?"); //$NON-NLS-1$ + } + return found.substring(startidx + 1, endidx); + } + + /** + * Extracts the content from an XML tag from data with the given param="value" + * + * @param data + * the whole data string + * @param tag + * the tag name + * @param param + * the parameter to look for + * @param value + * the parameter value to look for + * @return the found tag's content + * @throws Exception + * not found + */ + public static String extractContentFromTagWithParam(String data, String tag, + String param, String value) throws Exception { + String found = extractTagWithParam(data, tag, param, value); + int startidx = data.indexOf(found) + found.length(); + int endidx = data.indexOf(""); //$NON-NLS-1$ //$NON-NLS-2$ + if (endidx == -1) { + log.error("extracting tag: closing tag not found! " + tag + " (" + param + "=" + value + ")"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + throw new Exception("Tag not found! Mobile BKU site changed?"); //$NON-NLS-1$ + } + return data.substring(startidx, endidx); } /** -- cgit v1.2.3