Search in sources :

Example 1 with AS2CertificateExistsException

use of com.helger.as2lib.cert.AS2CertificateExistsException in project as2-lib by phax.

the class AS2Client method initCertificateFactory.

/**
 * This method initializes the certificate factory. If you override this
 * method, please make sure that you call
 * <code>aSession.setCertificateFactory (aCertFactory);</code>.
 *
 * @param aSettings
 *        The AS2 client settings. Never <code>null</code>.
 * @param aSession
 *        The AS2 session to be initialized. Never <code>null</code>.
 * @throws AS2Exception
 *         In case of error
 */
@OverrideOnDemand
protected void initCertificateFactory(@Nonnull final AS2ClientSettings aSettings, @Nonnull final AS2Session aSession) throws AS2Exception {
    final StringMap aParams = new StringMap();
    // TYPE is the only parameter that must be present in initDynamicComponents
    aParams.putIn(AbstractCertificateFactory.ATTR_TYPE, aSettings.getKeyStoreType().getID());
    final CertificateFactory aCertFactory = createCertificateFactory();
    aCertFactory.initDynamicComponent(aSession, aParams);
    if (aSettings.getKeyStoreFile() != null) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Loading AS2 client keystore from file " + aSettings.getKeyStoreFile());
        aCertFactory.setFilename(aSettings.getKeyStoreFile().getAbsolutePath());
        aCertFactory.setPassword(aSettings.getKeyStorePassword());
        aCertFactory.setSaveChangesToFile(aSettings.isSaveKeyStoreChangesToFile());
        aCertFactory.load();
    } else if (aSettings.getKeyStoreBytes() != null && aSettings.getKeyStorePassword() != null) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Loading AS2 client keystore from byte array. No changes will be saved.");
        aCertFactory.setPassword(aSettings.getKeyStorePassword());
        aCertFactory.setSaveChangesToFile(false);
        try (final NonBlockingByteArrayInputStream aBAIS = new NonBlockingByteArrayInputStream(aSettings.getKeyStoreBytes())) {
            aCertFactory.load(aBAIS, aSettings.getKeyStorePassword().toCharArray());
        }
    } else {
        if (LOGGER.isInfoEnabled())
            LOGGER.warn("No AS2 client keystore data was provided. Signing and encryption/decryption will most likely fail.");
        // No file provided - no storage
        aCertFactory.setSaveChangesToFile(false);
    }
    if (aSettings.getReceiverCertificate() != null) {
        // Dynamically add recipient certificate if provided
        try {
            aCertFactory.addCertificate(aSettings.getReceiverKeyAlias(), aSettings.getReceiverCertificate(), false);
        } catch (final AS2CertificateExistsException ex) {
        // ignore
        }
    }
    aSession.setCertificateFactory(aCertFactory);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) StringMap(com.helger.commons.collection.attr.StringMap) AS2CertificateExistsException(com.helger.as2lib.cert.AS2CertificateExistsException) AbstractCertificateFactory(com.helger.as2lib.cert.AbstractCertificateFactory) CertificateFactory(com.helger.as2lib.cert.CertificateFactory) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Aggregations

AS2CertificateExistsException (com.helger.as2lib.cert.AS2CertificateExistsException)1 AbstractCertificateFactory (com.helger.as2lib.cert.AbstractCertificateFactory)1 CertificateFactory (com.helger.as2lib.cert.CertificateFactory)1 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 StringMap (com.helger.commons.collection.attr.StringMap)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1