/** * 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: VerifyPreview.java,v 1.4 2006/10/11 07:39:13 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.web; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; 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.log4j.Logger; import at.knowcenter.wag.egov.egiz.PdfAS; import at.knowcenter.wag.egov.egiz.PdfASID; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.ErrorCodeException; import at.knowcenter.wag.egov.egiz.exceptions.InvalidIDException; import at.knowcenter.wag.egov.egiz.exceptions.PresentableException; import at.knowcenter.wag.egov.egiz.exceptions.SignatureException; import at.knowcenter.wag.egov.egiz.pdf.BinarySignatureHolder; import at.knowcenter.wag.egov.egiz.pdf.SignatureHolder; import at.knowcenter.wag.egov.egiz.pdf.TextualSignatureHolder; import at.knowcenter.wag.egov.egiz.sig.ConnectorFactory; import at.knowcenter.wag.egov.egiz.sig.SignatureEntry; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; /** * @author wprinz */ public class VerifyPreview extends HttpServlet { /** * SVUID. */ private static final long serialVersionUID = 6954343542890239109L; /** * The logger. */ private static final Logger logger_ = ConfigLogger.getLogger(VerifyPreview.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 static String generateNamePrefix(int num) { return SIG_INPUT_PREFIX + num + "_"; } // public static void formatPreview(List signature_holders, String connector, // HttpServletRequest request, HttpServletResponse response) throws // ServletException, IOException // { // response.setContentType("text/html"); // response.setCharacterEncoding("UTF-8"); // // PrintWriter writer = response.getWriter(); // // writer.println(""); // writer.println(""); // writer.println(""); // writer.println(""); // writer.println("PDF-AS Amtssignaturen Vorschau"); // writer.println(""); // // writer.println(""); // // writer.println(""); // // writer.println(""); // writer.println("
"); // // writer.println("

PDF-AS Amtssignaturen

"); // // writer.println("
"); // writer.println("
Vorschau: Dokument Prüfen
"); // writer.println("
"); // writer.println("
"); // // // writer.println(""); // writer.println(""); // // // if (mode.equals(FormFields.VALUE_MODE_TEXTUAL)) // // { // // String raw_document_text = ((SignatureHolder) // // signature_holders.get(0)).signed_text; // // writer.println(""); // // writer.println("
"); // // } // // if (signature_holders.size() == 1) // { // writer.println("
Rekonstruktion der Signaturmarke:
"); // } // else // { // writer.println("
Rekonstruktion der Signaturmarken:
"); // } // // // this special
is only needed because internet explorer doesn't // // format the width of the // // tables right when the div is not given. // // probably this is because the table then tries to calculate the relative // // width from the surrounding form object. // writer.println("
"); // int num = 0; // Iterator it = signature_holders.iterator(); // while (it.hasNext()) // { // SignatureHolder signature_holder = (SignatureHolder) it.next(); // // String name_prefix = generateNamePrefix(num); // // String input_key = name_prefix + FormFields.FIELD_RAW_DOCUMENT_TEXT; // if (signature_holder.signature_object.isBinary()) // { // writer.println(""); // } // else // { // writer.println("
"); // writer.println(""); // writer.println("
"); // } // // String html = renderRequiredKeys(signature_holder.signature_object, // name_prefix, false); // writer.println(html); // // writer.println(""); // // num++; // } // writer.println("
"); // // writer.println("");// + // // FormFields.VALUE_VERIFY_WHICH_ALL // // + // // "\" // // />"); // writer.println(""); // // writer.println("
"); // writer.println(""); // writer.println("
"); // // writer.println("zurück"); // // writer.println("
"); // writer.println(""); // // writer.println(""); // // } public static String renderRequiredKeysJavaScript( SignatureObject signature_object, String name_prefix) { StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); String[] rkeys = SignatureTypes.REQUIRED_SIG_KEYS; for (int key_idx = 0; key_idx < rkeys.length; key_idx++) { String key = rkeys[key_idx]; SignatureEntry entry = signature_object.getSigEntry(key); String value = entry.getValue(); if (SignatureTypes.SIG_ID.equals(key) && value == null) { continue; } value = value.replaceAll("\\s", " "); value = value.replaceAll("\\\"", "\\\""); String input_key = name_prefix + key; writer.println(" document.submitform." + input_key + ".value = \"" + value + "\";"); } return sw.toString(); } public static String renderRequiredKeys(SignatureObject signature_object, String name_prefix, boolean write_value) { StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); writer.println(""); String[] rkeys = SignatureTypes.REQUIRED_SIG_KEYS; for (int key_idx = 0; key_idx < rkeys.length; key_idx++) { String key = rkeys[key_idx]; SignatureEntry entry = signature_object.getSigEntry(key); String caption = entry.getCaption(); String value = entry.getValue(); if (SignatureTypes.SIG_ID.equals(key) && value == null) { continue; } writer.println(" "); writer.println(" "); writer.println(" "); writer.println(" "); } writer.println("
" + caption + ":"); value = value.replaceAll("\\s", " "); value = value.replaceAll("\\\"", "\\\""); String input_key = name_prefix + key; writer.println(" "); writer.println("
"); return sw.toString(); } public static String renderRequiredKeysText(SignatureObject signature_object) { StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); String title = "alte PDF-AS Signatur"; try { PdfASID kz = signature_object.getKZ(); if (kz != null) { title = kz.toString(); } } catch (InvalidIDException e) { e.printStackTrace(); } writer.println("
Signatur: " + title + "
"); writer.println(""); // just render useful information String[] rkeys = { SignatureTypes.SIG_DATE, SignatureTypes.SIG_ISSUER, SignatureTypes.SIG_NUMBER }; // SignatureTypes.REQUIRED_SIG_KEYS; for (int key_idx = 0; key_idx < rkeys.length; key_idx++) { String key = rkeys[key_idx]; SignatureEntry entry = signature_object.getSigEntry(key); String caption = entry.getCaption(); String value = entry.getValue(); if (SignatureTypes.SIG_ID.equals(key) && value == null) { continue; } writer.println(" "); writer.println(" "); writer.println(" "); writer.println(" "); } writer.println("
" + caption + ":"); value = value.replaceAll("\\s", " "); value = value.replaceAll("\\\"", "\\\""); writer.println(" " + value); writer.println("
"); return sw.toString(); } protected void dispatchToResults(List results, HttpServletRequest request, HttpServletResponse response, boolean backbutton) throws ServletException, IOException { request.setAttribute("results", results); request.setAttribute("backbutton", backbutton ? "true" : "false"); dispatch(request, response, "/jsp/results.jsp"); } // public static void formatVerifyResults(List results, // HttpServletResponse response) throws IOException, SettingNotFoundException // { // response.setContentType("text/html"); // response.setCharacterEncoding("UTF-8"); // // PrintWriter writer = response.getWriter(); // // writer.println(""); // writer.println(""); // writer.println(""); // writer.println(""); // writer.println("PDF-AS Amtssignaturen Resultat"); // writer.println(""); // writer.println(""); // // writer.println(""); // writer.println("
"); // // writer.println("

PDF-AS Amtssignaturen

"); // // writer.println("
"); // writer.println("
Resultat
"); // writer.println("
"); // // Iterator it = results.iterator(); // while (it.hasNext()) // { // SignatureResponse result = (SignatureResponse) it.next(); // formatSignatureResponse(result, writer); // // if (it.hasNext()) // { // writer.println("
"); // } // } // // writer.println("
"); // writer.println("
"); // // writer.println("zurück"); // // writer.println("
"); // writer.println(""); // // writer.println(""); // // } // // public static void formatSignatureResponse(SignatureResponse result, // PrintWriter writer) throws SettingNotFoundException // { // writer.println("
Zertifikat"); // writer.println(""); // writer.println(" "); // writer.println(" "); // writer.println(" "); // List public_properties = result.getPublicProperties(); // Iterator it = public_properties.iterator(); // while (it.hasNext()) // { // String public_property = (String) it.next(); // writer.println(" "); // } // // writer.println(" "); // // writer.println("
Signator:" + // result.getX509SubjectName() + "
Aussteller:" + // result.getX509IssuerName() + "
Seriennummer:" + // result.getX509SerialNumber() + "
Eigenschaft:" + // public_property + "
Zertifikat:" + result.getCertificateCheckInfo() // + "
"); // writer.println("
"); // writer.println("
Signatur-Check"); // writer.println("
" + result.getSignatureCheckInfo() + "
"); // writer.println("
"); // writer.println("
Manifest-Check"); // writer.println("
" + // result.getSignatureManifestCheckInfo() + "
"); // writer.println("
"); // } /** * Form field name prefix for signature table names */ public static final String SIG_INPUT_PREFIX = "sig_inp_"; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SessionInformation si = null; HttpSession session = null; try { session = request.getSession(false); // String session_id_string = request.getParameter("session"); if (session == null) { throw new ErrorCodeException(600, "The session is missing."); } si = (SessionInformation) session.getAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION); // long session_id = Long.parseLong(session_id_string); // si = SessionTable.get(session_id); if (si == null) { throw new ErrorCodeException(600, "The session is not found or is no longer valid."); } } catch (PresentableException e) { e.printStackTrace(); Sign.prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); return; } try { int verify_which = -1; boolean preview = false; { String str_verify_which = request.getParameter(FormFields.FIELD_VERIFY_WHICH); logger_.debug("verify_which = " + str_verify_which); if (str_verify_which.equals(FormFields.VALUE_VERIFY_WHICH_ALL)) { verify_which = -1; } else { verify_which = Integer.parseInt(str_verify_which); } logger_.debug("verify_which = " + verify_which); } { String preview_string = request.getParameter(FormFields.FIELD_PREVIEW); if (preview_string.equals("true")) { preview = true; } } List holders_to_verify = si.signature_holders; if (verify_which >= 0) { if (verify_which >= si.signature_holders.size()) { throw new SignatureException(312, "The selected signature to be verified doesn't exist."); } SignatureHolder holder = (SignatureHolder) si.signature_holders.get(verify_which); holders_to_verify = new ArrayList(); holders_to_verify.add(holder); if (preview == true) { if (holder instanceof BinarySignatureHolder) { BinarySignatureHolder binary_holder = (BinarySignatureHolder) holder; response.setContentType("application/pdf"); response.getOutputStream().write(binary_holder.getSignedPdf(), 0, binary_holder.getSignedPdfLength()); } else // if (holder.getSignatureObject().isTextual()) { // formatPreview(holders_to_verify, si.connector, request, // response); request.setAttribute(FormFields.FIELD_VERIFY_WHICH, new Integer(verify_which)); dispatch(request, response, "/jsp/verifypreview.jsp"); } return; } } if (ConnectorFactory.isConnectorLocal(si.connector)) { LocalRequestHelper.processLocalVerify(si, holders_to_verify, request, response); dispatch(request, response, LocalRequestHelper.LOCAL_CONNECTION_PAGE_JSP); return; } List results = PdfAS.verifySignatureHolders(holders_to_verify, si.connector); boolean backbutton = true; if (verify_which >= 0) { backbutton = false; } dispatchToResults(results, request, response, backbutton); } catch (PresentableException e) { e.printStackTrace(); Sign.prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SessionInformation si = null; HttpSession session = null; try { session = request.getSession(false); // String session_id_string = request.getParameter("session"); if (session == null) { throw new ErrorCodeException(600, "The session is missing."); } si = (SessionInformation) session.getAttribute(SessionAttributes.ATTRIBUTE_SESSION_INFORMATION); // long session_id = Long.parseLong(session_id_string); // si = SessionTable.get(session_id); if (si == null) { throw new ErrorCodeException(600, "The session is not found or is no longer valid."); } } catch (PresentableException e) { e.printStackTrace(); Sign.prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); return; } DiskFileItemFactory fif = new DiskFileItemFactory(); fif.setRepository(SettingsReader.getTemporaryDirectory()); ServletFileUpload sfu = new ServletFileUpload(fif); try { String text_to_be_verified = null; SignatureObject signature_object = new SignatureObject(); String default_type = SettingsReader.getInstance().getValueFromKey(SignatureTypes.DEFAULT_TYPE); signature_object.setSigType(default_type); signature_object.initByType(); int verify_which = -1; boolean verify = false; List items = sfu.parseRequest(request); Iterator it = items.iterator(); while (it.hasNext()) { FileItem item = (FileItem) it.next(); logger_.debug("item = " + item.getFieldName()); if (item.getFieldName().equals(FormFields.FIELD_RAW_DOCUMENT_TEXT)) { String raw_document_text = item.getString("UTF-8"); text_to_be_verified = PdfAS.normalizeText(raw_document_text); continue; } if (item.getFieldName().equals(FormFields.FIELD_VERIFY_WHICH)) { String which_str = item.getString("UTF-8"); verify_which = Integer.parseInt(which_str); continue; } if (item.getFieldName().equals("verify")) { if (item.getString("UTF-8").equals("true")) { verify = true; } continue; } String key = item.getFieldName(); String value = item.getString("UTF-8"); signature_object.setSigValue(key, value); logger_.debug("sig_obj_number" + key + " = " + value); } SignatureHolder new_holder = new TextualSignatureHolder(text_to_be_verified, signature_object); si.signature_holders.set(verify_which, new_holder); if (verify == false) { dispatch(request, response, "/dataok.html"); return; } List holders_to_verify = si.signature_holders; if (verify_which >= 0) { if (verify_which >= si.signature_holders.size()) { throw new SignatureException(312, "The selected signature to be verified doesn't exist."); } SignatureHolder holder = (SignatureHolder) si.signature_holders.get(verify_which); holders_to_verify = new ArrayList(); holders_to_verify.add(holder); } if (ConnectorFactory.isConnectorLocal(si.connector)) { LocalRequestHelper.processLocalVerify(si, holders_to_verify, request, response); dispatch(request, response, LocalRequestHelper.LOCAL_CONNECTION_PAGE_JSP); return; } List results = PdfAS.verifySignatureHolders(holders_to_verify, si.connector); dispatchToResults(results, request, response, true); } 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"); } } }