diff options
author | Tobias Kellner <tobias.kellner@iaik.tugraz.at> | 2016-02-26 09:33:17 +0100 |
---|---|---|
committer | Tobias Kellner <tobias.kellner@iaik.tugraz.at> | 2016-03-14 17:23:01 +0100 |
commit | c0f137897c8761ffe21e73f009913e08de8e9a71 (patch) | |
tree | ccc20a9d345418cec8be7ef47c851b7b9dbcc16d /pdf-over-gui/src | |
parent | 9cc4fcbe2eab39846d8cd7532bcab26bad85d59d (diff) | |
download | pdf-over-c0f137897c8761ffe21e73f009913e08de8e9a71.tar.gz pdf-over-c0f137897c8761ffe21e73f009913e08de8e9a71.tar.bz2 pdf-over-c0f137897c8761ffe21e73f009913e08de8e9a71.zip |
More flexible HTML parsing for mobile BKU
Way more lenient towards slight page changes now
Diffstat (limited to 'pdf-over-gui/src')
3 files changed, 136 insertions, 40 deletions
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("<sl:ErrorResponse")) { //$NON-NLS-1$ - String errorCode = MobileBKUHelper.extractTag(responseData, + String errorCode = MobileBKUHelper.extractSubstring(responseData, "<sl:ErrorCode>", "</sl:ErrorCode>"); //$NON-NLS-1$ //$NON-NLS-2$ - String errorMsg = MobileBKUHelper.extractTag(responseData, + String errorMsg = MobileBKUHelper.extractSubstring(responseData, "<sl:Info>", "</sl:Info>"); //$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, "<span id=\"Label2\">", "</span>")) //$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'><b>Vergleichswert:</b>", "</div>"); //$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'><b>Vergleichswert:</b>", "</div>"); //$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, "<img class='qrcode' src='", "'"); //$NON-NLS-1$ //$NON-NLS-2$ + qrCode = MobileBKUHelper.extractValueFromTagWithParam(responseData, "img", "class", "qrcode", "src"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ log.debug("QR Code found: " + qrCode); //$NON-NLS-1$ status.setQRCode(qrCode); } catch (Exception e) { @@ -218,11 +218,11 @@ public class ATrustHandler extends MobileBKUHandler { // error page // extract error text! try { - String errorMessage = MobileBKUHelper.extractTag(responseData, "<span id=\"Label1\" class=\"ErrorClass\">", "</span>"); //$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, "<sl:ErrorCode>", "</sl:ErrorCode>") + ": " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - MobileBKUHelper.extractTag(responseData, "<sl:Info>", "</sl:Info>")); //$NON-NLS-1$ //$NON-NLS-2$ + throw new SignatureException(MobileBKUHelper.extractSubstring(responseData, "<sl:ErrorCode>", "</sl:ErrorCode>") + ": " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + MobileBKUHelper.extractSubstring(responseData, "<sl:Info>", "</sl:Info>")); //$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\">", "</span>"); //$NON-NLS-1$ //$NON-NLS-2$ + errorMessage = MobileBKUHelper.extractSubstring(responseData, ":errorMessage\">", "</span>"); //$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$ - String viewState = MobileBKUHelper.extractTag(responseData, + String viewState = MobileBKUHelper.extractSubstring(responseData, "id=\"javax.faces.ViewState\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ status.setViewState(viewState); @@ -186,7 +186,7 @@ public class IAIKHandler extends MobileBKUHandler { return; } - redirectURL = MobileBKUHelper.extractTag(responseData, + redirectURL = MobileBKUHelper.extractSubstring(responseData, "redirect url=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ URL baseURL = new URL(status.getBaseURL()); int p = baseURL.getPort(); @@ -196,7 +196,7 @@ public class IAIKHandler extends MobileBKUHandler { responseData = getRedirect(client, redirectURL); - viewState = MobileBKUHelper.extractTag(responseData, + viewState = MobileBKUHelper.extractSubstring(responseData, "id=\"javax.faces.ViewState\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ status.setViewState(viewState); } @@ -204,11 +204,11 @@ public class IAIKHandler extends MobileBKUHandler { signatureDataURL = status.getBaseURL(); signatureDataURL = signatureDataURL.substring(0, signatureDataURL.lastIndexOf('/') + 1); signatureDataURL += "viewer.jsf" + //$NON-NLS-1$ - MobileBKUHelper.extractTag(responseData, "viewer.jsf", "\""); //$NON-NLS-1$ //$NON-NLS-2$ + MobileBKUHelper.extractSubstring(responseData, "viewer.jsf", "\""); //$NON-NLS-1$ //$NON-NLS-2$ signatureDataURL += (signatureDataURL.contains("?") ? "&" : "?") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ "pdfoversessionid=" + status.getSessionID(); //$NON-NLS-1$ - String tanURL = MobileBKUHelper.extractTag(responseData, + String tanURL = MobileBKUHelper.extractSubstring(responseData, "name=\"j_idt6\" method=\"post\" action=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ URL baseURL = new URL(status.getBaseURL()); int p = baseURL.getPort(); @@ -259,7 +259,7 @@ public class IAIKHandler extends MobileBKUHandler { new SLResponse(responseData, getStatus().getServer(), null, null)); } else { try { - String errorMessage = MobileBKUHelper.extractTag(responseData, + String errorMessage = MobileBKUHelper.extractSubstring(responseData, ":errorMessage\">", "</span>"); //$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("</" + tag + ">"); //$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); } /** |