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/StartSignature.java | 320 +++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java (limited to 'simpleSigning/src/main/java/at/gv/egiz/simpleSigning/StartSignature.java') 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()); + } + } +} -- cgit v1.2.3