diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-07-15 18:49:39 +0200 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-07-15 18:49:39 +0200 |
commit | 308bb7903ca0a293438bf4039a194ec098645ffd (patch) | |
tree | 08cf1e7d2ac37a48d50dfd212717251f58c80df9 /pdf-as-web/src/main/java/at/gv/egiz/pdfas/web | |
parent | 7194ad1f6a22507867f471fd6c8a37c6596d4b8a (diff) | |
download | pdf-as-4-308bb7903ca0a293438bf4039a194ec098645ffd.tar.gz pdf-as-4-308bb7903ca0a293438bf4039a194ec098645ffd.tar.bz2 pdf-as-4-308bb7903ca0a293438bf4039a194ec098645ffd.zip |
added JSON capabilities to Verify Servlet
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web')
5 files changed, 224 insertions, 81 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(); + } + +} 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<VerifyResult> results = PdfAsHelper.synchornousVerify(request, - response, pdfData); + logger.debug("Format: " + format); + + List<VerifyResult> 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("<html><head><title></title></head><body>"); - sb.append("<h3>Verification Results for: " + PdfAsHelper.getPDFFileName(request) + "</h3>"); - sb.append("<table style=\"width:100%\" border='1' >"); + VerifyResultEncoder encoder = VerifyEncoder.getEncoder(format); - 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>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>"); + if(encoder == null) { + encoder = VerifyEncoder.getEncoder(PdfAsParameterExtractor.PARAM_HTML); } - sb.append("</table>"); - sb.append("</body></html>"); - response.setContentType("text/html"); - OutputStream os = response.getOutputStream(); - os.write(sb.toString().getBytes()); - os.close(); + encoder.produce(request, response, results); } } |