From 55af1b61fbbee1a0c553ba463398e4646c692224 Mon Sep 17 00:00:00 2001 From: tzefferer Date: Fri, 27 Jul 2007 08:58:12 +0000 Subject: git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@164 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/web/servlets/SignServlet.java | 176 ++++++++++++++++++--- 1 file changed, 151 insertions(+), 25 deletions(-) (limited to 'src/main/java/at') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java index a98fb79..54be8df 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java @@ -17,8 +17,11 @@ */ package at.knowcenter.wag.egov.egiz.web.servlets; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URL; @@ -54,6 +57,7 @@ import at.knowcenter.wag.egov.egiz.framework.SignResult; import at.knowcenter.wag.egov.egiz.framework.Signator; import at.knowcenter.wag.egov.egiz.framework.SignatorFactory; import at.knowcenter.wag.egov.egiz.framework.signators.DetachedSignator_1_0_0; +import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.sig.ConnectorFactory; import at.knowcenter.wag.egov.egiz.sig.SignatureData; import at.knowcenter.wag.egov.egiz.sig.SignatureDataImpl; @@ -65,9 +69,11 @@ import at.knowcenter.wag.egov.egiz.sig.connectors.bku.EnvelopedBase64BKUConnecto import at.knowcenter.wag.egov.egiz.sig.connectors.bku.LocRefDetachedBKUConnector; import at.knowcenter.wag.egov.egiz.sig.connectors.moa.DetachedLocRefMOAConnector; import at.knowcenter.wag.egov.egiz.tools.CodingHelper; +import at.knowcenter.wag.egov.egiz.web.ExternAppInformation; import at.knowcenter.wag.egov.egiz.web.FormFields; import at.knowcenter.wag.egov.egiz.web.LocalRequest; import at.knowcenter.wag.egov.egiz.web.LocalRequestHelper; +import at.knowcenter.wag.egov.egiz.web.PDFContainer; import at.knowcenter.wag.egov.egiz.web.SessionAttributes; import at.knowcenter.wag.egov.egiz.web.SessionInformation; @@ -91,7 +97,7 @@ public class SignServlet extends HttpServlet * The log. */ private static Log log = LogFactory.getLog(SignServlet.class); - + protected void dispatch(HttpServletRequest request, HttpServletResponse response, String resource) throws ServletException, IOException { @@ -172,9 +178,72 @@ public class SignServlet extends HttpServlet */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - try - { - UploadedData ud = retrieveUploadedDataFromRequest(request); + UploadedData ud = null; + ExternAppInformation exappinf = null; + TablePos pos = null; + + // check if pdf-as has been called by external webapp + if(request.getParameter(FormFields.FIELD_PDF_URL) != null) { + + String preview = (String) request.getParameter(FormFields.FIELD_PREVIEW); + String sig_type = (String) request.getParameter(FormFields.FIELD_SIGNATURE_TYPE); + String sig_app = (String) request.getParameter(FormFields.FIELD_CONNECTOR); + String sig_mode = (String) request.getParameter(FormFields.FIELD_MODE); + String filename = (String) request.getParameter(FormFields.FIELD_FILENAME); + String pdf_url = (String) request.getParameter(FormFields.FIELD_PDF_URL); + String pdf_id = (String) request.getParameter(FormFields.FIELD_PDF_ID); + String pdf_length = (String) request.getParameter(FormFields.FIELD_FILE_LENGTH); + String invoke_url = (String) request.getParameter(FormFields.FIELD_INVOKE_APP_URL); + String session_id = (String) request.getParameter(FormFields.FIELD_SESSION_ID); + String sig_pos_y = (String) request.getParameter(FormFields.FIELD_SIGPOS_Y); + String sig_pos_p = (String) request.getParameter(FormFields.FIELD_SIGPOS_P); + + try { + pos = new TablePos("y:" + sig_pos_y +";p:" + sig_pos_p); + } catch (PDFDocumentException e) { + log.warn("Uanable to create signature position object: " + e.getMessage()); + } + + String query = pdf_url + "&" + FormFields.FIELD_PDF_ID + "=" + pdf_id; + + byte[] extern_pdf = new byte[Integer.parseInt(pdf_length)]; + URL source_url = new URL(query); + InputStream is = source_url.openStream(); + + extern_pdf = toByteArray(is); + + // set UploadedData object... + UploadedData ud_extern = new UploadedData(); + + ud_extern.file_name = filename; + ud_extern.pdf = extern_pdf; + ud_extern.preview = preview.equalsIgnoreCase("true") ? true : false; + ud_extern.sig_app = sig_app; + ud_extern.sig_mode = sig_mode; + ud_extern.sig_type = sig_type; + + ud = ud_extern; + + exappinf = new ExternAppInformation(invoke_url,pdf_id, session_id); + } + else { + + try + { + // tzefferer: modified + //UploadedData ud = retrieveUploadedDataFromRequest(request); + UploadedData ud_form = retrieveUploadedDataFromRequest(request); + ud = ud_form; + // end modify + + } catch(Exception e) { + // Error retrieving data + request.setAttribute("error", "Fehler beim Upload der Daten"); + request.setAttribute("cause", "Beim Upload der Daten ist ein Fehler aufgetreten."); + dispatch(request, response, "/jsp/error.jsp"); + } + } + try { PdfAS.applyStrictMode(ud.pdf); @@ -186,6 +255,11 @@ public class SignServlet extends HttpServlet si.type = ud.sig_type; si.filename = formatFileName(ud.file_name); si.download_inline = ud.download_inline; + + // added tzefferer: + si.exappinf = exappinf; + si.pos = pos; + // end add request.getSession().setAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION, si); @@ -213,12 +287,6 @@ public class SignServlet extends HttpServlet finishSign(si, request, response, getServletContext()); } - catch (FileUploadException e) - { - request.setAttribute("error", "Fehler beim Upload der Daten"); - request.setAttribute("cause", "Beim Upload der Daten ist ein Fehler aufgetreten."); - dispatch(request, response, "/jsp/error.jsp"); - } catch (PresentableException e) { e.printStackTrace(); @@ -227,6 +295,12 @@ public class SignServlet extends HttpServlet } } + // tzefferer:added + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request, response); + } + // end add + protected UploadedData retrieveUploadedDataFromRequest( HttpServletRequest request) throws ServletException, UnsupportedEncodingException, FileUploadException, PDFDocumentException { @@ -376,8 +450,11 @@ public class SignServlet extends HttpServlet PdfASID algorithm = FormFields.translateSignatureModeToPdfASID(si.mode); Signator signator = SignatorFactory.createSignator(algorithm); - si.iui = signator.prepareSign(si.pdf, si.type, null, ConnectorFactory.needsSIG_ID(si.connector)); - + + //tzefferer: modified + //si.iui = signator.prepareSign(si.pdf, si.type, null, ConnectorFactory.needsSIG_ID(si.connector)); + si.iui = signator.prepareSign(si.pdf, si.type, si.pos, ConnectorFactory.needsSIG_ID(si.connector)); + // end modify log.debug("prepareSign finished."); //$NON-NLS-1$ } @@ -541,22 +618,71 @@ public class SignServlet extends HttpServlet String file_name = formatFileNameForSignResult(si.filename, sign_result); - // The name parameter is actually deprecated in favour of - // Content-Disposition filename - // Unfortunately Acrobat reader does recognize neither of these parameters - // with its inline save-as. It always takes the page name. - response.setContentType(sign_result.getMimeType() + "; name=\"" + file_name + "\""); - if (si.download_inline) - { - response.addHeader("Content-Disposition", "inline; filename=\"" + file_name + "\""); - } - else - { - response.addHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\""); + // tzefferer: added condition + if (si.exappinf == null) { + + // The name parameter is actually deprecated in favour of + // Content-Disposition filename + // Unfortunately Acrobat reader does recognize neither of these parameters + // with its inline save-as. It always takes the page name. + response.setContentType(sign_result.getMimeType() + "; name=\"" + file_name + "\""); + if (si.download_inline) + { + response.addHeader("Content-Disposition", "inline; filename=\"" + file_name + "\""); + } + else + { + response.addHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\""); + } + response.getOutputStream().write(sign_result.getData()); + // tzefferer: added else-block + } else { + SignResult sr = si.sign_result; + byte[] signed_pdf = sr.getData(); + PDFContainer entry = new PDFContainer(signed_pdf, si.exappinf.pdf_id); + ProvidePDFServlet.signedDocuments.add(entry); + + // notify webapp... + String invoke_url = si.exappinf.invoke_url; + + String providePDFServlet = "ProvidePDF"; + String pdf_id = String.valueOf(si.exappinf.pdf_id); + String session_id = si.exappinf.session_id; + + // build URL + int ind = invoke_url.indexOf("?"); + String query = invoke_url.substring(0, ind) + ";jsessionid=" + session_id + invoke_url.substring(ind) + "&" + FormFields.FIELD_PDF_URL + "=" + providePDFServlet + "&" + FormFields.FIELD_PDF_ID + "=" + pdf_id + "&" + FormFields.FIELD_FILE_LENGTH + "=" + signed_pdf.length; + + response.sendRedirect(query); + } - response.getOutputStream().write(sign_result.getData()); } + + + // tzefferer: added + public static byte[] toByteArray(InputStream inputStream) throws IOException { + + if(inputStream == null) { + return null; + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(8192); + int n; + byte[] buffer = new byte[2048]; + BufferedInputStream bufIn = new BufferedInputStream(inputStream); + try { + while ((n = bufIn.read(buffer)) != -1) { + out.write(buffer, 0, n); + } + } finally { + if(bufIn != null) { + bufIn.close(); + } + } + return out.toByteArray(); + } + // end add protected static class UploadedData { -- cgit v1.2.3