Example usage for com.itextpdf.text ExceptionConverter getException

List of usage examples for com.itextpdf.text ExceptionConverter getException

Introduction

In this page you can find the example usage for com.itextpdf.text ExceptionConverter getException.

Prototype

public Exception getException() 

Source Link

Document

and allow the user of ExceptionConverter to get a handle to it.

Usage

From source file:cz.hobrasoft.pdfmu.operation.signature.OperationSignatureAdd.java

License:Open Source License

private static void sign(PdfSignatureAppearance sap, ExternalDigest externalDigest,
        ExternalSignature externalSignature, Certificate[] chain, TSAClient tsaClient,
        MakeSignature.CryptoStandard sigtype) throws OperationException {
    // TODO?: Set some of the following parameters more sensibly

    // Certificate Revocation List
    // digitalsignatures20130304.pdf : Section 3.2
    Collection<CrlClient> crlList = null;

    // Online Certificate Status Protocol
    // digitalsignatures20130304.pdf : Section 3.2.4
    OcspClient ocspClient = null;/*ww  w.ja va  2  s  . c om*/

    // digitalsignatures20130304.pdf : Section 3.5
    // The value of 0 means "try a generous educated guess".
    // We need not change this unless we want to optimize the resulting PDF document size.
    int estimatedSize = 0;

    logger.info(String.format("Cryptographic standard (signature format): %s", sigtype));

    try {
        MakeSignature.signDetached(sap, externalDigest, externalSignature, chain, crlList, ocspClient,
                tsaClient, estimatedSize, sigtype);
    } catch (ExceptionConverter ex) {
        Exception exInner = ex.getException();
        if (exInner instanceof IOException) {
            if (exInner instanceof SSLHandshakeException) {
                Set<ExceptionMessagePattern> patterns = new HashSet<>();

                // Untrusted
                patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_TSA_UNTRUSTED,
                        "sun\\.security\\.validator\\.ValidatorException: PKIX path building failed: sun\\.security\\.provider\\.certpath\\.SunCertPathBuilderException: unable to find valid certification path to requested target",
                        new ArrayList<String>()));

                // Bad certificate
                patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_TSA_BAD_CERTIFICATE,
                        "Received fatal alert: bad_certificate", new ArrayList<String>()));

                // Handshake failure
                patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_TSA_HANDSHAKE_FAILURE,
                        "Received fatal alert: handshake_failure", new ArrayList<String>()));

                OperationException oe = null;
                for (ExceptionMessagePattern p : patterns) {
                    oe = p.getOperationException(exInner);
                    if (oe != null) {
                        break;
                    }
                }
                if (oe == null) {
                    ExceptionMessagePattern emp = new ExceptionMessagePattern(SIGNATURE_ADD_TSA_SSL_FATAL_ALERT,
                            "Received fatal alert: (?<alert>.*)", Arrays.asList(new String[] { "alert" }));
                    oe = emp.getOperationException(exInner);

                    if (oe == null) {
                        // Unknown exception
                        oe = new OperationException(SIGNATURE_ADD_TSA_SSL_HANDSHAKE_EXCEPTION, exInner);
                    }
                }
                assert oe != null;
                throw oe;
            }

            if (exInner instanceof SSLException) {
                ExceptionMessagePattern emp = new ExceptionMessagePattern(SSL_TRUSTSTORE_EMPTY,
                        "java\\.lang\\.RuntimeException: Unexpected error: java\\.security\\.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty",
                        new ArrayList<String>());
                OperationException oe = emp.getOperationException(exInner);
                if (oe != null) {
                    throw oe;
                }
                throw new OperationException(SIGNATURE_ADD_FAIL, exInner);
            }

            if (exInner instanceof UnknownHostException || exInner instanceof FileNotFoundException) {
                String host = exInner.getMessage();
                throw new OperationException(SIGNATURE_ADD_TSA_UNREACHABLE, exInner,
                        new SimpleEntry<String, Object>("host", host));
            }

            if (exInner instanceof SocketException) {
                ExceptionMessagePattern emp = new ExceptionMessagePattern(SSL_TRUSTSTORE_INCORRECT_TYPE,
                        "java\\.security\\.NoSuchAlgorithmException: Error constructing implementation \\(algorithm: (?<algorithm>.*), provider: (?<provider>.*), class: (?<class>.*)\\)",
                        Arrays.asList(new String[] { "algorithm", "provider", "class" }));
                OperationException oe = emp.getOperationException(exInner);
                if (oe != null) {
                    throw oe;
                }
                throw new OperationException(SIGNATURE_ADD_FAIL, exInner);
            }

            Set<ExceptionMessagePattern> patterns = new HashSet<>();

            // No username
            // May also be returned if the username and password are incorrect.
            patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_TSA_UNAUTHORIZED,
                    "Server returned HTTP response code: 401 for URL: (?<url>.*)",
                    Arrays.asList(new String[] { "url" })));

            // Incorrect username or incorrect password
            patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_TSA_LOGIN_FAIL,
                    "Invalid TSA '(?<url>.*)' response, code (?<code>\\d+)",
                    Arrays.asList(new String[] { "url", "code" })));

            patterns.add(new ExceptionMessagePattern(SIGNATURE_ADD_FAIL, "unknown tag (?<tag>\\d+) encountered",
                    Arrays.asList(new String[] { "tag" })));

            OperationException oe = null;
            for (ExceptionMessagePattern p : patterns) {
                oe = p.getOperationException(exInner);
                if (oe != null) {
                    break;
                }
            }
            if (oe == null) {
                // Unknown exception
                oe = new OperationException(SIGNATURE_ADD_FAIL, exInner);
            }
            assert oe != null;
            throw oe;
        }
        throw new OperationException(SIGNATURE_ADD_FAIL, exInner);
    } catch (SignatureException ex) {
        throw new OperationException(SIGNATURE_ADD_SIGNATURE_EXCEPTION, ex);
    } catch (IOException | DocumentException | GeneralSecurityException ex) {
        throw new OperationException(SIGNATURE_ADD_FAIL, ex);
    } catch (NullPointerException ex) {
        // Invalid digest algorithm?
        throw new OperationException(SIGNATURE_ADD_FAIL, ex);
    }
    logger.info("Document successfully signed.");
}