From 527484bcc0a65c61d50209849f7b3db34f0128f7 Mon Sep 17 00:00:00 2001 From: knowcenter Date: Thu, 17 May 2007 15:28:32 +0000 Subject: web git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@87 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/web/servlets/VerifyServlet.java | 334 +++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/VerifyServlet.java (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/VerifyServlet.java') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/VerifyServlet.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/VerifyServlet.java new file mode 100644 index 0000000..ee20498 --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/VerifyServlet.java @@ -0,0 +1,334 @@ +/* + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR + * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY + * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. + * + * $Id: Verify.java,v 1.7 2006/10/11 07:39:13 wprinz Exp $ + */ +package at.knowcenter.wag.egov.egiz.web.servlets; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +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.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Logger; + +import at.knowcenter.wag.egov.egiz.PdfAS; +import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; +import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; +import at.knowcenter.wag.egov.egiz.exceptions.ConnectorFactoryException; +import at.knowcenter.wag.egov.egiz.exceptions.NormalizeException; +import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; +import at.knowcenter.wag.egov.egiz.exceptions.PresentableException; +import at.knowcenter.wag.egov.egiz.exceptions.SignatureException; +import at.knowcenter.wag.egov.egiz.exceptions.SignatureTypesException; +import at.knowcenter.wag.egov.egiz.framework.VerificationFilter; +import at.knowcenter.wag.egov.egiz.pdf.SignatureHolder; +import at.knowcenter.wag.egov.egiz.sig.ConnectorFactory; +import at.knowcenter.wag.egov.egiz.sig.SignatureData; +import at.knowcenter.wag.egov.egiz.sig.SignatureDataImpl; +import at.knowcenter.wag.egov.egiz.sig.SignatureObject; +import at.knowcenter.wag.egov.egiz.sig.connectors.LocalConnector; +import at.knowcenter.wag.egov.egiz.sig.connectors.bku.LocRefDetachedBKUConnector; +import at.knowcenter.wag.egov.egiz.sig.connectors.bku.SignSignatureObject; +import at.knowcenter.wag.egov.egiz.tools.CodingHelper; +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.SessionAttributes; +import at.knowcenter.wag.egov.egiz.web.SessionInformation; +import at.knowcenter.wag.egov.egiz.web.servlets.SignServlet.UploadedData; + +/** + * This method is the verify servlet for the pdf-as web application. It takes + * get and post requests fill out jsp templates and give the user feedback about + * the results of the verify process. + * + * @author wlackner + * @author wprinz + */ +public class VerifyServlet extends HttpServlet +{ + + /** + * SVUID. + */ + private static final long serialVersionUID = 309198792358636766L; + + /** + * The log. + */ + private static Log log = LogFactory.getLog(SignServlet.class); + + protected void dispatch(HttpServletRequest request, + HttpServletResponse response, String resource) throws ServletException, IOException + { + response.setContentType("text/html"); //$NON-NLS-1$ + response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$ + + RequestDispatcher disp = getServletContext().getRequestDispatcher(resource); + disp.forward(request, response); + } + + protected void dispatchToResults(List results, HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException + { + request.setAttribute("results", results); //$NON-NLS-1$ + dispatch(request, response, "/jsp/results.jsp"); //$NON-NLS-1$ + } + + /** + * Processes the verify upload. + * + * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, + * javax.servlet.http.HttpServletResponse) + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + try + { + UploadedData ud = retrieveUploadedDataFromRequest(request); + + List signature_holders = extractSignatureHoldersFromFile(ud.file_name, ud.file_data); + + SessionInformation si = new SessionInformation(); // SessionTable.generateSessionInformationObject(); + si.connector = ud.sig_app; + si.application = "verify"; + si.mode = null; + si.pdf = null; + si.type = null; + si.user_name = null; + si.user_password = null; + + si.signature_holders = signature_holders; + + request.getSession().setAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION, si); + + if (ud.preview) + { + dispatch(request, response, "/jsp/verifylist.jsp"); + // VerifyPreview.formatPreview(signature_holders, connector, request, + // response); + } + else + { + if (ConnectorFactory.isConnectorLocal(si.connector)) + { + String dispatch_to = LocalRequestHelper.processLocalVerify(si, si.signature_holders, request, response); + dispatch(request, response, dispatch_to); + return; + } + + List results = PdfAS.verifySignatureHolders(signature_holders, si.connector); + dispatchToResults(results, request, response); + } + + } + 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(); + SignServlet.prepareDispatchToErrorPage(e, request); + dispatch(request, response, "/jsp/error.jsp"); + } + + } + + protected UploadedData retrieveUploadedDataFromRequest( + HttpServletRequest request) throws ServletException, UnsupportedEncodingException, FileUploadException, PDFDocumentException + { + DiskFileItemFactory fif = new DiskFileItemFactory(); + fif.setRepository(SettingsReader.getTemporaryDirectory()); + ServletFileUpload sfu = new ServletFileUpload(fif); + + List items = sfu.parseRequest(request); + + FileItem upload_fi = null; + FileItem connector_fi = null; + // FileItem mode_fi = null; + FileItem preview_fi = null; + + { + Iterator it = items.iterator(); + while (it.hasNext()) + { + FileItem item = (FileItem) it.next(); + log.debug("item = " + item.getFieldName()); //$NON-NLS-1$ + + 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_UPLOAD)) + { + upload_fi = item; + continue; + } + + if (item.getFieldName().equals(FormFields.FIELD_CONNECTOR)) + { + connector_fi = item; + continue; + } + + // if (item.getFieldName().equals(FormFields.FIELD_MODE)) + // { + // mode_fi = item; + // continue; + // } + + if (item.getFieldName().equals(FormFields.FIELD_PREVIEW)) + { + preview_fi = item; + continue; + } + + throw new ServletException("unrecognized POST data."); //$NON-NLS-1$ + } + } + + if (upload_fi == null || connector_fi == null || /* mode_fi == null || */preview_fi == null) + { + throw new ServletException("Unsufficient data provided in request."); //$NON-NLS-1$ + } + + String connector = connector_fi.getString("UTF-8"); //$NON-NLS-1$ + + // String mode = mode_fi.getString("UTF-8"); + // if (!mode.equals(FormFields.VALUE_MODE_BINARY) && + // !mode.equals(FormFields.VALUE_MODE_TEXTUAL)) + // { + // throw new ServletException("The mode '" + mode + "' is unrecognized."); + // } + + String preview_str = preview_fi.getString("UTF-8"); //$NON-NLS-1$ + if (!preview_str.equals(FormFields.VALUE_TRUE) && !preview_str.equals(FormFields.VALUE_FALSE)) + { + throw new ServletException("The preview '" + preview_str + "' is unrecognized."); //$NON-NLS-1$//$NON-NLS-2$ + } + boolean preview = false; + if (preview_str.equals(FormFields.VALUE_TRUE)) + { + preview = true; + } + + // process the request + log.debug("file content type =" + upload_fi.getContentType()); //$NON-NLS-1$ + log.debug("file size = " + upload_fi.getSize()); //$NON-NLS-1$ + if (upload_fi.getSize() <= 0) + { + throw new PDFDocumentException(250, "The document is empty."); //$NON-NLS-1$ + } + byte[] document_bytes = upload_fi.get(); + + UploadedData ud = new UploadedData(); + ud.preview = preview; + ud.sig_app = connector; + ud.file_name = upload_fi.getName(); + ud.file_data = document_bytes; + + return ud; + } + + protected List extractSignatureHoldersFromFile (String file_name, byte [] data) throws UnsupportedEncodingException, PresentableException + { + VerificationFilter vf = new VerificationFilter(); + List signature_holders = null; + + String extension = extractExtension(file_name); + + String raw_text = null; + if (file_name == null || (extension != null && extension.equals("txt"))) //$NON-NLS-1$ + { + raw_text = new String(data, "UTF-8"); //$NON-NLS-1$ + signature_holders = vf.extractSignaturesFromPlainText(raw_text); + } + else + { + signature_holders = vf.extractSignaturesFromPdf(data); + } + + if (signature_holders.size() == 0) + { + throw new PDFDocumentException(206, "PDF document not signed."); //$NON-NLS-1$ + } + + return signature_holders; + } + + /** + * Extracts the extension from a file name string. + * + *

+ * The extension of a file name is whatever text follows the last '.'. + *

+ * + * @param file_name + * The file name. + * @return Returns the extension. If the file name ends with the '.', then an + * empty string is returned. If the file name doesn't contain any '.' + * or file_name is null, null is returned. + */ + public static String extractExtension(String file_name) + { + if (file_name == null) + { + return null; + } + + int dot_index = file_name.lastIndexOf('.'); + if (dot_index < 0) + { + return null; + } + return file_name.substring(dot_index + 1); + } + + protected static class UploadedData + { + protected boolean preview = false; + + protected String sig_app = null; + + protected String file_name = null; + + protected byte[] file_data = null; + } +} \ No newline at end of file -- cgit v1.2.3