formsignedsignaturewidgetfactory.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 534 行 · 第 1/2 页
JAVA
534 行
// $Id: FormSignedSignatureWidgetFactory.java,v 1.12 2007/11/08 09:44:18 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import org.faceless.pdf2.Event;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.awt.*;import java.security.*;import java.security.cert.*;import java.security.cert.Certificate;import java.io.*;import javax.swing.filechooser.FileFilter;import java.util.*;import java.text.*;import java.awt.event.*;/** * Create annotations to handle {@link WidgetAnnotation} objects belonging to signed * {@link FormSignature} fields. As supplied, this class will verify signatures against * certificates found in the {@link KeyStore} returned from * {@link FormSignature#loadDefaultKeyStore}, but it's possible to create an instance of * this factory that verifies against a KeyStore passed in to {@link #setKeyStoreManager setKeyStoreManager()}. * Then just remove the default <code>FormSignedSignatureWidgetFactory</code> from the * features passed in to the {@link PDFViewer} constructor and replace it with that new * instance. * The name of this feature is "FormSignedSignatureWidgetFactory". * * <p><i>This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.</i></p> * @since 2.8 */public class FormSignedSignatureWidgetFactory extends AnnotationComponentFactory{ private KeyStoreManager keystoremanager; private PDFViewer viewer; /** * Create a new FormSignedSignatureWidgetFactory that verifies * against the default KeyStore. */ public FormSignedSignatureWidgetFactory() { super("FormSignedSignatureWidgetFactory"); } public void initialize(PDFViewer viewer) { this.viewer = viewer; } public boolean matches(PDFAnnotation annot) { return annot instanceof WidgetAnnotation && ((WidgetAnnotation)annot).getField() instanceof FormSignature && ((FormSignature)((WidgetAnnotation)annot).getField()).getState()==FormSignature.STATE_SIGNED; } public JComponent createComponent(final PagePanel pagepanel, PDFAnnotation annot) { final WidgetAnnotation widget = (WidgetAnnotation)annot; final DocumentPanel docpanel = pagepanel.getDocumentPanel(); final FormSignature field = (FormSignature)widget.getField(); final JComponent comp = new JPanel() { public void paintComponent(Graphics g) { SignatureState state = (SignatureState)docpanel.getClientProperty(field); AnnotationComponentFactory.paintComponent(this, "N", g); getIcon(state).paintIcon(this, g, 0, 0); if (isFocusOwner()) { ((Graphics2D)g).setStroke(new BasicStroke(1, 0, 0, 1, new float[] { 1, 1 }, 0)); g.setColor(Color.gray); g.drawRect(2, 2, getWidth()-6, getHeight()-6); } } }; comp.setOpaque(false); comp.setToolTipText(field.getDescription()); comp.addMouseListener(new MouseListener() { public void mouseEntered(MouseEvent event) { comp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } public void mouseExited(MouseEvent event) { comp.setCursor(Cursor.getDefaultCursor()); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseClicked(MouseEvent event) { SignatureState state = (SignatureState)docpanel.getClientProperty(field); if (state==null) { state = validate(field); docpanel.putClientProperty(field, state); comp.repaint(); } displaySignatureState(state, docpanel); } }); comp.addFocusListener(new FocusListener() { public void focusGained(FocusEvent event) { comp.repaint(); docpanel.runAction(widget.getAction(Event.FOCUS)); } public void focusLost(FocusEvent event) { comp.repaint(); docpanel.runAction(widget.getAction(Event.BLUR)); } }); return comp; } /** * A SignatureState contains information about a Signature once it's been verified. * This can be used to display information about the signatures in a popup window * to the user. */ public class SignatureState { private final FormSignature sig; private final Boolean validity; private final String reason; private final boolean alteredsince; private final X509Certificate cert; private final Exception exception; private SignatureState(FormSignature sig, Boolean validity, String reason, boolean alteredsince, X509Certificate cert, Exception exception) { this.validity = validity; this.reason = reason; this.alteredsince = alteredsince; this.cert = cert; this.exception = exception; this.sig = sig; } /** * Return the validity of the Signature, with true indicating good, false indicating * bad and null meaning unknown. */ public Boolean getValidity() { return validity; } /** * Return the descriptive text describing the state */ public String getReason() { return reason; } /** * Return true of the PDF was altered since the signature was applied. * Only useful if {@link #getValidity} returns true. */ public boolean isAlteredSince() { return alteredsince; } /** * Return the X.509 certificate that couldn't be verified, or <code>null</code> * if all the certificates passed or the signature isn't a PKCS#7 signature. */ public X509Certificate getCertificate() { return cert; } /** * Return the exception that occurred when trying to verify the signature or * certificate, or <code>null</code> if none was thrown */ public Exception getException() { return exception; } /** * Return the signature itself */ public FormSignature getSignature() { return sig; } } /** * Set the {@link KeyStoreManager} that signatures should be verified against * @param manager the keystore * @since 2.8.3 */ public void setKeyStoreManager(KeyStoreManager manager) { this.keystoremanager = manager; } private KeyStoreManager getKeyStoreManager() { if (viewer!=null) { return viewer.getKeyStoreManager(); } else { if (keystoremanager==null) { keystoremanager = KeyStoreManager.createDefaultKeyStoreManager(null); } return keystoremanager; } } /** * Validate the Signature and return it's validation state. */ public SignatureState validate(FormSignature sig) { boolean verified = false, verifiable = false, alteredsince = false; X509Certificate cert = null; Exception exception = null; Calendar when = sig.getSignDate(); String reason = null; Boolean validity = null; try { verified = sig.verify(); verifiable = true; } catch (Exception e) { if (sig.getSignatureHandler() instanceof PKCS7SignatureHandler) { validity = Boolean.FALSE; reason = SuperJOptionPane.getLocalizedString("ssig.ErrorVerifyingSig", e.toString()); exception = e; } else { validity = null; reason = SuperJOptionPane.getLocalizedString("ssig.NotPKCS7Sig"); } } if (verifiable) { if (sig.getNumberOfRevisionsCovered()==0) { validity = Boolean.FALSE; reason = SuperJOptionPane.getLocalizedString("ssig.NotEntireRevision"); } else { try { if (sig.getSignatureHandler() instanceof PKCS7SignatureHandler) { PKCS7SignatureHandler handler = (PKCS7SignatureHandler)sig.getSignatureHandler(); X509Certificate[] certs = handler.getCertificates(); cert = FormSignature.verifyCertificates(certs, getKeyStoreManager().getKeyStore(), null, when); } if (sig.getNumberOfRevisionsCovered()!=sig.getForm().getPDF().getNumberOfRevisions()) { alteredsince = true; } if (verified && verifiable && cert==null) { validity = Boolean.TRUE; reason = SuperJOptionPane.getLocalizedString("ssig.DocAndCertVerified"); } else if (verified && verifiable && cert!=null) { validity = Boolean.TRUE; reason = SuperJOptionPane.getLocalizedString("ssig.UnableToVerifyCerts"); } else { validity = Boolean.FALSE; reason = SuperJOptionPane.getLocalizedString("ssig.AlteredSinceSigning"); } } catch (Exception e) { validity = Boolean.FALSE; reason = SuperJOptionPane.getLocalizedString("ssig.ErrorVerifyingCerts", e.getMessage()); exception = e; } } } return new SignatureState(sig, validity, reason, alteredsince, cert, exception); } /** * Display a popup dialog that displays information about the SignatureState * @param state the SignatureState to display information on * @param docpanel the Component to display the dialog in */ public void displaySignatureState(final SignatureState state, final DocumentPanel docpanel) { final JTabbedPane tabbedpane = new JTabbedPane(JTabbedPane.LEFT); tabbedpane.setTabPlacement(JTabbedPane.TOP); tabbedpane.addTab(SuperJOptionPane.getLocalizedString("Summary"), getSignatureStatePanel(state)); tabbedpane.addTab(SuperJOptionPane.getLocalizedString("Timestamp"), getTimestampPanel(state, docpanel)); try { tabbedpane.addTab(SuperJOptionPane.getLocalizedString("Certificates"), getCertificatesPanel(((PKCS7SignatureHandler)state.getSignature().getSignatureHandler()).getCertificates(), state.getSignature(), state.getCertificate(), tabbedpane, docpanel)); } catch (Exception e) { } Window window = JOptionPane.getFrameForComponent(docpanel);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?