diff options
Diffstat (limited to 'pdf-as-web/src')
-rw-r--r-- | pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java | 28 | ||||
-rw-r--r-- | pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java | 94 | ||||
-rw-r--r-- | pdf-as-web/src/main/resources/qualified.cer | bin | 0 -> 1257 bytes | |||
-rw-r--r-- | pdf-as-web/src/main/webapp/WEB-INF/web.xml | 10 |
4 files changed, 132 insertions, 0 deletions
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"); + } + } + +} diff --git a/pdf-as-web/src/main/resources/qualified.cer b/pdf-as-web/src/main/resources/qualified.cer Binary files differnew file mode 100644 index 00000000..b448abfb --- /dev/null +++ b/pdf-as-web/src/main/resources/qualified.cer diff --git a/pdf-as-web/src/main/webapp/WEB-INF/web.xml b/pdf-as-web/src/main/webapp/WEB-INF/web.xml index 8373c195..0648ece4 100644 --- a/pdf-as-web/src/main/webapp/WEB-INF/web.xml +++ b/pdf-as-web/src/main/webapp/WEB-INF/web.xml @@ -59,6 +59,12 @@ <servlet-class>at.gv.egiz.pdfas.web.servlets.DataURLServlet</servlet-class> </servlet> <servlet> + <servlet-name>VisBlockServlet</servlet-name> + <display-name>VisBlockServlet</display-name> + <description></description> + <servlet-class>at.gv.egiz.pdfas.web.servlets.VisBlockServlet</servlet-class> + </servlet> + <servlet> <servlet-name>VerifyServlet</servlet-name> <display-name>VerifyServlet</display-name> <description></description> @@ -113,6 +119,10 @@ <url-pattern>/Sign</url-pattern> </servlet-mapping> <servlet-mapping> + <servlet-name>VisBlockServlet</servlet-name> + <url-pattern>/visblock</url-pattern> + </servlet-mapping> + <servlet-mapping> <servlet-name>ProvidePDF</servlet-name> <url-pattern>/ProvidePDF</url-pattern> </servlet-mapping> |