From dbd565ec8195117306ae869f4e7b507ff275ed32 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 25 Feb 2015 13:45:34 +0100 Subject: added QR Code generation + Base64 Data provising --- simpleSigning/build.gradle | 1 + .../at/gv/egiz/simpleSigning/StartSignature.java | 101 ++++++++------- .../egiz/simpleSigning/helper/SessionHelper.java | 20 ++- .../src/main/resources/html/template_start.html | 1 + simpleSigning/src/main/resources/js/pdfas.js | 13 +- simpleSigning/src/main/webapp/WEB-INF/web.xml | 31 +++-- simpleSigning/src/main/webapp/index.jsp | 136 +++++++++++++++++++++ 7 files changed, 247 insertions(+), 56 deletions(-) diff --git a/simpleSigning/build.gradle b/simpleSigning/build.gradle index 07f6a20..1bf4cf2 100644 --- a/simpleSigning/build.gradle +++ b/simpleSigning/build.gradle @@ -34,6 +34,7 @@ dependencies { compile "commons-codec:commons-codec:1.9" //compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '1.8.5' compile 'com.lowagie:itext:4.2.0' + compile 'com.thetransactioncompany:cors-filter:2.2.1' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' testCompile group: 'junit', name: 'junit', version: '4.+' } diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java index e018499..5c3927e 100644 --- a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java @@ -10,6 +10,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; @@ -40,6 +41,7 @@ public class StartSignature extends HttpServlet { private static final String PARAM_TARGETURL = "TARGETURL"; private static final String PARAM_RESIZE = "RESIZE"; + private static final String PARAM_QRCODE = "QRCODE"; private static final String PARAM_NEW = "NEW"; private static final String PARAM_NEW_V = "1"; @@ -80,7 +82,7 @@ public class StartSignature extends HttpServlet { FileItemStream fileItem = iter.next(); if ("pdf-file".equals(fileItem.getFieldName())) { uploadForm = IOUtils.toByteArray(fileItem.openStream()); - if(uploadForm.length > 5) { + if (uploadForm.length > 5) { SessionHelper.setDocument(req, uploadForm); } else { logger.info("No Document uploaded"); @@ -133,6 +135,7 @@ public class StartSignature extends HttpServlet { String eventId = req.getParameter(PARAM_EVENTID); String type = req.getParameter(PARAM_TYPE); String targetUrl = req.getParameter(PARAM_TARGETURL); + String qrCodeData = req.getParameter(PARAM_QRCODE); // Step 1. Setup Parameters if (type != null) { @@ -143,6 +146,14 @@ public class StartSignature extends HttpServlet { + "]: setting Type to: " + type); } + if (qrCodeData != null) { + // If we have a type set it + SessionHelper.setQRCodeContent(req, qrCodeData); + + logger.info("[" + req.getSession().getId() + + "]: setting QR Code Data to: " + qrCodeData); + } + if (eventId != null) { SessionHelper.setEventID(req, eventId); @@ -169,14 +180,15 @@ public class StartSignature extends HttpServlet { SessionHelper.setContent(req, content); logger.info("[" + req.getSession().getId() + "]: setting Content"); - - + if (SessionHelper.getType(req).equals(Type.TEXT)) { SessionHelper.setDocument(req, PDFHelper.createPDFDocument(content)); + } else if (SessionHelper.getType(req).equals(Type.B64)) { + SessionHelper + .setDocument(req, Base64.decodeBase64(content)); } - } if ((SessionHelper.getContent(req) == null && SessionHelper @@ -218,19 +230,22 @@ public class StartSignature extends HttpServlet { bkusel = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/html/submit"))); - - if (SessionHelper.getType(req) != null && - SessionHelper.getType(req).equals(Type.PDF)) { - bkusel = bkusel.replace("##REPLACE##NAME##", "Dokument signieren"); + + if (SessionHelper.getType(req) != null + && SessionHelper.getType(req).equals(Type.PDF)) { + bkusel = bkusel.replace("##REPLACE##NAME##", + "Dokument signieren"); } else { - bkusel = bkusel.replace("##REPLACE##NAME##", "Text signieren"); + bkusel = bkusel.replace("##REPLACE##NAME##", + "Text signieren"); } - + } form = form.replace("##REPLACE##BKUSEL##", bkusel); form = form.replace("##ACTIONURL##", - Configuration.getPublicUrl() + "/Start;jsessionid=" + req.getSession().getId()); + Configuration.getPublicUrl() + "/Start;jsessionid=" + + req.getSession().getId()); String resize = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class @@ -257,20 +272,21 @@ public class StartSignature extends HttpServlet { } // Resize Iframe for BKU communication - + String resizeDone = req.getParameter(PARAM_RESIZE); - - if(resizeDone != null && resizeDone.equals("1")) { + + if (resizeDone != null && resizeDone.equals("1")) { SessionHelper.setResizeDone(req); } - - if(!SessionHelper.getResizeDone(req)) { + + if (!SessionHelper.getResizeDone(req)) { String form = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/html/resize_form.html"))); form = form.replace("##ACTIONURL##", - Configuration.getPublicUrl() + "/Start;jsessionid=" + req.getSession().getId()); - + Configuration.getPublicUrl() + "/Start;jsessionid=" + + req.getSession().getId()); + String resize = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/js/resizing_response.js"))); @@ -280,26 +296,20 @@ public class StartSignature extends HttpServlet { resize = resize.replace("##REPLACE##TARGETURL##", SessionHelper.getTargetURL(req)); - // Handy Signatur: style="width: 255px; height: 250px;" - + // Online BKU: width: 225px; height: 225px; - - if(SessionHelper.getConnector(req).equals("mobilebku")) { - resize = resize.replace("##REPLACE##HEIGHT##", - "255"); - - resize = resize.replace("##REPLACE##WIDTH##", - "250"); + + if (SessionHelper.getConnector(req).equals("mobilebku")) { + resize = resize.replace("##REPLACE##HEIGHT##", "255"); + + resize = resize.replace("##REPLACE##WIDTH##", "250"); } else { - resize = resize.replace("##REPLACE##HEIGHT##", - "225"); - - resize = resize.replace("##REPLACE##WIDTH##", - "225"); + resize = resize.replace("##REPLACE##HEIGHT##", "225"); + + resize = resize.replace("##REPLACE##WIDTH##", "225"); } - - + String postman = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/js/postman.js"))); @@ -307,15 +317,15 @@ public class StartSignature extends HttpServlet { String script_txt = postman + resize; form = form.replace("##REPLACE##SCRIPT##", script_txt); - + resp.setContentType("text/html"); OutputStream os = resp.getOutputStream(); os.write(form.getBytes()); os.close(); - + return; } - + // Step 4. Start Signature Process with PDF-AS String template = FileUtils.readFileToString(FileUtils @@ -358,7 +368,15 @@ public class StartSignature extends HttpServlet { } } } - + String entry = ""; + if (SessionHelper.getQRCodeContent(req) != null) { + entry = ""; + } + + template = template.replace("##ADDITIONAL##", + entry); + template = template.replace("##PDFASURL##", Configuration.getPDFAsLocation() + "/Sign"); template = template.replace("##INVOKEURL##", @@ -379,11 +397,8 @@ public class StartSignature extends HttpServlet { } catch (Throwable e) { logger.error("[" + req.getSession().getId() + "]: Something went wrong", e); - PDFHelper.toError( - req, - resp, - "Something went wrong", - e.getMessage()); + PDFHelper + .toError(req, resp, "Something went wrong", e.getMessage()); } } } diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java index 8815b87..720c93c 100644 --- a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java @@ -16,20 +16,25 @@ public class SessionHelper { private static final String SESSION_DOCUMENT = "SESSION_DOCUMENT"; private static final String SESSION_RESIZE_DONE = "SESSION_RESIZE_DONE"; + private static final String SESSION_QRCODE_CONTENT = "SESSION_QRCODE_CONTENT"; + private static final String SESSION_TYPE_TEXT = "TEXT"; private static final String SESSION_TYPE_PDF = "PDF"; + private static final String SESSION_TYPE_BASE64 = "B64"; private static final Logger logger = LoggerFactory .getLogger(SessionHelper.class); public enum Type { - TEXT, PDF + TEXT, PDF, B64 } public static void setType(HttpServletRequest request, String value) { if (value != null) { if (value.equals(SESSION_TYPE_PDF)) { request.getSession().setAttribute(SESSION_TYPE, Type.PDF); + } else if (value.equals(SESSION_TYPE_BASE64)) { + request.getSession().setAttribute(SESSION_TYPE, Type.B64); } else { request.getSession().setAttribute(SESSION_TYPE, Type.TEXT); } @@ -47,6 +52,19 @@ public class SessionHelper { } } + public static void setQRCodeContent(HttpServletRequest request, String value) { + request.getSession().setAttribute(SESSION_QRCODE_CONTENT, value); + } + + public static String getQRCodeContent(HttpServletRequest request) { + Object s = request.getSession().getAttribute(SESSION_QRCODE_CONTENT); + if (s != null) { + return s.toString(); + } else { + return null; + } + } + public static void setContent(HttpServletRequest request, String value) { request.getSession().setAttribute(SESSION_CONTENT, value); } diff --git a/simpleSigning/src/main/resources/html/template_start.html b/simpleSigning/src/main/resources/html/template_start.html index 19e780b..9c48304 100644 --- a/simpleSigning/src/main/resources/html/template_start.html +++ b/simpleSigning/src/main/resources/html/template_start.html @@ -20,6 +20,7 @@ function onAnmeldeSubmit() { + ##ADDITIONAL## diff --git a/simpleSigning/src/main/resources/js/pdfas.js b/simpleSigning/src/main/resources/js/pdfas.js index 1017fee..30f27f9 100644 --- a/simpleSigning/src/main/resources/js/pdfas.js +++ b/simpleSigning/src/main/resources/js/pdfas.js @@ -37,11 +37,15 @@ var pdfAs = {}; * @returns */ pdfAs.signText = function(content, error, success) { - this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, "TEXT", success, error); + this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, content.qrdata, "TEXT", success, error); }; pdfAs.signPdf = function(content, error, success) { - this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, "PDF", success, error); + this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, content.qrdata, "PDF", success, error); +}; + +pdfAs.signPdfBase64 = function(content, error, success) { + this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, content.qrdata, "B64", success, error); }; pdfAs.getVersion = function(cb, err) { @@ -132,7 +136,7 @@ pdfAs.autoResize = function(eventId, height, width){ element.style.margin = '-' + element.scrollHeight / 2 + 'px 0 0 -' + element.scrollWidth / 2 + 'px'; }; -pdfAs.createAsyncSignature = function(pdfUrl, connector, divID, pdfAsURL, type, success, error) { +pdfAs.createAsyncSignature = function(pdfUrl, connector, divID, pdfAsURL, qrcode, type, success, error) { // generate EventID var eventId = guid(); @@ -142,7 +146,8 @@ pdfAs.createAsyncSignature = function(pdfUrl, connector, divID, pdfAsURL, type, EVENTID: eventId, TYPE: type, TARGETURL: document.URL, - NEW: "1" + NEW: "1", + QRCODE: qrcode }; /*var querystring = encodeQueryData(parameters); diff --git a/simpleSigning/src/main/webapp/WEB-INF/web.xml b/simpleSigning/src/main/webapp/WEB-INF/web.xml index 4e292a8..44aa624 100644 --- a/simpleSigning/src/main/webapp/WEB-INF/web.xml +++ b/simpleSigning/src/main/webapp/WEB-INF/web.xml @@ -8,6 +8,21 @@ Simple Signing Simple Signing Application + + CORS + com.thetransactioncompany.cors.CORSFilter + + + cors.allowOrigin + * + + + + + CORS + /* + + PDFASJsServlet PDFASJsServlet @@ -15,7 +30,7 @@ at.gv.egiz.simpleSigning.PDFASJsServlet 0 - + StartSignature StartSignature @@ -23,7 +38,7 @@ at.gv.egiz.simpleSigning.StartSignature 0 - + Provider Provider @@ -31,7 +46,7 @@ at.gv.egiz.simpleSigning.Provider 0 - + JQueryServlet JQueryServlet @@ -47,7 +62,7 @@ at.gv.egiz.simpleSigning.FinishSignature 0 - + ErrorSignature ErrorSignature @@ -55,7 +70,7 @@ at.gv.egiz.simpleSigning.ErrorSignature 0 - + VersionServlet VersionServlet @@ -63,7 +78,7 @@ at.gv.egiz.simpleSigning.VersionServlet 0 - + -- cgit v1.2.3