From 308bb7903ca0a293438bf4039a194ec098645ffd Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 15 Jul 2014 18:49:39 +0200 Subject: added JSON capabilities to Verify Servlet --- .../at/gv/egiz/pdfas/web/helper/VerifyEncoder.java | 17 ++++ .../egiz/pdfas/web/helper/VerifyResultEncoder.java | 15 ++++ .../pdfas/web/helper/VerifyResultHTMLEncoder.java | 98 ++++++++++++++++++++++ .../pdfas/web/helper/VerifyResultJSONEncoder.java | 78 +++++++++++++++++ .../gv/egiz/pdfas/web/servlets/VerifyServlet.java | 97 ++++----------------- 5 files changed, 224 insertions(+), 81 deletions(-) create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyEncoder.java create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultEncoder.java create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultHTMLEncoder.java create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/VerifyResultJSONEncoder.java (limited to 'pdf-as-web/src/main/java/at') 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 encoders = new HashMap(); + + 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 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 results) throws IOException { + StringBuilder sb = new StringBuilder(); + + sb.append(""); + sb.append("

Verification Results for: " + PdfAsHelper.getPDFFileName(request) + "

"); + sb.append(""); + + sb.append(""); + + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + + sb.append(""); + + for (int i = 0; i < results.size(); i++) { + VerifyResult result = results.get(i); + sb.append(""); + + sb.append(""); + + if (result.isVerificationDone()) { + sb.append(""); + 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(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + if(e != null) { + sb.append(""); + } else { + sb.append(""); + } + if(result.isQualifiedCertificate()) { + sb.append(""); + } else { + sb.append(""); + } + sb.append(""); + + } else { + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + } + + + sb.append(""); + } + sb.append("
SignatureProcessedSigned ByCert CodeCert MessageValue CodeValue MessageErrorCertificateSigned Data
" + i + "YES" + cert.getSubjectDN().getName() + "" + certCode + "" + certMessage + "" + valueCode + "" + valueMessage + "" + e.getMessage() + "-here (QC)herehereNO--------
"); + sb.append(""); + + 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 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(); + } + +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VerifyServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VerifyServlet.java index e9d1f246..09af4357 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VerifyServlet.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VerifyServlet.java @@ -23,11 +23,8 @@ ******************************************************************************/ package at.gv.egiz.pdfas.web.servlets; -import iaik.x509.X509Certificate; - import java.io.File; import java.io.IOException; -import java.io.OutputStream; import java.util.List; import javax.servlet.ServletException; @@ -42,11 +39,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter.SignatureVerificationLevel; import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; import at.gv.egiz.pdfas.web.exception.PdfAsWebException; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; import at.gv.egiz.pdfas.web.helper.PdfAsParameterExtractor; import at.gv.egiz.pdfas.web.helper.RemotePDFFetcher; +import at.gv.egiz.pdfas.web.helper.VerifyEncoder; +import at.gv.egiz.pdfas.web.helper.VerifyResultEncoder; /** * Servlet implementation class VerifyServlet @@ -243,11 +243,18 @@ public class VerifyServlet extends HttpServlet { protected void doVerify(HttpServletRequest request, HttpServletResponse response, byte[] pdfData) throws Exception { + SignatureVerificationLevel lvl = PdfAsParameterExtractor + .getVerificationLevel(request); + PdfAsHelper.setVerificationLevel(request, lvl); + + String format = PdfAsParameterExtractor.getFormat(request); + logger.debug("doVerify"); logger.info("Starting verification of pdf dokument"); - List results = PdfAsHelper.synchornousVerify(request, - response, pdfData); + logger.debug("Format: " + format); + + List results = PdfAsHelper.synchornousVerify(pdfData, -1, lvl); PdfAsHelper.setVerificationResult(request, results); @@ -256,85 +263,13 @@ public class VerifyServlet extends HttpServlet { // Or create a JSON response with the verification results for automated // processing - StringBuilder sb = new StringBuilder(); - - sb.append(""); - sb.append("

Verification Results for: " + PdfAsHelper.getPDFFileName(request) + "

"); - sb.append(""); + VerifyResultEncoder encoder = VerifyEncoder.getEncoder(format); - sb.append(""); - - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - - sb.append(""); - - for (int i = 0; i < results.size(); i++) { - VerifyResult result = results.get(i); - sb.append(""); - - sb.append(""); - - if (result.isVerificationDone()) { - sb.append(""); - 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(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - if(e != null) { - sb.append(""); - } else { - sb.append(""); - } - if(result.isQualifiedCertificate()) { - sb.append(""); - } else { - sb.append(""); - } - sb.append(""); - - } else { - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - } - - - sb.append(""); + if(encoder == null) { + encoder = VerifyEncoder.getEncoder(PdfAsParameterExtractor.PARAM_HTML); } - sb.append("
SignatureProcessedSigned ByCert CodeCert MessageValue CodeValue MessageErrorCertificateCertificateSigned Data
" + i + "YES" + cert.getSubjectDN().getName() + "" + certCode + "" + certMessage + "" + valueCode + "" + valueMessage + "" + e.getMessage() + "-here (QC)herehereNO--------
"); - sb.append(""); - response.setContentType("text/html"); - OutputStream os = response.getOutputStream(); - os.write(sb.toString().getBytes()); - os.close(); + encoder.produce(request, response, results); } } -- cgit v1.2.3