Skip to content

Commit

Permalink
Added and used AS4CryptoFactoryConfiguration.getDefaultInstanceOrNull ()
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Oct 29, 2024
1 parent 876c386 commit d9eaa1a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class AS4CryptoFactoryConfiguration extends AS4CryptoFactoryInMemoryKeySt
* @return The default instance, created by reading the default properties
* from the configuration sources (application.properties, environment
* variables and Java system properties).
* @throws RuntimeException
* if one of the mandatory configuration parameters is not present.
*/
@Nonnull
public static AS4CryptoFactoryConfiguration getDefaultInstance ()
Expand All @@ -69,6 +71,28 @@ public static AS4CryptoFactoryConfiguration getDefaultInstance ()
return new AS4CryptoFactoryConfiguration (AS4Configuration.getConfig ());
}

/**
* Same as {@link #getDefaultInstance()} just that it returns
* <code>null</code> instead of throwing a RuntimeException.
*
* @return <code>null</code> in case of error.
*/
@Nullable
public static AS4CryptoFactoryConfiguration getDefaultInstanceOrNull ()
{
try
{
return getDefaultInstance ();
}
catch (final RuntimeException ex)
{
// Use debug level only, as this is used in many default scenarios
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Failed to create AS4CryptoFactoryConfiguration default instance", ex);
return null;
}
}

