/* * 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; import java.io.IOException; 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 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.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.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.PresentableException; import at.knowcenter.wag.egov.egiz.framework.VerificationFilter; import at.knowcenter.wag.egov.egiz.sig.ConnectorFactory; /** * 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 Verify extends HttpServlet { /** * SVUID. */ private static final long serialVersionUID = 309198792358636766L; /** * The logger. */ private static final Logger logger_ = ConfigLogger.getLogger(Verify.class); protected void dispatch(HttpServletRequest request, HttpServletResponse response, String resource) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); 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); dispatch(request, response, "/jsp/results.jsp"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { dispatch(request, response, "/jsp/verifyupload.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory fif = new DiskFileItemFactory(); fif.setRepository(SettingsReader.getTemporaryDirectory()); ServletFileUpload sfu = new ServletFileUpload(fif); try { 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(); logger_.debug("item = " + item.getFieldName()); if (item.isFormField()) { byte[] item_data = item.get(); String item_string = new String(item_data, "UTF-8"); logger_.debug(" form field string = " + item_string); } else { logger_.debug(" filename = " + item.getName()); logger_.debug(" filesize = " + item.getSize()); } 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."); } } if (upload_fi == null || connector_fi == null || /*mode_fi == null ||*/ preview_fi == null) { throw new ServletException("Unsufficient data provided in request."); } String connector = connector_fi.getString("UTF-8"); // 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"); if (!preview_str.equals(FormFields.VALUE_TRUE) && !preview_str.equals(FormFields.VALUE_FALSE)) { throw new ServletException("The preview '" + preview_str + "' is unrecognized."); } boolean preview = false; if (preview_str.equals(FormFields.VALUE_TRUE)) { preview = true; } // process the request logger_.debug("file content type =" + upload_fi.getContentType()); logger_.debug("file size = " + upload_fi.getSize()); if (upload_fi.getSize() <= 0) { throw new PDFDocumentException(250, "The document is empty."); } byte[] document_bytes = upload_fi.get(); VerificationFilter vf = new VerificationFilter(); List signature_holders = null; String doc_file_name = upload_fi.getName(); String extension = extractExtension(doc_file_name); String raw_text = null; if (doc_file_name == null || (extension != null && extension.equals("txt"))) { raw_text = new String(document_bytes, "UTF-8"); signature_holders = vf.extractSignaturesFromPlainText(raw_text); } else { signature_holders = vf.extractSignaturesFromPdf(document_bytes); } if (signature_holders.size() == 0) { throw new PDFDocumentException(206, "PDF document not signed."); } if (preview) { SessionInformation si = new SessionInformation(); si.application = "verify"; si.connector = connector; si.signature_holders = signature_holders; request.getSession().setAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION, si); dispatch(request, response, "/jsp/verifylist.jsp"); //VerifyPreview.formatPreview(signature_holders, connector, request, response); } else { if (ConnectorFactory.isConnectorLocal(connector)) { SessionInformation si = new SessionInformation(); //SessionTable.generateSessionInformationObject(); si.connector = connector; 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; LocalRequestHelper.processLocalVerify(si, si.signature_holders, request, response); dispatch(request, response, LocalRequestHelper.LOCAL_CONNECTION_PAGE_JSP); return; } List results = PdfAS.verifySignatureHolders(signature_holders, 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(); Sign.prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); } } /** * 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); } }