From 2436aa273f00dafb465c2342ea8e7297898915d2 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 21 Jan 2014 09:57:16 +0100 Subject: Implementations for pdf-as-web and pdf-as-legacy --- .../gv/egiz/pdfas/web/config/WebConfiguration.java | 101 +++++++++++++++++++-- .../at/gv/egiz/pdfas/web/helper/PdfAsHelper.java | 24 ++--- .../at/gv/egiz/pdfas/web/servlets/ErrorPage.java | 5 +- .../egiz/pdfas/web/servlets/ExternSignServlet.java | 28 +++++- pdf-as-web/src/main/webapp/WEB-INF/web.xml | 2 +- .../src/main/webapp/assets/img/mobileBKU.png | Bin 0 -> 9742 bytes .../src/main/webapp/assets/img/onlineBKU.png | Bin 0 -> 6650 bytes pdf-as-web/src/main/webapp/index.jsp | 59 +++++++++--- pdf-as-web/src/test/pdf-as-web.properties | 27 ++++++ 9 files changed, 207 insertions(+), 39 deletions(-) create mode 100644 pdf-as-web/src/main/webapp/assets/img/mobileBKU.png create mode 100644 pdf-as-web/src/main/webapp/assets/img/onlineBKU.png create mode 100644 pdf-as-web/src/test/pdf-as-web.properties (limited to 'pdf-as-web/src') diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/config/WebConfiguration.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/config/WebConfiguration.java index eb04dde8..3c6a7f21 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/config/WebConfiguration.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/config/WebConfiguration.java @@ -1,24 +1,111 @@ package at.gv.egiz.pdfas.web.config; +import java.io.File; +import java.io.FileInputStream; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class WebConfiguration { + + public static final String PUBLIC_URL = "public.url"; + public static final String LOCAL_BKU_URL = "bku.local.url"; + public static final String ONLINE_BKU_URL = "bku.online.url"; + public static final String MOBILE_BKU_URL = "bku.mobile.url"; + public static final String ERROR_DETAILS = "error.showdetails"; + public static final String PDF_AS_WORK_DIR = "pdfas.dir"; + + public static final String KEYSTORE_ENABLED = "ks.enabled"; + public static final String KEYSTORE_FILE = "ks.file"; + public static final String KEYSTORE_TYPE = "ks.type"; + public static final String KEYSTORE_PASS = "ks.pass"; + public static final String KEYSTORE_ALIAS = "ks.key.alias"; + public static final String KEYSTORE_KEY_PASS = "ks.key.pass"; + + private static Properties properties = new Properties(); + + private static final Logger logger = LoggerFactory + .getLogger(WebConfiguration.class); + + public static void configure(String config) { + try { + properties.load(new FileInputStream(config)); + } catch(Exception e) { + logger.error("Failed to load configuration: " + e.getMessage()); + throw new RuntimeException(e); + } + + String pdfASDir = getPdfASDir(); + if(pdfASDir == null) { + logger.error("Please configure pdf as working directory in the web configuration"); + throw new RuntimeException("Please configure pdf as working directory in the web configuration"); + } + + File f = new File(pdfASDir); + + if(!f.exists() || !f.isDirectory()) { + logger.error("Pdf As working directory does not exists or is not a directory!: " + pdfASDir); + throw new RuntimeException("Pdf As working directory does not exists or is not a directory!"); + } + } + public static String getPublicURL() { - return null; + return properties.getProperty(PUBLIC_URL); } public static String getLocalBKUURL() { - // TODO: Read URL from config - return "http://127.0.0.1:3495/http-security-layer-request"; + return properties.getProperty(LOCAL_BKU_URL); } public static String getOnlineBKUURL() { - // TODO: Read URL from config - return "http://abyss.iaik.tugraz.at/bkuonline/http-security-layer-request"; + return properties.getProperty(ONLINE_BKU_URL); } public static String getHandyBKUURL() { - // TODO: Read URL from config - return "http://127.0.0.1:3495/http-security-layer-request"; + return properties.getProperty(MOBILE_BKU_URL); + } + + public static String getPdfASDir() { + return properties.getProperty(PDF_AS_WORK_DIR); + } + + public static String getKeystoreFile() { + return properties.getProperty(KEYSTORE_FILE); + } + public static String getKeystoreType() { + return properties.getProperty(KEYSTORE_TYPE); + } + public static String getKeystorePass() { + return properties.getProperty(KEYSTORE_PASS); + } + public static String getKeystoreAlias() { + return properties.getProperty(KEYSTORE_ALIAS); + } + public static String getKeystoreKeyPass() { + return properties.getProperty(KEYSTORE_KEY_PASS); + } + + + public static boolean getKeystoreEnabled() { + String value = properties.getProperty(KEYSTORE_ENABLED); + if(value != null) { + if(value.equals("true")) { + return true; + } + } + return false; + } + + public static boolean isShowErrorDetails() { + String value = properties.getProperty(ERROR_DETAILS); + if(value != null) { + if(value.equals("true")) { + return true; + } + } + return false; } } 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 daf18108..e61a113a 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 @@ -66,14 +66,6 @@ public class PdfAsHelper { private static final String PDF_INVOKE_URL = "PDF_INVOKE_URL"; private static final String REQUEST_FROM_DU = "REQ_DATA_URL"; - // For development only: - public static final String keyStoreFile = "/home/afitzek/devel/pdfas_neu/test.p12"; - public static final String keyStoreType = "PKCS12"; - public static final String keyStorePass = "123456"; - // public static final String keyAlias = "pdf"; - public static final String keyAlias = "ecc_test"; - public static final String keyPass = "123456"; - private static final Logger logger = LoggerFactory .getLogger(PdfAsHelper.class); @@ -83,9 +75,14 @@ public class PdfAsHelper { static { // TODO: read from config file logger.debug("Creating PDF-AS"); - pdfAs = PdfAsFactory.createPdfAs(new File("/home/afitzek/.pdfas")); + pdfAs = PdfAsFactory.createPdfAs(new File(WebConfiguration.getPdfASDir())); logger.debug("Creating PDF-AS done"); } + + public static void init() { + logger.debug("PDF-AS Helper initialized"); + } + private static void validatePdfSize(HttpServletRequest request, HttpServletResponse response, byte[] pdfData) @@ -218,11 +215,10 @@ public class PdfAsHelper { if (connector.equals("moa")) { signer = new PAdESSigner(new MOAConnector(config)); } else { - // TODO: - // signer = new PAdESSignerKeystore(file, alias, kspassword, - // keypassword, type) - signer = new PKCS7DetachedSigner(keyStoreFile, keyAlias, - keyStorePass, keyPass, keyStoreType); + signer = new PKCS7DetachedSigner(WebConfiguration.getKeystoreFile(), + WebConfiguration.getKeystoreAlias(), + WebConfiguration.getKeystorePass(), WebConfiguration.getKeystoreKeyPass(), + WebConfiguration.getKeystoreType()); } signParameter.setPlainSigner(signer); diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ErrorPage.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ErrorPage.java index fceeed85..fe436566 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ErrorPage.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ErrorPage.java @@ -12,6 +12,7 @@ import javax.swing.text.html.HTML; import org.apache.commons.lang3.StringEscapeUtils; +import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.helper.HTMLFormater; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; @@ -64,7 +65,7 @@ public class ErrorPage extends HttpServlet { String template = PdfAsHelper.getErrorRedirectTemplateSL(); template = template.replace("##ERROR_URL##", errorURL); - if (e != null) { + if (e != null && WebConfiguration.isShowErrorDetails()) { template = template.replace("##CAUSE##", URLEncoder.encode(e.getMessage(), "UTF-8")); } else { @@ -88,7 +89,7 @@ public class ErrorPage extends HttpServlet { pw.write("

" + message + "

"); } - if (e != null) { + if (e != null && WebConfiguration.isShowErrorDetails()) { pw.write("

" + HTMLFormater.formatStackTrace(e.getStackTrace()) + "

"); diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java index 90c77320..c96225bd 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java @@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; +import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.exception.PdfAsWebException; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; import at.gv.egiz.pdfas.web.helper.PdfAsParameterExtractor; @@ -33,6 +34,8 @@ public class ExternSignServlet extends HttpServlet { private static final long serialVersionUID = 1L; + public static final String PDF_AS_WEB_CONF = "pdf-as-web.conf"; + private static final String UPLOAD_PDF_DATA = "pdfFile"; private static final String UPLOAD_DIRECTORY = "upload"; private static final int THRESHOLD_SIZE = 1024 * 1024 * 3; // 3MB @@ -46,6 +49,15 @@ public class ExternSignServlet extends HttpServlet { * Default constructor. */ public ExternSignServlet() { + String webconfig = System.getProperty(PDF_AS_WEB_CONF); + + if(webconfig == null) { + logger.error("No web configuration provided! Please specify: " + PDF_AS_WEB_CONF); + throw new RuntimeException("No web configuration provided! Please specify: " + PDF_AS_WEB_CONF); + } + + WebConfiguration.configure(webconfig); + PdfAsHelper.init(); } protected void doGet(HttpServletRequest request, @@ -142,7 +154,12 @@ public class ExternSignServlet extends HttpServlet { FileItem item = (FileItem) obj; if(item.getFieldName().equals(UPLOAD_PDF_DATA)) { filecontent = item.get(); - logger.debug("Found pdf Data!"); + + if(filecontent.length < 10) { + filecontent = null; + } else { + logger.debug("Found pdf Data! Size: " + filecontent.length); + } } else { request.setAttribute(item.getFieldName(), item.getString()); logger.debug("Setting " + item.getFieldName() + " = " + item.getString()); @@ -161,6 +178,15 @@ public class ExternSignServlet extends HttpServlet { } if(filecontent == null) { + Object sourceObj = request.getAttribute("source"); + if(sourceObj != null) { + String source = sourceObj.toString(); + if(source.equals("internal")) { + request.setAttribute("FILEERR", true); + request.getRequestDispatcher("index.jsp").forward(request, response); + return; + } + } throw new PdfAsException("No Signature data available"); } 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 81fa6dad..40642fcc 100644 --- a/pdf-as-web/src/main/webapp/WEB-INF/web.xml +++ b/pdf-as-web/src/main/webapp/WEB-INF/web.xml @@ -35,7 +35,7 @@ The Sign Servlet allows Users to Sign PDF Documents ... at.gv.egiz.pdfas.web.servlets.ExternSignServlet - 5 + 0 ProvidePDF diff --git a/pdf-as-web/src/main/webapp/assets/img/mobileBKU.png b/pdf-as-web/src/main/webapp/assets/img/mobileBKU.png new file mode 100644 index 00000000..48e77934 Binary files /dev/null and b/pdf-as-web/src/main/webapp/assets/img/mobileBKU.png differ diff --git a/pdf-as-web/src/main/webapp/assets/img/onlineBKU.png b/pdf-as-web/src/main/webapp/assets/img/onlineBKU.png new file mode 100644 index 00000000..2ec41892 Binary files /dev/null and b/pdf-as-web/src/main/webapp/assets/img/onlineBKU.png differ diff --git a/pdf-as-web/src/main/webapp/index.jsp b/pdf-as-web/src/main/webapp/index.jsp index 2dd79101..8aba0dff 100644 --- a/pdf-as-web/src/main/webapp/index.jsp +++ b/pdf-as-web/src/main/webapp/index.jsp @@ -1,21 +1,52 @@ +<%@page import="at.gv.egiz.pdfas.web.config.WebConfiguration"%> PDF-Signatur -
-
- - -

Zu signierende PDF Datei

-
-
- - - - - -
-
+
+ +
has-error <% } %>"> + +

+ <% if(request.getAttribute("FILEERR") != null) { %> + Bitte die zu signierende PDF Datei angeben. + <% } else { %> + Zu signierende PDF Datei + <% } %>

+
+ <% if(WebConfiguration.getOnlineBKUURL() != null || + WebConfiguration.getLocalBKUURL() != null) { %> +
+ + + <% if(WebConfiguration.getLocalBKUURL() != null) { %> + + <% } %> + <% if(WebConfiguration.getOnlineBKUURL() != null) { %> + + <% } %> +
+ <% } %> + <% if(WebConfiguration.getHandyBKUURL() != null) { %> +
+ + + +
+ <% } %> + <% if(WebConfiguration.getKeystoreEnabled()) { %> +
+ + +
+ <% } %> +
\ No newline at end of file diff --git a/pdf-as-web/src/test/pdf-as-web.properties b/pdf-as-web/src/test/pdf-as-web.properties new file mode 100644 index 00000000..8f60c63f --- /dev/null +++ b/pdf-as-web/src/test/pdf-as-web.properties @@ -0,0 +1,27 @@ + +# Define Public URL prefix for PDF-AS Web. For example if behind a proxy, or in a cluster +#public.url= + +error.showdetails=true + +pdfas.dir= + +#BKU URLs. To deactivate a BKU, just uncomment it. + +# URL for Local BKU +bku.local.url=http://127.0.0.1:3495/http-security-layer-request + +# URL for Online BKU +bku.online.url=http://abyss.iaik.tugraz.at/bkuonline/http-security-layer-request + +# URL for Mobile BKU +#bku.mobile.url= + + +# Support Keystore +ks.enabled=true +ks.file=/home/afitzek/devel/pdfas_neu/test.p12 +ks.type=PKCS12 +ks.pass=123456 +ks.key.alias=ecc_test +ks.key.pass=123456 \ No newline at end of file -- cgit v1.2.3