private final IAS4KeyStoreDescriptor m_aKeyStoreDesc;
private final IAS4TrustStoreDescriptor m_aTrustStorDesc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected AbstractAS4MessageBuilder ()
{
httpClientFactory (new HttpClientFactory ());
// By default set the same for sign and crypt
cryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstance ());
cryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstanceOrNull ());
soapVersion (ESoapVersion.SOAP_12);
incomingAttachmentFactory (IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE);
}
Expand Down Expand Up @@ -237,7 +237,7 @@ public final IAS4CryptoFactory cryptoFactoryCrypt ()
/**
* Set the crypto factory to be used for crypting. The default crypto factory
* is set in the constructor to
* {@link AS4CryptoFactoryConfiguration#getDefaultInstance()}.
* {@link AS4CryptoFactoryConfiguration#getDefaultInstanceOrNull()}.
*
* @param aCryptoFactoryCrypt
* The crypto factory to be used. May be <code>null</code>.
Expand All @@ -254,7 +254,7 @@ public final IMPLTYPE cryptoFactoryCrypt (@Nullable final IAS4CryptoFactory aCry
/**
* Set the crypto factory to be used for signing and crypting. The default
* crypto factory is set in the constructor to
* {@link AS4CryptoFactoryConfiguration#getDefaultInstance()}.
* {@link AS4CryptoFactoryConfiguration#getDefaultInstanceOrNullOrNull()}.
*
* @param aCryptoFactory
* The crypto factory to be used. May be <code>null</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRe
final String sAS4ProfileID = AS4ProfileSelector.getDefaultAS4ProfileID ();

// Set default values in handler
aHandler.setCryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstance ());
aHandler.setCryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstanceOrNull ());
aHandler.setPModeResolver (new AS4DefaultPModeResolver (sAS4ProfileID));
aHandler.setIncomingProfileSelector (new AS4IncomingProfileSelectorConstant (sAS4ProfileID, true));
aHandler.setIncomingAttachmentFactory (IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ protected void initGlobalSettings ()
HttpDebugger.setEnabled (false);

// Sanity check
if (CommandMap.getDefaultCommandMap ()
.createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) == null)
if (CommandMap.getDefaultCommandMap ().createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) ==
null)
throw new IllegalStateException ("No DataContentHandler for MIME Type '" +
CMimeType.MULTIPART_RELATED.getAsString () +
"' is available. There seems to be a problem with the dependencies/packaging");
Expand Down Expand Up @@ -187,20 +187,18 @@ public void onEndRequest (@Nonnull final IAS4IncomingMessageMetadata aMessageMet
if (SimpleFileIO.writeFile (aFile,
AS4IncomingHelper.getIncomingMetadataAsJson (aMessageMetadata)
.getAsJsonString (JsonWriterSettings.DEFAULT_SETTINGS_FORMATTED),
StandardCharsets.UTF_8)
.isFailure ())
StandardCharsets.UTF_8).isFailure ())
LOGGER.error ("Failed to write metadata to '" + aFile.getAbsolutePath () + "'");
else
LOGGER.info ("Wrote metadata to '" + aFile.getAbsolutePath () + "'");
}
});

// Store the outgoings file as well
AS4DumpManager.setOutgoingDumper (new AS4OutgoingDumperFileBased ( (eMsgMode,
sMessageID,
nTry) -> StorageHelper.getStorageFile (sMessageID,
nTry,
".as4out")));
AS4DumpManager.setOutgoingDumper (new AS4OutgoingDumperFileBased ( (eMsgMode, sMessageID, nTry) -> StorageHelper
.getStorageFile (sMessageID,
nTry,
".as4out")));
}

private static void _initPeppolAS4 ()
Expand All @@ -223,6 +221,7 @@ private static void _initPeppolAS4 ()
AS4Configuration.getConfig ().getAsInt ("http.proxy.port")));
PeppolCRLDownloader.setAsDefaultCRLCache (aHCS);

// Throws an exception if configuration parameters are missing
final AS4CryptoFactoryConfiguration aCF = AS4CryptoFactoryConfiguration.getDefaultInstance ();

// Check if crypto properties are okay - fail early if something is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ protected void initGlobalSettings ()
HttpDebugger.setEnabled (false);

// Sanity check
if (CommandMap.getDefaultCommandMap ()
.createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) == null)
if (CommandMap.getDefaultCommandMap ().createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) ==
null)
throw new IllegalStateException ("No DataContentHandler for MIME Type '" +
CMimeType.MULTIPART_RELATED.getAsString () +
"' is available. There seems to be a problem with the dependencies/packaging");
Expand Down Expand Up @@ -153,6 +153,7 @@ private static void _initAS4 ()

// Check if crypto properties are okay
{
// Throws an exception if configuration parameters are missing
final IAS4CryptoFactory aCF = AS4CryptoFactoryConfiguration.getDefaultInstance ();

final KeyStore aKS = aCF.getKeyStore ();
Expand Down Expand Up @@ -180,26 +181,25 @@ public void onEndRequest (@Nonnull final IAS4IncomingMessageMetadata aMessageMet
if (SimpleFileIO.writeFile (aFile,
AS4IncomingHelper.getIncomingMetadataAsJson (aMessageMetadata)
.getAsJsonString (JsonWriterSettings.DEFAULT_SETTINGS_FORMATTED),
StandardCharsets.UTF_8)
.isFailure ())
StandardCharsets.UTF_8).isFailure ())
LOGGER.error ("Failed to write metadata to '" + aFile.getAbsolutePath () + "'");
else
LOGGER.info ("Wrote metadata to '" + aFile.getAbsolutePath () + "'");
}
});

// Store the outgoings file as well
AS4DumpManager.setOutgoingDumper (new AS4OutgoingDumperFileBased ( (eMsgMode,
sMessageID,
nTry) -> StorageHelper.getStorageFile (sMessageID,
nTry,
".as4out")));
AS4DumpManager.setOutgoingDumper (new AS4OutgoingDumperFileBased ( (eMsgMode, sMessageID, nTry) -> StorageHelper
.getStorageFile (sMessageID,
nTry,
".as4out")));
}

@Override
protected void initManagers ()
{
_initAS4 ();
// Throws an exception if configuration parameters are missing
DropFolderUserMessage.init (AS4CryptoFactoryConfiguration.getDefaultInstance ());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ public static IJsonObject getDefaultStatusData ()
aStatusData.add ("phase4.version", CAS4Version.BUILD_VERSION);
aStatusData.add ("phase4.build-timestamp", CAS4Version.BUILD_TIMESTAMP);

final IAS4CryptoFactory aCF = AS4CryptoFactoryConfiguration.getDefaultInstance ();
final KeyStore aKS = aCF.getKeyStore ();
aStatusData.add ("phase4.keystore.loaded", aKS != null);
if (aKS != null)
final IAS4CryptoFactory aCF = AS4CryptoFactoryConfiguration.getDefaultInstanceOrNull ();
if (aCF != null)
{
aStatusData.add ("phase4.keystore.key.alias", aCF.getKeyAlias ());
final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
aStatusData.add ("phase4.keystore.key.loaded", aPKE != null);
if (aPKE != null)
final KeyStore aKS = aCF.getKeyStore ();
aStatusData.add ("phase4.keystore.loaded", aKS != null);
if (aKS != null)
{
final X509Certificate aCert = (X509Certificate) aPKE.getCertificate ();
aStatusData.add ("phase4.keystore.key.issuer", aCert.getIssuerX500Principal ().getName ());
aStatusData.add ("phase4.keystore.key.subject", aCert.getSubjectX500Principal ().getName ());
aStatusData.add ("phase4.keystore.key.alias", aCF.getKeyAlias ());
final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
aStatusData.add ("phase4.keystore.key.loaded", aPKE != null);
if (aPKE != null)
{
final X509Certificate aCert = (X509Certificate) aPKE.getCertificate ();
aStatusData.add ("phase4.keystore.key.issuer", aCert.getIssuerX500Principal ().getName ());
aStatusData.add ("phase4.keystore.key.subject", aCert.getSubjectX500Principal ().getName ());
}
}
}

return aStatusData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ private static void _initGlobalSettings (@Nonnull final ServletContext aSC)
HttpDebugger.setEnabled (false);

// Sanity check
if (CommandMap.getDefaultCommandMap ()
.createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) == null)
if (CommandMap.getDefaultCommandMap ().createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) ==
null)
throw new IllegalStateException ("No DataContentHandler for MIME Type '" +
CMimeType.MULTIPART_RELATED.getAsString () +
"' is available. There seems to be a problem with the dependencies/packaging");
Expand Down Expand Up @@ -245,13 +245,16 @@ private static void _initPeppolAS4 ()
// resources, it can be configured here
PeppolCRLDownloader.setAsDefaultCRLCache (new Phase4PeppolHttpClientSettings ());

// Throws an exception if configuration parameters are missing
final IAS4CryptoFactory aCF = getCryptoFactoryToUse ();

// Check if crypto properties are okay
final KeyStore aKS = AS4CryptoFactoryConfiguration.getDefaultInstance ().getKeyStore ();
final KeyStore aKS = aCF.getKeyStore ();
if (aKS == null)
throw new InitializationException ("Failed to load configured AS4 Key store - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 key store from the crypto factory");

final KeyStore.PrivateKeyEntry aPKE = AS4CryptoFactoryConfiguration.getDefaultInstance ().getPrivateKeyEntry ();
final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
if (aPKE == null)
throw new InitializationException ("Failed to load configured AS4 private key - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 private key from the crypto factory");
Expand Down

0 comments on commit d9eaa1a

Please sign in to comment.