From 11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9 Mon Sep 17 00:00:00 2001 From: tknall Date: Tue, 16 Mar 2010 12:07:29 +0000 Subject: Catching OutOfMemory exceptions, returning appropriate error message/code Binary signature: bug concerning indirect pdf objects fixed SignaturePositioning improved (Signature position can be declared by String which is parsed) Some more error codes (Out of memory, Invalid signature position) iText utility for creation of pdf files added ConfigUtils updated (destination of configuration to be extracted can now be chosen) PDFASUtils updated (more tools) WebApplication: Freetext pdf creation implemented WebApplication: XSS security updates git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@580 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../knowcenter/wag/egov/egiz/web/FormFields.java | 10 +- .../egov/egiz/web/servlets/ProvidePDFServlet.java | 2 + .../wag/egov/egiz/web/servlets/SignServlet.java | 102 +++++++++++++++------ .../egov/egiz/web/servlets/UpdateFormServlet.java | 24 +++-- 4 files changed, 103 insertions(+), 35 deletions(-) (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/web') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/FormFields.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/FormFields.java index d455179..876413a 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/web/FormFields.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/FormFields.java @@ -41,8 +41,6 @@ import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; */ public abstract class FormFields { - public static final String PARAMETER_FREE_TEXT_ENTRY = "freetext"; - public static final String FIELD_UPLOAD = "upload"; public static final String FIELD_CONNECTOR = "connector"; @@ -101,6 +99,14 @@ public abstract class FormFields public static final String FIELD_SIGPOS_Y = "sig-pos-y"; public static final String FIELD_SIGPOS_P = "sig-pos-p"; + + public static final String FIELD_SOURCE = "source_filefreetext"; + + public static final String VALUE_SOURCE_FILE = "source-is-file"; + + public static final String VALUE_SOURCE_FREETEXT = "source-is-freetext"; + + public static final String FIELD_FREETEXT = "freetext"; // end add diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/ProvidePDFServlet.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/ProvidePDFServlet.java index 803dc59..0efed03 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/ProvidePDFServlet.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/ProvidePDFServlet.java @@ -60,6 +60,8 @@ public class ProvidePDFServlet extends HttpServlet { session.removeAttribute(SessionAttributes.SIGNED_PDF_DOCUMENT); log.debug("Returning signed pdf to browser."); SignServletHelper.returnSignResponse(si, request, response); + log.debug("Removing free text (if any) from session."); + session.removeAttribute(UpdateFormServlet.UPLOADFORM_FREETEXT_KEY); return; } } else { 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 c946225..69b237f 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 @@ -33,18 +33,26 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.lowagie.text.DocumentException; + import at.gv.egiz.pdfas.exceptions.ErrorCode; import at.gv.egiz.pdfas.exceptions.ErrorCodeHelper; import at.gv.egiz.pdfas.exceptions.external.ExternalErrorException; import at.gv.egiz.pdfas.framework.input.PdfDataSource; +import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; +import at.gv.egiz.pdfas.itext.IText; +import at.gv.egiz.pdfas.utils.ConfigUtils; +import at.gv.egiz.pdfas.utils.PDFASUtils; import at.gv.egiz.pdfas.utils.WebUtils; import at.gv.egiz.pdfas.web.SignSessionInformation; import at.gv.egiz.pdfas.web.helper.SignServletHelper; @@ -235,6 +243,7 @@ public class SignServlet extends HttpServlet try { + // tzefferer: modified // UploadedData ud = retrieveUploadedDataFromRequest(request); UploadedData ud_form = retrieveUploadedDataFromRequest(request); @@ -244,9 +253,7 @@ public class SignServlet extends HttpServlet } 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."); + log.error(e); request.setAttribute("error", "signservlet.error"); request.setAttribute("cause", "signservlet.cause"); request.setAttribute("resourcebundle", Boolean.TRUE); @@ -344,8 +351,11 @@ public class SignServlet extends HttpServlet FileItem mode_fi = null; FileItem file_upload_fi = null; FileItem download_fi = null; + FileItem freeText = null; + FileItem pdfa = null; Iterator it = items.iterator(); + HttpSession session = request.getSession(); while (it.hasNext()) { FileItem item = (FileItem) it.next(); @@ -374,18 +384,21 @@ public class SignServlet extends HttpServlet if (item.getFieldName().equals(FormFields.FIELD_SIGNATURE_TYPE)) { sig_type_fi = item; + session.setAttribute(UpdateFormServlet.UPLOADFORM_SIGNATURE_TYPE_KEY, sig_type_fi.getString("UTF-8")); continue; } if (item.getFieldName().equals(FormFields.FIELD_CONNECTOR)) { sig_app_fi = item; + session.setAttribute(UpdateFormServlet.UPLOADFORM_SIGNATURE_DEVICE_KEY, sig_app_fi.getString("UTF-8")); continue; } if (item.getFieldName().equals(FormFields.FIELD_MODE)) { mode_fi = item; + session.setAttribute(UpdateFormServlet.UPLOADFORM_SIGNATURE_MODE_KEY, mode_fi.getString("UTF-8")); continue; } @@ -402,14 +415,30 @@ public class SignServlet extends HttpServlet } if (FormFields.FIELD_PDFA_ENABLED.equals(item.getFieldName())) { + pdfa = item; + session.setAttribute(UpdateFormServlet.UPLOADFORM_PDFA_KEY, pdfa.getString("UTF-8")); + continue; + } + + if (FormFields.FIELD_FREETEXT.equals(item.getFieldName())) { + freeText = item; + String value = freeText.getString("UTF-8"); + if (value != null) { + session.setAttribute(UpdateFormServlet.UPLOADFORM_FREETEXT_KEY, value); + } + continue; + } + + if (FormFields.FIELD_SOURCE.equals(item.getFieldName())) { + session.setAttribute(UpdateFormServlet.UPLOADFORM_SOURCE_KEY, item.getString("UTF-8")); continue; } - throw new ServletException("unrecognized POST data."); //$NON-NLS-1$ + throw new ServletException("Unrecognized POST data."); //$NON-NLS-1$ } - if (preview_fi == null || sig_type_fi == null || sig_app_fi == null || file_upload_fi == null || download_fi == null) + if (preview_fi == null || sig_type_fi == null || sig_app_fi == null || (file_upload_fi == null && freeText== null) || download_fi == null) { throw new ServletException("Insufficient data provided in request"); //$NON-NLS-1$ } @@ -436,28 +465,47 @@ public class SignServlet extends HttpServlet String sig_type = sig_type_fi.getString("UTF-8"); //$NON-NLS-1$ String sig_app = sig_app_fi.getString("UTF-8"); //$NON-NLS-1$ - String doc_file_name = TempDirHelper.extractFileNameSuffix(file_upload_fi.getName()); - log.debug("file content type =" + file_upload_fi.getContentType()); //$NON-NLS-1$ - - String extension = VerifyServlet.extractExtension(doc_file_name); - if (extension != null && !extension.equals("pdf")) //$NON-NLS-1$ - { - throw new PDFDocumentException(201, "The provided file '" + doc_file_name + "' doesn't have the PDF extension (.pdf)."); //$NON-NLS-1$//$NON-NLS-2$ - } - - if (file_upload_fi.getSize() <= 0) - { - throw new PDFDocumentException(250, "The document is empty."); //$NON-NLS-1$ - } - - PdfDataSource pdfDataSource = null; - try - { - pdfDataSource = TempDirHelper.placePdfIntoTempDir(file_upload_fi.getInputStream(), doc_file_name); - } - catch (IOException e) - { - throw new PDFDocumentException(201, "Couldn't store the file in the temp dir.", e); + PdfDataSource pdfDataSource; + String doc_file_name; + // distinguish between file and freetext + if (file_upload_fi != null) { + log.debug("Processing file."); + doc_file_name = TempDirHelper.extractFileNameSuffix(file_upload_fi.getName()); + log.debug("file content type =" + file_upload_fi.getContentType()); //$NON-NLS-1$ + + String extension = VerifyServlet.extractExtension(doc_file_name); + if (extension != null && !extension.equals("pdf")) //$NON-NLS-1$ + { + throw new PDFDocumentException(201, "The provided file '" + doc_file_name + "' doesn't have the PDF extension (.pdf)."); //$NON-NLS-1$//$NON-NLS-2$ + } + + if (file_upload_fi.getSize() <= 0) + { + throw new PDFDocumentException(250, "The document is empty."); //$NON-NLS-1$ + } + + try + { + pdfDataSource = TempDirHelper.placePdfIntoTempDir(file_upload_fi.getInputStream(), doc_file_name); + } + catch (IOException e) + { + throw new PDFDocumentException(201, "Couldn't store the file in the temp dir.", e); + } + } else { + log.debug("Processing free text."); + try { + boolean pdfaEnabled = pdfa != null && "true".equalsIgnoreCase(pdfa.getString()); + byte[] freeTextPDF = IText.createPDF(freeText.getString("UTF-8"), pdfaEnabled); + + pdfDataSource = new ByteArrayPdfDataSourceImpl(freeTextPDF); + doc_file_name = IText.DEFAULT_FILENAME; + } catch (DocumentException e) { + throw new PDFDocumentException(201, "Unable to create PDF document.", e); + } catch (IOException e) { + throw new PDFDocumentException(201, "Unable to create PDF document.", e); + } + } // byte[] pdf = file_upload_fi.get(); diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/UpdateFormServlet.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/UpdateFormServlet.java index 2d21545..f0d3d3d 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/UpdateFormServlet.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/UpdateFormServlet.java @@ -19,22 +19,34 @@ public class UpdateFormServlet extends HttpServlet { public static final String UPLOADFORM_SIGNATURE_MODE_KEY = "signupload.jsp:signatureMode"; public static final String UPLOADFORM_SIGNATURE_DEVICE_KEY = "signupload.jsp:signatureKey"; public static final String UPLOADFORM_PDFA_KEY = "signupload.jsp:pdfaKey"; + public static final String UPLOADFORM_SOURCE_KEY = "signupload.jsp:sourceKey"; + public static final String UPLOADFORM_FREETEXT_KEY = "signupload.jsp:freeTextKey"; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } - - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String mode = request.getParameter(FormFields.FIELD_MODE); - String device = request.getParameter(FormFields.FIELD_CONNECTOR); - String type = request.getParameter(FormFields.FIELD_SIGNATURE_TYPE); - String pdfa = request.getParameter(FormFields.FIELD_PDFA_ENABLED); + + public static void updateSession(HttpServletRequest request) { + String mode = request.getParameter(FormFields.FIELD_MODE); + String device = request.getParameter(FormFields.FIELD_CONNECTOR); + String type = request.getParameter(FormFields.FIELD_SIGNATURE_TYPE); + String pdfa = request.getParameter(FormFields.FIELD_PDFA_ENABLED); + String source = request.getParameter(FormFields.FIELD_SOURCE); + String freeText = request.getParameter(FormFields.FIELD_FREETEXT); HttpSession session = request.getSession(); session.setAttribute(UPLOADFORM_SIGNATURE_DEVICE_KEY, device); session.setAttribute(UPLOADFORM_SIGNATURE_MODE_KEY, mode); session.setAttribute(UPLOADFORM_SIGNATURE_TYPE_KEY, type); session.setAttribute(UPLOADFORM_PDFA_KEY, pdfa); + session.setAttribute(UPLOADFORM_SOURCE_KEY, source); + if (freeText != null) { + session.setAttribute(UPLOADFORM_FREETEXT_KEY, freeText); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + updateSession(request); RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/signupload.jsp"); dispatcher.forward(request, response); -- cgit v1.2.3