From cdfd4153809a9f1f5fed903b06e19f92cdd10d1a Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 22 Jul 2014 10:14:21 +0200 Subject: einfach Signieren first commit --- .../at/gv/egiz/simpleSigning/ErrorSignature.java | 124 ++++++++ .../at/gv/egiz/simpleSigning/FinishSignature.java | 94 ++++++ .../at/gv/egiz/simpleSigning/JQueryServlet.java | 48 ++++ .../at/gv/egiz/simpleSigning/PDFASJsServlet.java | 76 +++++ .../java/at/gv/egiz/simpleSigning/Provider.java | 46 +++ .../at/gv/egiz/simpleSigning/StartSignature.java | 320 +++++++++++++++++++++ .../gv/egiz/simpleSigning/cfg/Configuration.java | 68 +++++ .../at/gv/egiz/simpleSigning/helper/PDFHelper.java | 141 +++++++++ .../egiz/simpleSigning/helper/SessionHelper.java | 126 ++++++++ 9 files changed, 1043 insertions(+) create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/ErrorSignature.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/FinishSignature.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/JQueryServlet.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/PDFASJsServlet.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/Provider.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/cfg/Configuration.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java (limited to 'simpleSigning/src/main/java') diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/ErrorSignature.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/ErrorSignature.java new file mode 100644 index 0000000..446d1c1 --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/ErrorSignature.java @@ -0,0 +1,124 @@ +package at.gv.egiz.simpleSigning; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.simpleSigning.helper.SessionHelper; + +@WebServlet("/Error") +public class ErrorSignature extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 7338000172211889255L; + + public static final String PARAM_ERROR = "error"; + public static final String PARAM_CAUSE = "cause"; + + private static final Logger logger = LoggerFactory + .getLogger(ErrorSignature.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + this.doProcess(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + this.doProcess(req, resp); + } + + protected void doProcess(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + try { + + logger.info("[" + req.getSession().getId() + + "]: error Signature"); + + String error = req.getParameter(PARAM_ERROR); + + if (error == null) { + if (req.getAttribute(PARAM_ERROR) != null) { + error = req.getAttribute(PARAM_ERROR).toString(); + } + } + + if (error == null) { + error = "UNKNOWN ERROR"; + } + + logger.info("[" + req.getSession().getId() + "]: Error: {}", + error); + + String cause = req.getParameter(PARAM_CAUSE); + + if (cause == null) { + if (req.getAttribute(PARAM_CAUSE) != null) { + cause = req.getAttribute(PARAM_CAUSE).toString(); + } + } + + if (cause == null) { + cause = "UNKNOWN CAUSE"; + } + + logger.info("[" + req.getSession().getId() + "]: Cause: {}", + cause); + + + String errorJS = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/error_response.js"))); + + String targetURL = SessionHelper.getTargetURL(req); + + if (targetURL == null) { + targetURL = "invalidURL"; + } + + logger.info("[" + req.getSession().getId() + "]: TargetURL: {}", + targetURL); + + errorJS = errorJS.replace("##REPLACE##ERROR##", error); + errorJS = errorJS.replace("##REPLACE##CAUSE##", cause); + errorJS = errorJS.replace("##REPLACE##TARGETURL##", targetURL); + + String postman = FileUtils + .readFileToString(FileUtils.toFile(PDFASJsServlet.class + .getResource("/js/postman.js"))); + + String combined = postman + errorJS; + + String html = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/script_exec.html"))); + + html = html.replace("##REPLACE##SCRIPT##", combined); + + SessionHelper.killSession(req); + + resp.setContentType("text/html"); + OutputStream os = resp.getOutputStream(); + os.write(html.getBytes()); + os.close(); + + } catch (Throwable e) { + logger.error("[" + req.getSession().getId() + + "]: failed to finish error signature", e); + throw new ServletException("Generic Exception"); + } + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/FinishSignature.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/FinishSignature.java new file mode 100644 index 0000000..33ad8fd --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/FinishSignature.java @@ -0,0 +1,94 @@ +package at.gv.egiz.simpleSigning; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.simpleSigning.helper.SessionHelper; + +@WebServlet("/Finish") +public class FinishSignature extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 7338000172211889255L; + + private static final String PARAM_PDFURL = "pdfurl"; + + private static final Logger logger = LoggerFactory + .getLogger(FinishSignature.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + this.doProcess(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + this.doProcess(req, resp); + } + + protected void doProcess(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + try { + logger.info("[" + req.getSession().getId() + + "]: finishing Signature"); + + String pdfUrl = req.getParameter(PARAM_PDFURL); + + logger.info("[" + req.getSession().getId() + "]: EventID {}", + SessionHelper.getEventID(req)); + logger.info("[" + req.getSession().getId() + "]: PDFURL: {}", + pdfUrl); + logger.info("[" + req.getSession().getId() + "]: TargetURL: {}", + SessionHelper.getTargetURL(req)); + + // Generate JS Code to postMessage to EventId with Success + + String success = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/success_response.js"))); + + success = success.replace("##REPLACE##EVENTID##", + SessionHelper.getEventID(req)); + success = success.replace("##REPLACE##URL##", pdfUrl); + success = success.replace("##REPLACE##TARGETURL##", + SessionHelper.getTargetURL(req)); + + String postman = FileUtils + .readFileToString(FileUtils.toFile(PDFASJsServlet.class + .getResource("/js/postman.js"))); + + String combined = postman + success; + + String html = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/script_exec.html"))); + + html = html.replace("##REPLACE##SCRIPT##", combined); + + SessionHelper.killSession(req); + + resp.setContentType("text/html"); + OutputStream os = resp.getOutputStream(); + os.write(html.getBytes()); + os.close(); + } catch (Throwable e) { + logger.error("[" + req.getSession().getId() + + "]: failed to finish signature", e); + throw new ServletException("Generic Exception"); + } + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/JQueryServlet.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/JQueryServlet.java new file mode 100644 index 0000000..bbed2fa --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/JQueryServlet.java @@ -0,0 +1,48 @@ +package at.gv.egiz.simpleSigning; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; + +@WebServlet("/jquery.js") +public class JQueryServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = -4511221672112082939L; + + public JQueryServlet() { + super(); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doProcess(req, resp); + } + + protected void doProcess(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + String jquery = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/jquery.js"))); + + String postMessage = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/jquery.ba-postmessage.min.js"))); + + resp.setContentType("text/javascript"); + OutputStream os = resp.getOutputStream(); + os.write(jquery.getBytes()); + os.write(postMessage.getBytes()); + os.close(); + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/PDFASJsServlet.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/PDFASJsServlet.java new file mode 100644 index 0000000..4ea456e --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/PDFASJsServlet.java @@ -0,0 +1,76 @@ +package at.gv.egiz.simpleSigning; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.PropertyConfigurator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.simpleSigning.cfg.Configuration; + +public class PDFASJsServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = -2069326194649859734L; + + private static final Logger logger = LoggerFactory + .getLogger(PDFASJsServlet.class); + + private static final String DEFAULT_LOG4J_ENV = "log4j.configuration"; + + public PDFASJsServlet() { + super(); + Configuration.configure(); + + try { + PropertyConfigurator.configure(new FileInputStream(Configuration.getLog4J())); + logger.info("Configured Log4j with: " + Configuration.getLog4J()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.err + .println("Failed to initialize logging System. Defaulting to basic configuration!"); + BasicConfigurator.configure(); + } + + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doProcess(req, resp); + } + + protected void doProcess(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + String pdfJS = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class.getResource("/js/pdfas.js"))); + + pdfJS = pdfJS.replace("##REPLACE##CONTEXT##", + Configuration.getPublicContext()); + pdfJS = pdfJS.replace("##REPLACE##DOMAIN##", + Configuration.getPublicHost()); + + String postman = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class.getResource("/js/postman.js"))); + + resp.setContentType("text/javascript"); + OutputStream os = resp.getOutputStream(); + os.write(postman.getBytes()); + os.write(pdfJS.getBytes()); + os.close(); + } + +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/Provider.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/Provider.java new file mode 100644 index 0000000..7cde301 --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/Provider.java @@ -0,0 +1,46 @@ +package at.gv.egiz.simpleSigning; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +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.simpleSigning.helper.SessionHelper; + +@WebServlet("/Provide") +public class Provider extends HttpServlet { + /** + * + */ + private static final long serialVersionUID = 7338000172211889255L; + + private static final Logger logger = LoggerFactory + .getLogger(Provider.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + try { + byte[] data = SessionHelper.getDocument(req); + if (data == null) { + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + resp.setContentLength(0); + return; + } + + resp.setContentType("application/pdf"); + OutputStream os = resp.getOutputStream(); + os.write(data); + os.close(); + } catch (Throwable e) { + logger.error("Provide failed!", e); + } + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java new file mode 100644 index 0000000..09a9f6d --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java @@ -0,0 +1,320 @@ +package at.gv.egiz.simpleSigning; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Iterator; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; + +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileItemStream; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.simpleSigning.helper.SessionHelper.Type; +import at.gv.egiz.simpleSigning.cfg.Configuration; +import at.gv.egiz.simpleSigning.helper.PDFHelper; +import at.gv.egiz.simpleSigning.helper.SessionHelper; + +@WebServlet(urlPatterns = { "/Start" }) +public class StartSignature extends HttpServlet { + + private static final Logger logger = LoggerFactory + .getLogger(StartSignature.class); + + /** + * + */ + private static final long serialVersionUID = -2069326194649859734L; + + private static final String PARAM_PDF_URL = "PDFURL"; + private static final String PARAM_CONNECTOR = "CONNECTOR"; + private static final String PARAM_EVENTID = "EVENTID"; + private static final String PARAM_TYPE = "TYPE"; + private static final String PARAM_TARGETURL = "TARGETURL"; + + private static final String PARAM_NEW = "NEW"; + private static final String PARAM_NEW_V = "1"; + + public StartSignature() { + super(); + } + + private void doSession(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + String nV = req.getParameter(PARAM_NEW); + if (nV != null && nV.equals(PARAM_NEW_V)) { + SessionHelper.killSession(req); + } + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + this.doSession(req, resp); + this.doProcess(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + this.doSession(req, resp); + + byte[] uploadForm = null; + + boolean isMultipart = ServletFileUpload.isMultipartContent(req); + + if (isMultipart) { + try { + ServletFileUpload upload = new ServletFileUpload(); + FileItemIterator iter = upload.getItemIterator(req); + while (iter.hasNext()) { + FileItemStream fileItem = iter.next(); + if ("pdf-file".equals(fileItem.getFieldName())) { + uploadForm = IOUtils.toByteArray(fileItem.openStream()); + SessionHelper.setDocument(req, uploadForm); + } else if ("txtContent".equals(fileItem.getFieldName())) { + String txtContent = IOUtils.toString(fileItem + .openStream()); + uploadForm = PDFHelper.createPDFDocument(txtContent); + SessionHelper.setDocument(req, uploadForm); + } else if ("connector".equals(fileItem.getFieldName())) { + String connector = IOUtils.toString(fileItem + .openStream()); + SessionHelper.setConnector(req, connector); + } + } + } catch (Throwable e) { + logger.error("Failed to get upload PDF File:", e); + } + } + /* + * try { Part pdfFile = req.getPart("pdf-file"); if(pdfFile != null) { + * uploadForm = IOUtils.toByteArray(pdfFile.getInputStream()); + * SessionHelper.setDocument(req, uploadForm); } } catch(Throwable e) { + * logger.error("Failed to get upload PDF File:", e); } + */ + + String txtContent = req.getParameter("txtContent"); + + if (txtContent != null) { + uploadForm = PDFHelper.createPDFDocument(txtContent); + SessionHelper.setDocument(req, uploadForm); + } + + String connector = req.getParameter("connector"); + + if (connector != null) { + SessionHelper.setConnector(req, connector); + } + + this.doProcess(req, resp); + } + + protected void doProcess(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + try { + + String content = req.getParameter(PARAM_PDF_URL); + String connector = req.getParameter(PARAM_CONNECTOR); + String eventId = req.getParameter(PARAM_EVENTID); + String type = req.getParameter(PARAM_TYPE); + String targetUrl = req.getParameter(PARAM_TARGETURL); + + // Step 1. Setup Parameters + if (type != null) { + // If we have a type set it + SessionHelper.setType(req, type); + + logger.info("[" + req.getSession().getId() + + "]: setting Type to: " + type); + } + + if (eventId != null) { + SessionHelper.setEventID(req, eventId); + + logger.info("[" + req.getSession().getId() + + "]: setting Event Id to: " + eventId); + } + + if (targetUrl != null) { + SessionHelper.setTargetURL(req, targetUrl); + + logger.info("[" + req.getSession().getId() + + "]: setting Target URL to: " + targetUrl); + } + + if (connector != null && connector.length() != 0) { + // If we have a connector set it + SessionHelper.setConnector(req, connector); + logger.info("[" + req.getSession().getId() + + "]: setting Connector to: " + connector); + } + + if (content != null && content.length() != 0) { + // If we have a content set it + SessionHelper.setContent(req, content); + logger.info("[" + req.getSession().getId() + + "]: setting Content"); + + + if (SessionHelper.getType(req).equals(Type.TEXT)) { + SessionHelper.setDocument(req, + PDFHelper.createPDFDocument(content)); + } + + + } + + if ((SessionHelper.getContent(req) == null && SessionHelper + .getDocument(req) == null) + || SessionHelper.getConnector(req) == null) { + + // We need more information + + String form = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/questionForm.html"))); + + String input = ""; + if ((SessionHelper.getContent(req) == null && SessionHelper + .getDocument(req) == null)) { + + if (SessionHelper.getType(req).equals(Type.PDF)) { + input = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/fileupload"))); + } else { + input = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/textinput"))); + } + } + form = form.replace("##REPLACE##FILEUP##", input); + + String bkusel = ""; + if (SessionHelper.getConnector(req) == null) { + bkusel = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/bkuSelektion"))); + + bkusel = bkusel.replace("##REPLACE##PUBURL##", + Configuration.getPublicUrl()); + + } else { + bkusel = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/html/submit"))); + } + form = form.replace("##REPLACE##BKUSEL##", bkusel); + + form = form.replace("##ACTIONURL##", + Configuration.getPublicUrl() + "/Start;jsessionid=" + req.getSession().getId()); + + String resize = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/resize_response.js"))); + + resize = resize.replace("##REPLACE##EVENTID##", + SessionHelper.getEventID(req)); + resize = resize.replace("##REPLACE##TARGETURL##", + SessionHelper.getTargetURL(req)); + + String postman = FileUtils.readFileToString(FileUtils + .toFile(PDFASJsServlet.class + .getResource("/js/postman.js"))); + + 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 + .toFile(PDFASJsServlet.class + .getResource("/html/template_start.html"))); + + if (SessionHelper.getDocument(req) != null) { + template = template.replace("##PDFURL##", + Configuration.getPublicUrl() + "/Provide;jsessionid=" + + req.getSession().getId()); + } else { + if (SessionHelper.getContent(req) == null) { + logger.error("[" + + req.getSession().getId() + + "]: Failed to generate signature Data!: Document is null and Content is null"); + PDFHelper.toError(req, resp, + "Failed to get signature Data", + "Document is null and Content is null"); + return; + } else { + try { + URL url = new URL(SessionHelper.getContent(req)); + logger.info("[" + req.getSession().getId() + + "]: Setting PDF URL: " + url.toExternalForm()); + template = template.replace("##PDFURL##", + url.toExternalForm()); + } catch (MalformedURLException e) { + logger.error( + "[" + + req.getSession().getId() + + "]: Failed to generate signature Data!: Document is null and Content is not an URL: " + + SessionHelper.getContent(req), e); + PDFHelper.toError( + req, + resp, + "Failed to get signature Data", + "Invalid PDF URL: " + + SessionHelper.getContent(req)); + return; + } + } + } + + template = template.replace("##PDFASURL##", + Configuration.getPDFAsLocation() + "/Sign"); + template = template.replace("##INVOKEURL##", + Configuration.getPublicUrl() + "/Finish;jsessionid=" + + req.getSession().getId()); + template = template.replace("##CONNECTOR##", + SessionHelper.getConnector(req)); + template = template.replace("##INVOKETARGET##", "_self"); + template = template.replace("##INVOKEERRORURL##", + Configuration.getPublicUrl() + "/Error;jsessionid=" + + req.getSession().getId()); + + resp.setContentType("text/html"); + OutputStream os = resp.getOutputStream(); + os.write(template.getBytes()); + os.close(); + + } catch (Throwable e) { + logger.error("[" + req.getSession().getId() + + "]: Something went wrong", e); + PDFHelper.toError( + req, + resp, + "Something went wrong", + e.getMessage()); + } + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/cfg/Configuration.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/cfg/Configuration.java new file mode 100644 index 0000000..7b22bc3 --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/cfg/Configuration.java @@ -0,0 +1,68 @@ +package at.gv.egiz.simpleSigning.cfg; + +import java.io.FileInputStream; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Configuration { + + public static final String CONF_FILE = "simpleSign.config"; + + public static final String PUBLIC_HOST = "publichost"; + public static final String PUBLIC_CONTEXT = "publiccontext"; + public static final String PDF_AS_LOCATION = "pdfasurl"; + public static final String LOG4J_FILE = "log4j"; + + private static final Logger logger = LoggerFactory.getLogger(Configuration.class); + + private static Properties properties = new Properties(); + + public static void configure() { + + String config = System.getProperty(CONF_FILE); + + if(config == null) { + logger.error("No web configuration provided! Please specify: " + CONF_FILE); + throw new RuntimeException("No web configuration provided! Please specify: " + CONF_FILE); + } + + properties.clear(); + + try { + properties.load(new FileInputStream(config)); + } catch (Exception e) { + logger.error("Failed to load configuration: " + e.getMessage()); + throw new RuntimeException(e); + } + } + + private static String getStringValue(String configKey, String defaultValue) { + String value = properties.getProperty(configKey); + if(value == null) { + value = defaultValue; + } + return value; + } + + public static String getPublicHost() { + return getStringValue(PUBLIC_HOST, "http://demo.egiz.gv.at"); + } + + public static String getPublicContext() { + return getStringValue(PUBLIC_CONTEXT, "/signSimple"); + } + + public static String getPublicUrl() { + return getPublicHost() + getPublicContext(); + } + + public static String getPDFAsLocation() { + return getStringValue(PDF_AS_LOCATION, "http://demo.egiz.gv.at/pdf-as-web"); + } + + public static String getLog4J() { + return getStringValue(LOG4J_FILE, "log4j.properties"); + } +} diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java new file mode 100644 index 0000000..9c94dac --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java @@ -0,0 +1,141 @@ +package at.gv.egiz.simpleSigning.helper; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.lowagie.text.Document; +import com.lowagie.text.PageSize; +import com.lowagie.text.Paragraph; +import com.lowagie.text.pdf.PdfWriter; + +import at.gv.egiz.simpleSigning.ErrorSignature; + +public class PDFHelper { + + private static final Logger logger = LoggerFactory + .getLogger(PDFHelper.class); + + private static byte[] createPDFiText(String text) { + try { + Document document = new Document(PageSize.A4, 36, 72, 108, 180); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PdfWriter.getInstance(document,baos); + document.open(); + document.add(new Paragraph(text)); + System.out.println("Text is inserted into pdf file"); + document.close(); + baos.close(); + return baos.toByteArray(); + } catch(Throwable e) { + logger.error("Failed to create PDF", e); + } + return null; + } + + /* + private static byte[] createPDFPdfBox(String text) { + + PDDocument document = null; + + try { + // Create a document and add a page to it + document = new PDDocument(); + PDPage page = new PDPage(); + document.addPage(page); + + PDPageContentStream contentStream = new PDPageContentStream(document, page); + + PDFont pdfFont = PDType1Font.HELVETICA; + float fontSize = 25; + float leading = 1.5f * fontSize; + + PDRectangle mediabox = page.findMediaBox(); + float margin = 72; + float width = mediabox.getWidth() - 2*margin; + float startX = mediabox.getLowerLeftX() + margin; + float startY = mediabox.getUpperRightY() - margin; + + List lines = new ArrayList(); + int lastSpace = -1; + while (text.length() > 0) + { + int spaceIndex = text.indexOf(' ', lastSpace + 1); + if (spaceIndex < 0) + { + lines.add(text); + text = ""; + } + else + { + String subString = text.substring(0, spaceIndex); + float size = fontSize * pdfFont.getStringWidth(subString) / 1000; + if (size > width) + { + if (lastSpace < 0) // So we have a word longer than the line... draw it anyways + lastSpace = spaceIndex; + subString = text.substring(0, lastSpace); + lines.add(subString); + text = text.substring(lastSpace).trim(); + lastSpace = -1; + } + else + { + lastSpace = spaceIndex; + } + } + } + + contentStream.beginText(); + contentStream.setFont(pdfFont, fontSize); + contentStream.moveTextPositionByAmount(startX, startY); + for (String line: lines) + { + contentStream.drawString(line); + contentStream.moveTextPositionByAmount(0, -leading); + } + contentStream.endText(); + + + + // Make sure that the content stream is closed: + contentStream.close(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // Save the results and ensure that the document is properly closed: + document.save(baos); + + return baos.toByteArray(); + } catch (Throwable e) { + logger.error("Failed to create PDF", e); + } finally { + if(document != null) { + try { + document.close(); + } catch (Throwable e) { + logger.error("Failed to close PDF", e); + } + } + } + return null; + } + */ + public static byte[] createPDFDocument(String text) { + return createPDFiText(text); + } + + public static void toError(HttpServletRequest req, + HttpServletResponse resp, String error, String cause) + throws ServletException, IOException { + req.setAttribute(ErrorSignature.PARAM_ERROR, error); + req.setAttribute(ErrorSignature.PARAM_CAUSE, cause); + req.getRequestDispatcher("Error").forward(req, resp); + } + +} 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 new file mode 100644 index 0000000..0f2c6b5 --- /dev/null +++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java @@ -0,0 +1,126 @@ +package at.gv.egiz.simpleSigning.helper; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SessionHelper { + + private static final String SESSION_CONTENT = "SESSION_CONTENT"; + private static final String SESSION_TYPE = "SESSION_TYPE"; + private static final String SESSION_TARGETURL = "SESSION_TARGETURL"; + private static final String SESSION_CONNECTOR = "SESSION_CONNECTOR"; + private static final String SESSION_EVENTID = "SESSION_EVENTID"; + private static final String SESSION_DOCUMENT = "SESSION_DOCUMENT"; + + private static final String SESSION_TYPE_TEXT = "TEXT"; + private static final String SESSION_TYPE_PDF = "PDF"; + + private static final Logger logger = LoggerFactory + .getLogger(SessionHelper.class); + + public enum Type { + TEXT, PDF + } + + 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 { + request.getSession().setAttribute(SESSION_TYPE, Type.TEXT); + } + } else { + request.getSession().setAttribute(SESSION_TYPE, Type.TEXT); + } + } + + public static Type getType(HttpServletRequest request) { + Object obj = request.getSession().getAttribute(SESSION_TYPE); + if (obj != null && obj instanceof Type) { + return (Type) obj; + } else { + return Type.TEXT; + } + } + + public static void setContent(HttpServletRequest request, String value) { + request.getSession().setAttribute(SESSION_CONTENT, value); + } + + public static String getContent(HttpServletRequest request) { + Object s = request.getSession().getAttribute(SESSION_CONTENT); + if (s != null) { + return s.toString(); + } else { + return null; + } + } + + public static void setConnector(HttpServletRequest request, String value) { + request.getSession().setAttribute(SESSION_CONNECTOR, value); + } + + public static String getConnector(HttpServletRequest request) { + Object s = request.getSession().getAttribute(SESSION_CONNECTOR); + if (s != null) { + return s.toString(); + } else { + return null; + } + } + + public static void setEventID(HttpServletRequest request, String value) { + request.getSession().setAttribute(SESSION_EVENTID, value); + } + + public static String getEventID(HttpServletRequest request) { + Object s = request.getSession().getAttribute(SESSION_EVENTID); + if (s != null) { + return s.toString(); + } else { + return null; + } + } + + public static void setTargetURL(HttpServletRequest request, String target) { + request.getSession().setAttribute(SESSION_TARGETURL, target); + } + + public static String getTargetURL(HttpServletRequest request) { + Object s = request.getSession().getAttribute(SESSION_TARGETURL); + if (s != null) { + return s.toString(); + } else { + return null; + } + } + + public static void setDocument(HttpServletRequest request, byte[] doc) { + if(doc == null) { + logger.info("[" + request.getSession().getId() + "]: setting Document to NULL!!!"); + } else { + logger.info("[" + request.getSession().getId() + "]: setting Document"); + } + request.getSession().setAttribute(SESSION_DOCUMENT, doc); + } + + public static byte[] getDocument(HttpServletRequest request) { + logger.info("[" + request.getSession().getId() + "]: getting Document"); + HttpSession session = request.getSession(false); + if (session != null) { + Object obj = session.getAttribute(SESSION_DOCUMENT); + if (obj != null && obj instanceof byte[]) { + return (byte[]) obj; + } + } + return null; + } + + public static void killSession(HttpServletRequest request) { + logger.info("Killing Session: " + request.getSession().getId()); + request.getSession().invalidate(); + } +} -- cgit v1.2.3