From 030ad8ea8455922ad59c095d3a9ecbfc462ff69f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 14 Jul 2015 13:27:56 +0200 Subject: many fixes for html5 js viewer integration --- .../at/gv/egiz/pdfas/web/helper/PdfAsHelper.java | 194 +++++++++++++++++++-- .../pdfas/web/helper/PdfAsParameterExtractor.java | 29 ++- 2 files changed, 208 insertions(+), 15 deletions(-) (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper') 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 52eb8468..ab23e238 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 @@ -36,6 +36,7 @@ import java.security.cert.CertificateException; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; import javax.imageio.ImageIO; import javax.servlet.RequestDispatcher; @@ -76,6 +77,7 @@ import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.exception.PdfAsWebException; +import at.gv.egiz.pdfas.web.servlets.PDFPositioningDataServlet; import at.gv.egiz.pdfas.web.servlets.UIEntryPointServlet; import at.gv.egiz.pdfas.web.stats.StatisticEvent; import at.gv.egiz.sl.schema.CreateCMSSignatureResponseType; @@ -117,6 +119,16 @@ public class PdfAsHelper { private static final String SIGNATURE_ACTIVE = "SIGNATURE_ACTIVE"; private static final String VERIFICATION_RESULT = "VERIFICATION_RESULT"; private static final String QRCODE_CONTENT = "QR_CONT"; + private static final String PDF_DATA = "PDF_DATA"; + private static final String PDF_DATA_TOKEN = "PDF_DATA_TOKEN"; + private static final String PDF_CONNECTOR = "CONNECTOR"; + private static final String PDF_TRANSACTION = "TID"; + private static final String SIG_TYPE = "SIG_TYPE"; + private static final String PRE_PROCESSOR_MAP = "PREPROCMAP"; + private static final String OVERWRITE_MAP = "OVERWRITEMAP"; + private static final String KEYID = "KEYID"; + + private static final String POSITIONING_URL = "/assets/js/pdf.js/web/viewer.html"; private static final Logger logger = LoggerFactory .getLogger(PdfAsHelper.class); @@ -286,9 +298,54 @@ public class PdfAsHelper { sb.append("f:0;"); } + logger.debug("Build position string: {}", sb.toString()); + return sb.toString(); } + public static String storePdfData(byte[] data, HttpServletRequest request) + throws UnsupportedEncodingException { + String token = URLEncoder.encode(UUID.randomUUID().toString(), "UTF-8"); + HttpSession session = request.getSession(); + session.setAttribute(PDF_DATA, data); + session.setAttribute(PDF_DATA_TOKEN, token); + logger.info("Stored pdf data and token {} into session {}", token, session.getId()); + return token; + } + + public static byte[] getPdfData(String token, HttpServletRequest request) + throws UnsupportedEncodingException { + HttpSession session = request.getSession(); + logger.info("Fetching pdf data and token {} from session {}", token, session.getId()); + String storedToken = (String) session.getAttribute(PDF_DATA_TOKEN); + byte[] data = null; + if (storedToken != null) { + if (storedToken.equals(token)) { + data = (byte[]) session.getAttribute(PDF_DATA); + } else { + logger.warn("PDF Data token missmatch!"); + } + } else { + logger.warn("No PDF Data token stored!"); + } + return data; + } + + public static byte[] getPdfData(HttpServletRequest request) + throws UnsupportedEncodingException { + HttpSession session = request.getSession(); + logger.info("Fetching pdf data from session {}", session.getId()); + byte[] data = (byte[]) session.getAttribute(PDF_DATA); + return data; + } + + public static void clearPdfData(HttpServletRequest request) + throws UnsupportedEncodingException { + HttpSession session = request.getSession(); + logger.info("Removing pdf data from session {}", session.getId()); + session.removeAttribute(PDF_DATA); + } + public static List synchornousVerify( HttpServletRequest request, HttpServletResponse response, byte[] pdfData) throws Exception { @@ -361,10 +418,10 @@ public class PdfAsHelper { Configuration config = pdfAs.getConfiguration(); - - Map configOverwrite = PdfAsParameterExtractor.getOverwriteMap(request); + Map configOverwrite = PdfAsHelper + .getOverwriteMap(request); ConfigurationOverwrite.overwriteConfiguration(configOverwrite, config); - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Generate Sign Parameter @@ -372,7 +429,7 @@ public class PdfAsHelper { new ByteArrayDataSource(pdfData), baos); // Get Connector - String connector = PdfAsParameterExtractor.getConnector(request); + String connector = PdfAsHelper.getConnector(request); if (!connector.equals("moa") && !connector.equals("jks")) { throw new PdfAsWebException("Invalid connector (moa | jks)"); @@ -381,7 +438,7 @@ public class PdfAsHelper { IPlainSigner signer; if (connector.equals("moa")) { - String keyIdentifier = PdfAsParameterExtractor + String keyIdentifier = PdfAsHelper .getKeyIdentifier(request); if (keyIdentifier != null) { @@ -408,7 +465,7 @@ public class PdfAsHelper { signer = new PAdESSigner(new MOAConnector(config)); } else if (connector.equals("jks")) { - String keyIdentifier = PdfAsParameterExtractor + String keyIdentifier = PdfAsHelper .getKeyIdentifier(request); boolean ksEnabled = false; @@ -463,7 +520,7 @@ public class PdfAsHelper { signParameter.setPlainSigner(signer); - String profileId = PdfAsParameterExtractor.getSigType(request); + String profileId = PdfAsHelper.getSignatureType(request); String qrCodeContent = PdfAsHelper.getQRCodeContent(request); if (qrCodeContent != null) { @@ -516,8 +573,10 @@ public class PdfAsHelper { PDFASSignParameters params) throws Exception { Configuration config = pdfAs.getConfiguration(); - if (WebConfiguration.isAllowExtOverwrite() && params.getOverrides() != null) { - ConfigurationOverwrite.overwriteConfiguration(params.getOverrides().getMap(), config); + if (WebConfiguration.isAllowExtOverwrite() + && params.getOverrides() != null) { + ConfigurationOverwrite.overwriteConfiguration(params.getOverrides() + .getMap(), config); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -668,7 +727,8 @@ public class PdfAsHelper { HttpServletResponse response, ServletContext context, byte[] pdfData, String connector, String position, String transactionId, String profile, - Map preProcessor, Map overwrite) throws Exception { + Map preProcessor, Map overwrite) + throws Exception { // TODO: Protect session so that only one PDF can be signed during one // session @@ -689,7 +749,7 @@ public class PdfAsHelper { session.setAttribute(PDF_CONFIG, config); ConfigurationOverwrite.overwriteConfiguration(overwrite, config); - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); session.setAttribute(PDF_OUTPUT, baos); @@ -840,7 +900,7 @@ public class PdfAsHelper { public static void logAccess(HttpServletRequest request) { HttpSession session = request.getSession(); - logger.debug("Access to " + request.getServletPath() + " in Session: " + logger.trace("Access to " + request.getServletPath() + " in Session: " + session.getId()); } @@ -1193,6 +1253,29 @@ public class PdfAsHelper { return generateURL(request, response, PDF_PDFDATA_PAGE); } + public static String generatePositioningURL(String token, + HttpServletRequest request, HttpServletResponse response) { + String publicURL = WebConfiguration.getPublicURL(); + if (publicURL == null) { + logger.error("To use this functionality " + + WebConfiguration.PUBLIC_URL + + " has to be configured in the web configuration"); + return null; + } + + String pdfDataUrl = generateURL(request, response, "/PDFPosData") + "?" + + PDFPositioningDataServlet.PARAM_TOKEN + "=" + token; + + try { + String positioningUrl = generateURL(request, response, POSITIONING_URL) + "?file" + "=" + + URLEncoder.encode(pdfDataUrl, "UTF-8") + "&running=true"; + return positioningUrl; + } catch (UnsupportedEncodingException e) { + logger.error("Failed to generate positioning URL!", e); + return null; + } + } + public static String generateUserEntryURL(String storeId) { String publicURL = WebConfiguration.getPublicURL(); if (publicURL == null) { @@ -1252,6 +1335,51 @@ public class PdfAsHelper { } return ""; } + + public static void setSignatureType(HttpServletRequest request, + String value) { + HttpSession session = request.getSession(); + session.setAttribute(SIG_TYPE, value); + } + + public static String getSignatureType(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(SIG_TYPE); + if (obj != null) { + return obj.toString(); + } + return null; + } + + public static void setPreProcessorMap(HttpServletRequest request, + Map value) { + HttpSession session = request.getSession(); + session.setAttribute(PRE_PROCESSOR_MAP, value); + } + + public static Map getPreProcessorMap(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PRE_PROCESSOR_MAP); + if (obj != null) { + return (Map)obj; + } + return null; + } + + public static void setOverwriteMap(HttpServletRequest request, + Map value) { + HttpSession session = request.getSession(); + session.setAttribute(OVERWRITE_MAP, value); + } + + public static Map getOverwriteMap(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(OVERWRITE_MAP); + if (obj != null) { + return (Map)obj; + } + return null; + } public static void setQRCodeContent(HttpServletRequest request, String value) { HttpSession session = request.getSession(); @@ -1267,6 +1395,48 @@ public class PdfAsHelper { return null; } + public static void setConnector(HttpServletRequest request, String value) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_CONNECTOR, value); + } + + public static String getConnector(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_CONNECTOR); + if (obj != null) { + return obj.toString(); + } + return null; + } + + public static void setKeyIdentifier(HttpServletRequest request, String value) { + HttpSession session = request.getSession(); + session.setAttribute(KEYID, value); + } + + public static String getKeyIdentifier(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(KEYID); + if (obj != null) { + return obj.toString(); + } + return null; + } + + public static void setTransactionid(HttpServletRequest request, String value) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_TRANSACTION, value); + } + + public static String getTransactionid(HttpServletRequest request) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_TRANSACTION); + if (obj != null) { + return obj.toString(); + } + return null; + } + public static void setPDFFileName(HttpServletRequest request, String value) { HttpSession session = request.getSession(); session.setAttribute(PDF_FILE_NAME, value); diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java index 1c515efa..53335ba6 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java @@ -62,6 +62,9 @@ public class PdfAsParameterExtractor { public static final String PARAM_SIG_POS_P = "sig-pos-p"; public static final String PARAM_SIG_POS_Y = "sig-pos-y"; public static final String PARAM_SIG_POS_X = "sig-pos-x"; + public static final String PARAM_SIG_POS_P_ALT = "sigPosP"; + public static final String PARAM_SIG_POS_Y_ALT = "sigPosY"; + public static final String PARAM_SIG_POS_X_ALT = "sigPosX"; public static final String PARAM_SIG_POS_W = "sig-pos-w"; public static final String PARAM_SIG_POS_R = "sig-pos-r"; public static final String PARAM_SIG_POS_F = "sig-pos-f"; @@ -71,7 +74,15 @@ public class PdfAsParameterExtractor { public static final String PARAM_PREPROCESSOR_PREFIX = "pp:"; public static final String PARAM_OVERWRITE_PREFIX = "ov:"; public static final String PARAM_QRCODE_CONTENT = "qrcontent"; + public static final String PARAM_USER_POSITIONING = "upos"; + public static boolean isUserPositioning(HttpServletRequest request) { + String paramerterValue = (String)request.getAttribute(PARAM_USER_POSITIONING); + if(paramerterValue != null) { + return Boolean.parseBoolean(paramerterValue); + } + return false; + } public static String getConnector(HttpServletRequest request) { String connector = (String)request.getAttribute(PARAM_CONNECTOR); @@ -201,15 +212,27 @@ public class PdfAsParameterExtractor { } public static String getSigPosP(HttpServletRequest request) { - return (String)request.getAttribute(PARAM_SIG_POS_P); + if(request.getAttribute(PARAM_SIG_POS_P) != null) { + return (String)request.getAttribute(PARAM_SIG_POS_P); + } else { + return (String)request.getAttribute(PARAM_SIG_POS_P_ALT); + } } public static String getSigPosY(HttpServletRequest request) { - return (String)request.getAttribute(PARAM_SIG_POS_Y); + if(request.getAttribute(PARAM_SIG_POS_Y) != null) { + return (String)request.getAttribute(PARAM_SIG_POS_Y); + } else { + return (String)request.getAttribute(PARAM_SIG_POS_Y_ALT); + } } public static String getSigPosX(HttpServletRequest request) { - return (String)request.getAttribute(PARAM_SIG_POS_X); + if(request.getAttribute(PARAM_SIG_POS_X) != null) { + return (String)request.getAttribute(PARAM_SIG_POS_X); + } else { + return (String)request.getAttribute(PARAM_SIG_POS_X_ALT); + } } public static String getSigPosW(HttpServletRequest request) { -- cgit v1.2.3