From 8e71c5c2fe0bbd588b95dd4158dd1e378c146e07 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 1 Jul 2014 17:21:59 +0200 Subject: PDF-AS Web Signature Block Preview Service --- .../at/gv/egiz/pdfas/web/helper/PdfAsHelper.java | 28 +++++++ .../egiz/pdfas/web/servlets/VisBlockServlet.java | 94 ++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java (limited to 'pdf-as-web/src/main/java/at/gv/egiz') diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java index 85a36e17..041c7df3 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java @@ -23,13 +23,21 @@ ******************************************************************************/ package at.gv.egiz.pdfas.web.helper; +import iaik.x509.X509Certificate; + +import java.awt.Image; +import java.awt.image.RenderedImage; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.security.cert.CertificateException; import java.util.Iterator; import java.util.List; +import javax.imageio.ImageIO; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -45,6 +53,7 @@ import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.api.ws.PDFASSignParameters; import at.gv.egiz.pdfas.api.ws.PDFASSignParameters.Connector; +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; @@ -468,6 +477,25 @@ public class PdfAsHelper { return data; } + public static byte[] generateVisualBlock(String profile, int resolution) throws IOException, CertificateException, PdfAsException { + X509Certificate cert = new X509Certificate(PdfAsHelper.class.getResourceAsStream("/qualified.cer")); + Configuration config = pdfAs.getConfiguration(); + SignParameter parameter = PdfAsFactory.createSignParameter( + config, null); + parameter.setSignatureProfileId(profile); + Image img = pdfAs.generateVisibleSignaturePreview(parameter, + cert, resolution); + + if(img == null) { + return null; + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write((RenderedImage) img, "png", baos); + baos.close(); + return baos.toByteArray(); + } + public static void injectCertificate(HttpServletRequest request, HttpServletResponse response, InfoboxReadResponseType infoboxReadResponseType, diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java new file mode 100644 index 00000000..3008c25e --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java @@ -0,0 +1,94 @@ +package at.gv.egiz.pdfas.web.servlets; + +import java.io.IOException; +import java.io.OutputStream; +import java.security.cert.CertificateException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.web.helper.PdfAsHelper; + +public class VisBlockServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private static final Logger logger = LoggerFactory + .getLogger(VisBlockServlet.class); + + private static String PARAM_PROFILE = "p"; + private static String PARAM_RESOLUTION = "r"; + + /** + * @see HttpServlet#HttpServlet() + */ + public VisBlockServlet() { + super(); + } + + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + this.process(request, response); + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + this.process(request, response); + } + + protected void process(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + + try { + + String profile = request.getParameter(PARAM_PROFILE); + String resolutionString = request.getParameter(PARAM_RESOLUTION); + + int resolution = Integer.parseInt(resolutionString); + + if (resolution < 16 || resolution > 512) { + throw new ServletException(PARAM_RESOLUTION + + " invalid value! Out of Range"); + } + + byte[] imageData = PdfAsHelper.generateVisualBlock(profile, resolution); + + if (imageData != null) { + + response.setHeader("Content-Disposition", "inline;filename=" + + profile + "_" + resolution + ".png"); + response.setContentType("image/png"); + OutputStream os = response.getOutputStream(); + os.write(imageData); + os.close(); + } else { + logger.warn("generateVisualBlock returned null!"); + throw new ServletException("Invalid profile id"); + } + } catch (NumberFormatException e) { + throw new ServletException(PARAM_RESOLUTION + + " invalid value! Not a Number"); + } catch (CertificateException e) { + logger.error("CERT Error", e); + throw new ServletException("Failed to find certificate"); + } catch (PdfAsException e) { + logger.error("PDF_AS Error", e); + throw new ServletException("Generic Error"); + } + } + +} -- cgit v1.2.3