aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-web
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-web')
-rw-r--r--pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/SignPreviewServlet.java512
1 files changed, 498 insertions, 14 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/SignPreviewServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/SignPreviewServlet.java
index 9430494..50e61f4 100644
--- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/SignPreviewServlet.java
+++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/SignPreviewServlet.java
@@ -25,30 +25,68 @@
*/
package at.gv.egiz.pdfas.web.servlets;
+import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
import javax.servlet.RequestDispatcher;
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.api.commons.Constants;
import at.gv.egiz.pdfas.api.exceptions.PdfAsException;
-import at.gv.egiz.pdfas.web.helper.SessionHelper;
+import at.gv.egiz.pdfas.api.io.DataSource;
+import at.gv.egiz.pdfas.api.io.TextBased;
+import at.gv.egiz.pdfas.api.sign.pos.SignaturePositioning;
+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.web.ExternAppInformation;
+import at.gv.egiz.pdfas.web.FormFields;
+import at.gv.egiz.pdfas.web.helper.ApiHelper;
import at.gv.egiz.pdfas.web.helper.SignServletHelper;
+import at.gv.egiz.pdfas.web.helper.WebSettingsReader;
+import at.gv.egiz.pdfas.web.helper.WebUtils;
+import at.gv.egiz.pdfas.web.servlets.SignServlet.UploadedData;
+import at.gv.egiz.pdfas.web.io.ByteArrayPdfDataSource;
+import at.gv.egiz.pdfas.web.itext.IText;
+import at.gv.egiz.pdfas.web.session.SessionAttributes;
import at.gv.egiz.pdfas.web.session.SignSessionInformation;
+import at.gv.egiz.pdfas.web.servlets.SignServlet;
+import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
+import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException;
+import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
+
/**
* @author wprinz
+ * changed by rpiazzi
*/
+
+
public class SignPreviewServlet extends HttpServlet
{
- protected static Log logger = LogFactory.getLog(SignPreviewServlet.class);
+ protected static Log log = LogFactory.getLog(SignPreviewServlet.class);
+
/**
* SVUID.
*/
@@ -66,19 +104,465 @@ public class SignPreviewServlet extends HttpServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
- try
- {
- Object sessionObject = SessionHelper.getSession(request);
+ HttpSession session = request.getSession();
+ }
+
+
+
+
+ protected UploadedData retrieveUploadedDataFromRequest(HttpServletRequest request) throws ServletException, UnsupportedEncodingException, FileUploadException, PDFDocumentException
+ {
+ DiskFileItemFactory fif = new DiskFileItemFactory();
+ fif.setRepository(WebSettingsReader.getTemporaryDirectory());
+ ServletFileUpload sfu = new ServletFileUpload(fif);
+
+ List items = sfu.parseRequest(request);
+
+ //FileItem preview_fi = null;
+ FileItem sig_type_fi = null;
+ FileItem mode_fi = null;
+ FileItem file_upload_fi = null;
+ //FileItem download_fi = null;
+ FileItem freeText = null;
+ FileItem pdfa = null;
+ FileItem note_fi = null;
+ FileItem source_fi = null;
+
+
+ boolean pdfaEnabled=false;
+ boolean noteEnabled=false;
+ String sig_type;
+ String sig_app;
+ String mode="";
+ String doc_file_name;
+ DataSource pdfDataSource;
+
+
+ Iterator it = items.iterator();
+ HttpSession session = request.getSession();
+
+ session.setAttribute(SignServlet.ERROR_WITHIN_IFRAME, "no");
+
+ while (it.hasNext())
+ {
+ FileItem item = (FileItem) it.next();
+ log.debug("item = " + item.getFieldName()); //$NON-NLS-1$
+
+ if (log.isDebugEnabled())
+ {
+ if (item.isFormField())
+ {
+ String item_string = item.getString("UTF-8"); //$NON-NLS-1$
+ log.debug(" form field string = " + item_string); //$NON-NLS-1$
+ }
+ else
+ {
+ log.debug(" filename = " + item.getName()); //$NON-NLS-1$
+ log.debug(" filesize = " + item.getSize()); //$NON-NLS-1$
+ }
+ }
+
+ if (item.getFieldName().equals(FormFields.FIELD_SIGNATURE_TYPE))
+ {
+ sig_type_fi = item;
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_TYPE_KEY, sig_type_fi.getString("UTF-8"));
+ continue;
+ }
+
+ if (item.getFieldName().equals(FormFields.FIELD_MODE))
+ {
+ mode_fi = item;
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_MODE_KEY, mode_fi.getString("UTF-8"));
+ continue;
+ }
+
+ if (item.getFieldName().equals(FormFields.FIELD_SOURCE_FILE))
+ {
+ //changed by rpiazzi
+ //Item always contains something as it is just hidden by javascript
+ // because of this just set the value if not empty
+ if (item!=null) {
+ file_upload_fi = item;
+ }
+ continue;
+
+ }
+
+ if (item.getFieldName().equals(FormFields.FIELD_PREVIEW))
+ {
+ continue;
+ }
+
+ if (FormFields.FIELD_PDFA_ENABLED.equals(item.getFieldName())) {
+ pdfa = item;
+ session.setAttribute(SignServlet.SUBMITFORM_PDFA_KEY, pdfa.getString("UTF-8"));
+ continue;
+ }
+
+ if (FormFields.FIELD_SOURCE_FREETEXT.equals(item.getFieldName())) {
+ freeText = item;
+ String value = freeText.getString("UTF-8");
+ if (value != null) {
+ session.setAttribute(SignServlet.SUBMITFORM_FREETEXT_KEY, value);
+ }
+ continue;
+ }
+
+ if (FormFields.FIELD_SOURCE.equals(item.getFieldName())) {
+ source_fi = item;
+ session.setAttribute(SignServlet.SUBMITFORM_SOURCE_KEY, source_fi.getString("UTF-8"));
+ continue;
+ }
+
+ //Added by rpiazzi
+ if (FormFields.FIELD_NOTE_ENABLED.equals(item.getFieldName())) {
+ note_fi = item;
+ session.setAttribute(SignServlet.SUBMITFORM_NOTE_KEY, note_fi.getString("UTF-8"));
+ continue;
+ }
+
+ //Added by rpiazzi
+ //parameter to know if app is called from within an iframe to set
+ //the null request mobile page redirecttarget to self
+ if (item.getFieldName().equals("extern")) {
+ session.setAttribute("extern", "yes");
+ continue;
+ }
+
+ throw new ServletException("Unrecognized POST data."); //$NON-NLS-1$
+
+ }
+
+ if (sig_type_fi == null || (file_upload_fi == null && freeText== null))
+ {
+ throw new ServletException("Insufficient data provided in request"); //$NON-NLS-1$
+ }
+
+ mode = mode_fi.getString("UTF-8"); //$NON-NLS-1$
+ if (!mode.equals(FormFields.VALUE_MODE_BINARY) && !mode.equals(FormFields.VALUE_MODE_TEXTUAL) && !mode.equals(FormFields.VALUE_MODE_DETACHED))
+ {
+ throw new ServletException("The mode '" + mode + "' is unrecognized."); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ //added by rpiazzi
+ if (pdfa!=null) {
+ if (pdfa.getString("UTF-8").equals("on")) {
+ pdfaEnabled = true;
+ }
+ }
+
+ if (note_fi!=null) {
+ if (note_fi.getString("UTF-8").equals("on")) {
+ noteEnabled = true;
+ }
+ }
+ //end added
+
+
+ /*boolean download_inline = true;
+ if (download_fi.getString("UTF-8").equals(FormFields.VALUE_DOWNLOAD_ATTACHMENT)) //$NON-NLS-1$
+ {
+ download_inline = false;
+ }*/
+
+ sig_type = sig_type_fi.getString("UTF-8"); //$NON-NLS-1$
+ sig_app = "";
+
+
+ // distinguish between file and freetext
+ if (source_fi.getString("UTF-8").equals(FormFields.VALUE_SOURCE_FILE)) {
+ log.debug("Processing file.");
+ File f = new File(file_upload_fi.getName());
+ doc_file_name = f.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 = new ByteArrayPdfDataSource(IOUtils.toByteArray(file_upload_fi.getInputStream()));
+ session.setAttribute(SignServlet.SUBMITFORM_FILE_KEY, pdfDataSource);
+ session.setAttribute(SignServlet.SUBMITFORM_FILENAME_KEY, 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 {
+ byte[] freeTextPDF = IText.createPDF(freeText.getString("UTF-8"), pdfaEnabled);
+
+ pdfDataSource = new ByteArrayPdfDataSource(freeTextPDF);
+ doc_file_name = IText.DEFAULT_FILENAME;
+ session.setAttribute(SignServlet.SUBMITFORM_FILE_KEY, pdfDataSource);
+ session.setAttribute(SignServlet.SUBMITFORM_FILENAME_KEY, doc_file_name);
+ } 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);
+ }
+
+ }
+
+
+
+
+
+ UploadedData ud = new UploadedData();
+
+ ud.preview = true;
+ ud.pdfa = pdfaEnabled;
+ ud.download_inline = false;
+ ud.sig_type = sig_type;
+ ud.sig_app = sig_app;
+ ud.sig_mode = mode;
+ ud.file_name = doc_file_name;
+ ud.pdfDataSource = pdfDataSource;
+ ud.note = noteEnabled;
+
+ return ud;
+ }
+
+
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+
+ UploadedData ud = null;
+ ExternAppInformation exappinf = null;
+ SignaturePositioning sigpos = null;
+
+
+ if (request.getParameter(FormFields.FIELD_PDF_URL) != null)
+ {
+
+ 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 invoke_error_url = (String) request.getParameter(FormFields.FIELD_INVOKE_APP_ERROR_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);
+ String preview = (String) request.getParameter(FormFields.FIELD_PREVIEW);
+
+ // added by tknall
+ if (sig_pos_y != null && sig_pos_p != null) {
+ try
+ {
+ sigpos = new SignaturePositioning("y:" + sig_pos_y + ";p:" + sig_pos_p);
+ }
+ catch (PDFDocumentException e)
+ {
+ log.warn("Unable to create signature position object: " + e.getMessage(), e);
+ }
+ } else {
+ log.debug("No signature position provided.");
+ }
+
+ // fixed by tknall: if we already have parameters "&" must be used instead of "?"
+ String paramSeparator = (pdf_url.indexOf("?") != -1) ? "&" : "?";
+ String query = pdf_url + paramSeparator + FormFields.FIELD_PDF_ID + "=" + pdf_id;
+
+ // wprinz: rem: this allocation is useless
+ // 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.pdfDataSource = new ByteArrayPdfDataSource(IOUtils.toByteArray(is));
+
+ ud_extern.preview = "true".equalsIgnoreCase(preview);
+ 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, invoke_error_url);
+ }
+ else
+ {
+ try
+ {
+ // tzefferer: modified
+ // UploadedData ud = retrieveUploadedDataFromRequest(request);
+ ud = retrieveUploadedDataFromRequest(request);
+ // end modify
+
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ request.setAttribute("error", "signservlet.error");
+ request.setAttribute("cause", "signservlet.cause");
+ request.setAttribute("resourcebundle", Boolean.TRUE);
+ dispatch(request, response, "/jsp/error.jsp");
+ return;
+ }
+ }
+
+ try {
+
+
+ SignSessionInformation si = new SignSessionInformation(); // SessionTable.generateSessionInformationObject();
+ si.connector = Constants.SIGNATURE_DEVICE_MOC; //in the mean time set standard signature device...
+ si.application = "sign";
+ si.mode = ud.sig_mode;
+ si.pdfDataSource = ud.pdfDataSource;
+ si.type = ud.sig_type;
+
+ si.filename = formatFileName(ud.file_name);
+
+ si.download_inline = ud.download_inline;
+ si.pdfa = ud.pdfa;
+ si.note = ud.note;
+
+ // added tzefferer:
+ si.exappinf = exappinf;
+ si.pos = sigpos;
+ // end add
+
+ HttpSession session = request.getSession();
+
+ //Added by rpiazzi to set right signature parameters
+ if (FormFields.FIELD_MODE_BINARY.contains(si.mode)) {
+ si.mode = Constants.SIGNATURE_TYPE_BINARY;
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_MODE_KEY, si.mode);
+ }
+ else {
+ si.mode = Constants.SIGNATURE_TYPE_TEXTUAL;
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_MODE_KEY, si.mode);
+ }
+ si.mode = Constants.SIGNATURE_TYPE_BINARY;
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_MODE_KEY, si.mode);
+
+ if (!si.note) {
+ session.setAttribute(SignServlet.SUBMITFORM_NOTE_KEY, "off");
+ }
+ if (!si.pdfa) {
+ session.setAttribute(SignServlet.SUBMITFORM_PDFA_KEY, "off");
+ }
+
+ if (si.note) {
+ if (si.type.contains("DE")) {
+ si.type = "SIGNATURBLOCK_DE_NOTE";
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_TYPE_KEY, si.type);
+ }
+ else {
+ si.type = "SIGNATURBLOCK_EN_NOTE";
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_TYPE_KEY, si.type);
+ }
+ }
+ session.setAttribute(SignServlet.SUBMITFORM_SIGNATURE_DEVICE_KEY, si.connector);
+ //end added
+
+ log.info("Putting signature data into session " + session.getId());
+ session.setAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION, si);
+
+ SignServletHelper.prepareSign(ApiHelper.getPdfAsFromContext(getServletContext()), si);
+
+ String submit_url = response.encodeURL(request.getContextPath() + "/SignPreview");
+ String signature_data_url = response.encodeURL(WebUtils.buildRetrieveSignatureDataURL(request, response));
+
+ request.setAttribute("submit_url", submit_url);
+ request.setAttribute("signature_data_url", signature_data_url);
+
+ if (si.mode.equals(Constants.SIGNATURE_TYPE_TEXTUAL)){
+ request.setAttribute("document_text", ((TextBased)si.sdi.getSignatureData()).getText());
+ }
+ dispatch(request, response, "/jsp/signpreview.jsp");
+
+ }catch (PresentableException e)
+ {
+ log.error(e.getMessage(), e);
+ prepareDispatchToErrorPage(e, request);
+ dispatch(request, response, "/jsp/error.jsp");
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ PresentableException pe = new PresentableException(ErrorCode.UNKNOWN_ERROR, e);
+ prepareDispatchToErrorPage(pe, request);
+ dispatch(request, response, "/jsp/error.jsp");
+ }
+
+
+
+ }
+
+
+ /**
+ * Formats the file name so that it is suitable for content disposition.
+ *
+ * @param file_name
+ * The file name.
+ * @return Returns the formatted file name.
+ */
+ public static String formatFileName(String file_name)
+ {
+ File file = new File(file_name);
+ String file_name_only = file.getName();
+ // the file_name contains \\ ==> remove them so Internet Explorer works
+ // correctly.
+ return file_name_only;
+ }
+
+ public static void prepareDispatchToErrorPage(PdfAsException pe, HttpServletRequest request)
+ {
+ request.setAttribute("PresentableException", pe);
+// if (pe instanceof ErrorCodeException)
+// {
+ request.setAttribute("error", "Fehler " + pe.getErrorCode());
+
+ String cause = ErrorCodeHelper.getMessageForErrorCode(pe.getErrorCode());
+
+ if (pe instanceof ExternalErrorException)
+ {
+ ExternalErrorException eee = (ExternalErrorException) pe;
+ cause = eee.getExternalErrorCode() + ": " + eee.getExternalErrorMessage();
+ }
+ request.setAttribute("cause", cause);
+ if (pe.getErrorCode() == ErrorCode.PLACEHOLDER_EXCEPTION)
+ {
+ PlaceholderException phe = null;
+ if (pe instanceof PlaceholderException)
+ {
+ phe = (PlaceholderException) pe;
+ }
+ else
+ {
+ phe = (PlaceholderException) pe.getCause();
+ }
+
+ request.setAttribute("cause", "Der Platzhalter des Feldes " + phe.getField() + " ist um " + phe.getMissing() + " Bytes zu kurz. " + cause);
+ }
- SignSessionInformation si = (SignSessionInformation) sessionObject;
- SignServletHelper.finishSign(si, request, response, getServletContext());
- }
- catch (PdfAsException e) {
- logger.error(e.getMessage(), e);
-
- SignServlet.prepareDispatchToErrorPage(e, request);
- dispatch(request, response, "/jsp/error.jsp");
- }
+
+// }
+// else
+// {
+// request.setAttribute("error", "PresentableException");
+// request.setAttribute("cause", pe.toString());
+// }
}
+
}