diff options
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper')
4 files changed, 208 insertions, 0 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyEncoder.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyEncoder.java new file mode 100644 index 00000000..88038ed0 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyEncoder.java @@ -0,0 +1,17 @@ +package at.gv.egiz.pdfas.web.helper; + +import java.util.HashMap; +import java.util.Map; + +public class VerifyEncoder { + private static Map<String, VerifyResultEncoder> encoders = new HashMap<String, VerifyResultEncoder>(); + + static { + encoders.put(PdfAsParameterExtractor.PARAM_HTML, new VerifyResultHTMLEncoder()); + encoders.put(PdfAsParameterExtractor.PARAM_JSON, new VerifyResultJSONEncoder()); + } + + public static VerifyResultEncoder getEncoder(String type) { + return encoders.get(type); + } +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultEncoder.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultEncoder.java new file mode 100644 index 00000000..42a4068a --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultEncoder.java @@ -0,0 +1,15 @@ +package at.gv.egiz.pdfas.web.helper; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; + +public interface VerifyResultEncoder { + public void produce(HttpServletRequest request, + HttpServletResponse response, + List<VerifyResult> results) throws IOException; +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultHTMLEncoder.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultHTMLEncoder.java new file mode 100644 index 00000000..c53b0c4d --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultHTMLEncoder.java @@ -0,0 +1,98 @@ +package at.gv.egiz.pdfas.web.helper; + +import iaik.x509.X509Certificate; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; + +public class VerifyResultHTMLEncoder implements VerifyResultEncoder { + + public void produce(HttpServletRequest request, + HttpServletResponse response, List<VerifyResult> results) throws IOException { + StringBuilder sb = new StringBuilder(); + + sb.append("<html><head><title></title></head><body>"); + sb.append("<h3>Verification Results for: " + PdfAsHelper.getPDFFileName(request) + "</h3>"); + sb.append("<table style=\"width:100%\" border='1' >"); + + sb.append("<tr>"); + + sb.append("<th>Signature</th>"); + sb.append("<th>Processed</th>"); + sb.append("<th>Signed By</th>"); + sb.append("<th>Cert Code</th>"); + sb.append("<th>Cert Message</th>"); + sb.append("<th>Value Code</th>"); + sb.append("<th>Value Message</th>"); + sb.append("<th>Error</th>"); + sb.append("<th>Certificate</th>"); + sb.append("<th>Signed Data</th>"); + + sb.append("</tr>"); + + for (int i = 0; i < results.size(); i++) { + VerifyResult result = results.get(i); + sb.append("<tr>"); + + sb.append("<td>" + i + "</td>"); + + if (result.isVerificationDone()) { + sb.append("<td>YES</td>"); + int certCode = result.getCertificateCheck().getCode(); + String certMessage = result.getCertificateCheck().getMessage(); + + int valueCode = result.getValueCheckCode().getCode(); + String valueMessage = result.getValueCheckCode().getMessage(); + + Exception e = result.getVerificationException(); + + X509Certificate cert = (X509Certificate)result.getSignerCertificate(); + + sb.append("<td>" + cert.getSubjectDN().getName() + "</td>"); + sb.append("<td>" + certCode + "</td>"); + sb.append("<td>" + certMessage + "</td>"); + sb.append("<td>" + valueCode + "</td>"); + sb.append("<td>" + valueMessage + "</td>"); + if(e != null) { + sb.append("<td>" + e.getMessage() + "</td>"); + } else { + sb.append("<td>-</td>"); + } + if(result.isQualifiedCertificate()) { + sb.append("<td><a href=\"signCert?SIGID=" + i + "\">here</a> (QC)</td>"); + } else { + sb.append("<td><a href=\"signCert?SIGID=" + i + "\">here</a></td>"); + } + sb.append("<td><a href=\"signData?SIGID=" + i + "\">here</a></td>"); + + } else { + sb.append("<td>NO</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + sb.append("<td>-</td>"); + } + + + sb.append("</tr>"); + } + sb.append("</table>"); + sb.append("</body></html>"); + + response.setContentType("text/html"); + OutputStream os = response.getOutputStream(); + os.write(sb.toString().getBytes()); + os.close(); + } + +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultJSONEncoder.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultJSONEncoder.java new file mode 100644 index 00000000..c2d0aeb9 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultJSONEncoder.java @@ -0,0 +1,78 @@ +package at.gv.egiz.pdfas.web.helper; + +import iaik.x509.X509Certificate; + +import java.io.IOException; +import java.io.OutputStream; +import java.security.cert.CertificateEncodingException; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; + +import org.apache.commons.codec.binary.Base64; + +public class VerifyResultJSONEncoder implements VerifyResultEncoder { + + private static final Logger logger = LoggerFactory + .getLogger(VerifyResultJSONEncoder.class); + + public void produce(HttpServletRequest request, + HttpServletResponse response, List<VerifyResult> results) + throws IOException { + StringBuilder sb = new StringBuilder(); + + sb.append("{\"signatures\":["); + for (int i = 0; i < results.size(); i++) { + VerifyResult result = results.get(i); + + X509Certificate cert = (X509Certificate) result + .getSignerCertificate(); + + int certCode = result.getCertificateCheck().getCode(); + String certMessage = result.getCertificateCheck().getMessage(); + + int valueCode = result.getValueCheckCode().getCode(); + String valueMessage = result.getValueCheckCode().getMessage(); + + Exception e = result.getVerificationException(); + + sb.append("{"); + if (result.isVerificationDone()) { + sb.append("\"processed\":\"" + result.isVerificationDone() + + "\", "); + sb.append("\"signedBy\":\"" + cert.getSubjectDN().getName() + + "\", "); + sb.append("\"certCode\":\"" + certCode + "\", "); + sb.append("\"certMessage\":\"" + certMessage + "\", "); + sb.append("\"valueCode\":\"" + valueCode + "\", "); + sb.append("\"valueMessage\":\"" + valueMessage + "\""); + if (e != null) { + sb.append(", "); + sb.append("\"error\":\"" + e.getMessage() + + "\""); + } + sb.append(", "); + sb.append("\"certificate\":\"signCert?SIGID=" + i + "\", "); + sb.append("\"signedData\":\"signData?SIGID=" + i + "\""); + } else { + sb.append("\"processed\":\"" + result.isVerificationDone() + + "\""); + } + + sb.append("}"); + } + sb.append("]}"); + + response.setContentType("application/json"); + OutputStream os = response.getOutputStream(); + os.write(sb.toString().getBytes()); + os.close(); + } + +} |