From cddaad0f298689208a8a5a4a8c714be8154d260d Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 10 Dec 2019 20:33:38 +0000 Subject: [PATCH] move certificate code in a separate file --- .../java/com/muwire/webui/BrowseServlet.java | 1 + webui/src/main/js/browse.js | 6 +- webui/src/main/js/certificates.js | 179 ++++++++++++++++++ webui/src/main/js/search.js | 177 ----------------- webui/src/main/webapp/Home.jsp | 5 +- 5 files changed, 187 insertions(+), 181 deletions(-) create mode 100644 webui/src/main/js/certificates.js diff --git a/webui/src/main/java/com/muwire/webui/BrowseServlet.java b/webui/src/main/java/com/muwire/webui/BrowseServlet.java index fe72ff39..d88ee7ea 100644 --- a/webui/src/main/java/com/muwire/webui/BrowseServlet.java +++ b/webui/src/main/java/com/muwire/webui/BrowseServlet.java @@ -81,6 +81,7 @@ public class BrowseServlet extends HttpServlet { if (result.getComment() != null) { sb.append("").append(Util.escapeHTMLinXML(result.getComment())).append(""); } + sb.append("").append(result.getCertificates()).append(""); // TODO: add more fields sb.append(""); }); diff --git a/webui/src/main/js/browse.js b/webui/src/main/js/browse.js index a061474b..cc2977dc 100644 --- a/webui/src/main/js/browse.js +++ b/webui/src/main/js/browse.js @@ -1,10 +1,11 @@ class Result { - constructor(name, size, comment, infoHash, downloading) { + constructor(name, size, comment, infoHash, downloading, certificates) { this.name = name this.size = size this.infoHash = infoHash this.comment = comment this.downloading = downloading + this.certificates = certificates } } @@ -108,8 +109,9 @@ function showResults(host) { comment = comment[0].childNodes[0].nodeValue else comment = null + var certificates = results[i].getElementsByTagName("Certificates")[0].childNodes[0].nodeValue - var result = new Result(name, size, comment, infoHash, downloading) + var result = new Result(name, size, comment, infoHash, downloading, certificates) resultsByInfoHash.set(infoHash, result) } diff --git a/webui/src/main/js/certificates.js b/webui/src/main/js/certificates.js new file mode 100644 index 00000000..f1c0cc1d --- /dev/null +++ b/webui/src/main/js/certificates.js @@ -0,0 +1,179 @@ +var certificateFetches = new Map() +var expandedCertificateComments = new Map() + +class Certificate { + constructor(xmlNode, divId) { + this.divId = divId + this.issuer = xmlNode.getElementsByTagName("Issuer")[0].childNodes[0].nodeValue + this.name = xmlNode.getElementsByTagName("Name")[0].childNodes[0].nodeValue + this.comment = null + try { + this.comment = xmlNode.getElementsByTagName("Comment")[0].childNodes[0].nodeValue + } catch(ignore) {} + this.timestamp = xmlNode.getElementsByTagName("Timestamp")[0].childNodes[0].nodeValue + this.base64 = xmlNode.getElementsByTagName("Base64")[0].childNodes[0].nodeValue + this.imported = xmlNode.getElementsByTagName("Imported")[0].childNodes[0].nodeValue + } + + getViewCommentBlock() { + if (this.comment == null) + return "" + var id = this.divId + "_" + this.base64 + + if (expandedCertificateComments.get(id)) { + var linkText = _t("Hide Comment") + var link = "" + linkText + "" + var html = "" + html += "
" + html += "
" + this.comment + "
" + html += "
" + return html + } else { + var linkText = _t("Show Comment") + var link = "" + linkText + "" + var linkBlock = "" + + "
" + return linkBlock + } + } + + getImportLink() { + var linkText = _t("Import") + var link = "" + linkText + "" + return link + } + + renderRow() { + var commentPresent = "false" + if (this.comment != null) + commentPresent = "true" + + var html = "" + html += "" + this.issuer + this.getViewCommentBlock() + "" + html += "" + this.name + "" + html += "" + this.timestamp + "" + + if (this.imported == "true") + html += "" + _t("Imported") + "" + else + html += "" + this.getImportLink() + "" + + html += "" + return html + } +} + +class CertificateResponse { + constructor(xmlNode, divId) { + this.status = xmlNode.getElementsByTagName("Status")[0].childNodes[0].nodeValue + this.total = xmlNode.getElementsByTagName("Total")[0].childNodes[0].nodeValue + this.divId = divId + + var certNodes = xmlNode.getElementsByTagName("Certificates")[0].getElementsByTagName("Certificate") + var i + this.certificates = [] + this.certificatesBy64 = new Map() + for (i = 0; i < certNodes.length; i++) { + var certificate = new Certificate(certNodes[i], this.divId) + this.certificates.push(certificate) + this.certificatesBy64.set(certificate.base64, certificate) + } + } + + renderTable() { + var html = _t("Status") + " " + this.status + if (this.certificates.length == 0) + return html + + html += " " + html += _t("Certificates") + " " + this.certificates.length + "/" + this.total + + var headers = [_t("Issuer"), _t("Name"), _t("Timestamp"), _t("Import")] + html += "
" + html += "" + var i + for(i = 0; i < this.certificates.length; i++) { + html += this.certificates[i].renderRow() + } + html += "
" + headers.join("") + "
" + + return html + } +} + +class CertificateFetch { + constructor(senderB64, fileInfoHash) { + this.senderB64 = senderB64 + this.fileInfoHash = fileInfoHash + this.divId = senderB64 + "_" + fileInfoHash + this.lastResponse = null + } + + updateTable() { + var fetch = this + var block = document.getElementById("certificates-" + this.divId) + + var xmlhttp = new XMLHttpRequest() + xmlhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + fetch.lastResponse = new CertificateResponse(this.responseXML, fetch.divId) + block.innerHTML = fetch.lastResponse.renderTable() + } + } + xmlhttp.open("GET", "/MuWire/Certificate?user=" + this.senderB64 + "&infoHash=" + this.fileInfoHash, true) + xmlhttp.send() + } +} + +function importCertificate(b64) { + var xmlhttp = new XMLHttpRequest() + xmlhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + refreshCertificates() + } + } + xmlhttp.open("POST", "/MuWire/Certificate", true) + xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xmlhttp.send("action=import&base64=" + b64) +} + +function showCertificateComment(divId, base64) { + var certificateResponse = certificateFetches.get(divId).lastResponse + var certificate = certificateResponse.certificatesBy64.get(base64) + expandedCertificateComments.set(divId + "_" + base64, true) + + var linkDiv = document.getElementById("certificate-comment-link-" + divId + "_" + base64) + var linkText = _t("Hide Comment") + var link = "" + linkText + "" + linkDiv.innerHTML = link + + var commentDiv = document.getElementById("certificate-comment-" + divId + "_" + base64) + var commentHtml = "
" + certificate.comment + "
" + commentDiv.innerHTML = commentHtml + +} + +function hideCertificateComment(divId, base64) { + var certificateResponse = certificateFetches.get(divId).lastResponse + var certificate = certificateResponse.certificatesBy64.get(base64) + expandedCertificateComments.delete(divId + "_" + base64) + + var linkDiv = document.getElementById("certificate-comment-link-" + divId + "_" + base64) + var linkText = _t("Show Comment") + var link = "" + linkText + "" + linkDiv.innerHTML = link + + var commentDiv = document.getElementById("certificate-comment-" + divId + "_" + base64) + commentDiv.innerHTML = "" +} + +function refreshCertificates() { + for (var [ignored, fetch] of certificateFetches) { + fetch.updateTable() + } +} + +function initCertificates() { + setInterval(refreshCertificates, 3000) + setTimeout(refreshCertificates, 1) +} diff --git a/webui/src/main/js/search.js b/webui/src/main/js/search.js index 6bb41f9f..14bf8a4c 100644 --- a/webui/src/main/js/search.js +++ b/webui/src/main/js/search.js @@ -173,165 +173,6 @@ class ResultByFile { } } -function showCertificateComment(divId, base64) { - var certificateResponse = certificateFetches.get(divId).lastResponse - var certificate = certificateResponse.certificatesBy64.get(base64) - expandedCertificateComments.set(divId + "_" + base64, true) - - var linkDiv = document.getElementById("certificate-comment-link-" + divId + "_" + base64) - var linkText = _t("Hide Comment") - var link = "" + linkText + "" - linkDiv.innerHTML = link - - var commentDiv = document.getElementById("certificate-comment-" + divId + "_" + base64) - var commentHtml = "
" + certificate.comment + "
" - commentDiv.innerHTML = commentHtml - -} - -function hideCertificateComment(divId, base64) { - var certificateResponse = certificateFetches.get(divId).lastResponse - var certificate = certificateResponse.certificatesBy64.get(base64) - expandedCertificateComments.delete(divId + "_" + base64) - - var linkDiv = document.getElementById("certificate-comment-link-" + divId + "_" + base64) - var linkText = _t("Show Comment") - var link = "" + linkText + "" - linkDiv.innerHTML = link - - var commentDiv = document.getElementById("certificate-comment-" + divId + "_" + base64) - commentDiv.innerHTML = "" -} - -class Certificate { - constructor(xmlNode, divId) { - this.divId = divId - this.issuer = xmlNode.getElementsByTagName("Issuer")[0].childNodes[0].nodeValue - this.name = xmlNode.getElementsByTagName("Name")[0].childNodes[0].nodeValue - this.comment = null - try { - this.comment = xmlNode.getElementsByTagName("Comment")[0].childNodes[0].nodeValue - } catch(ignore) {} - this.timestamp = xmlNode.getElementsByTagName("Timestamp")[0].childNodes[0].nodeValue - this.base64 = xmlNode.getElementsByTagName("Base64")[0].childNodes[0].nodeValue - this.imported = xmlNode.getElementsByTagName("Imported")[0].childNodes[0].nodeValue - } - - getViewCommentBlock() { - if (this.comment == null) - return "" - var id = this.divId + "_" + this.base64 - - if (expandedCertificateComments.get(id)) { - var linkText = _t("Hide Comment") - var link = "" + linkText + "" - var html = "" - html += "
" - html += "
" + this.comment + "
" - html += "
" - return html - } else { - var linkText = _t("Show Comment") - var link = "" + linkText + "" - var linkBlock = "" + - "
" - return linkBlock - } - } - - getImportLink() { - var linkText = _t("Import") - var link = "" + linkText + "" - return link - } - - renderRow() { - var commentPresent = "false" - if (this.comment != null) - commentPresent = "true" - - var html = "" - html += "" + this.issuer + this.getViewCommentBlock() + "" - html += "" + this.name + "" - html += "" + this.timestamp + "" - - if (this.imported == "true") - html += "" + _t("Imported") + "" - else - html += "" + this.getImportLink() + "" - - html += "" - return html - } -} - -class CertificateResponse { - constructor(xmlNode, divId) { - this.status = xmlNode.getElementsByTagName("Status")[0].childNodes[0].nodeValue - this.total = xmlNode.getElementsByTagName("Total")[0].childNodes[0].nodeValue - this.divId = divId - - var certNodes = xmlNode.getElementsByTagName("Certificates")[0].getElementsByTagName("Certificate") - var i - this.certificates = [] - this.certificatesBy64 = new Map() - for (i = 0; i < certNodes.length; i++) { - var certificate = new Certificate(certNodes[i], this.divId) - this.certificates.push(certificate) - this.certificatesBy64.set(certificate.base64, certificate) - } - } - - renderTable() { - var html = _t("Status") + " " + this.status - if (this.certificates.length == 0) - return html - - html += " " - html += _t("Certificates") + " " + this.certificates.length + "/" + this.total - - var headers = [_t("Issuer"), _t("Name"), _t("Timestamp"), _t("Import")] - html += "
" - html += "" - var i - for(i = 0; i < this.certificates.length; i++) { - html += this.certificates[i].renderRow() - } - html += "
" + headers.join("") + "
" - - return html - } -} - -class CertificateFetch { - constructor(senderB64, fileInfoHash) { - this.senderB64 = senderB64 - this.fileInfoHash = fileInfoHash - this.divId = senderB64 + "_" + fileInfoHash - this.lastResponse = null - } - - updateTable() { - var fetch = this - var block = document.getElementById("certificates-" + this.divId) - - var xmlhttp = new XMLHttpRequest() - xmlhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - fetch.lastResponse = new CertificateResponse(this.responseXML, fetch.divId) - block.innerHTML = fetch.lastResponse.renderTable() - } - } - xmlhttp.open("GET", "/MuWire/Certificate?user=" + this.senderB64 + "&infoHash=" + this.fileInfoHash, true) - xmlhttp.send() - } -} - -function refreshCertificates() { - for (var [ignored, fetch] of certificateFetches) { - fetch.updateTable() - } -} var statusByUUID = new Map() var currentSearchBySender = null @@ -339,8 +180,6 @@ var currentSearchByFile = null var currentSender = null var currentFile = null var expandedComments = new Map(); -var certificateFetches = new Map() -var expandedCertificateComments = new Map() var uuid = null; var sender = null; @@ -746,18 +585,6 @@ function hideCertificatesBySender(fileInfoHash, count) { linkSpan.innerHTML = showLink } -function importCertificate(b64) { - var xmlhttp = new XMLHttpRequest() - xmlhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - refreshCertificates() - } - } - xmlhttp.open("POST", "/MuWire/Certificate", true) - xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - xmlhttp.send("action=import&base64=" + b64) -} - function refreshStatus() { var xmlhttp = new XMLHttpRequest() xmlhttp.onreadystatechange = function() { @@ -807,8 +634,6 @@ function initGroupBySender() { refreshType = "Sender" setInterval(refreshStatus, 3000); setTimeout(refreshStatus, 1); - setInterval(refreshCertificates, 3000) - setTimeout(refreshCertificates, 1) } function initGroupByFile() { @@ -816,6 +641,4 @@ function initGroupByFile() { refreshType = "File" setInterval ( refreshStatus, 3000); setTimeout ( refreshStatus, 1); - setInterval(refreshCertificates, 3000) - setTimeout(refreshCertificates, 1) } diff --git a/webui/src/main/webapp/Home.jsp b/webui/src/main/webapp/Home.jsp index 3eed9df1..f5bedbb4 100644 --- a/webui/src/main/webapp/Home.jsp +++ b/webui/src/main/webapp/Home.jsp @@ -19,12 +19,13 @@ <%@include file="css.jsi"%> + <% if (groupBy.equals("sender")) { %> - + <% } else { %> - + <% } %> <%@include file="header.jsi"%>