From 096e03f73df66753e2637d067d3584f27a8d2c31 Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Thu, 24 Oct 2024 21:08:20 +0200 Subject: [PATCH] Improved AS4 profile handling --- .../com/helger/phase4/dump/AS4DumpReader.java | 20 +++-- .../phase4/incoming/AS4IncomingHandler.java | 10 +-- .../AS4IncomingProfileSelectorFromGlobal.java | 2 +- .../phase4/incoming/AS4RequestHandler.java | 10 +-- .../incoming/IAS4IncomingProfileSelector.java | 4 + .../incoming/mgr/AS4ProfileSelector.java | 46 +++++----- ...ElementProcessorExtractEbms3Messaging.java | 29 +++++-- .../SoapHeaderElementProcessorRegistry.java | 4 +- ...lver.java => AS4DefaultPModeResolver.java} | 71 +++++++-------- ...deResolver.java => IAS4PModeResolver.java} | 10 ++- .../phase4/profile/AS4ProfileManager.java | 87 ++----------------- .../phase4/profile/IAS4ProfileManager.java | 40 --------- .../phase4/profile/IAS4ProfileRegistrar.java | 9 -- .../sender/AS4BidirectionalClientHelper.java | 8 +- .../sender/AbstractAS4MessageBuilder.java | 41 +++++---- .../sender/AbstractAS4PullRequestBuilder.java | 29 ++++++- .../phase4/servlet/AS4XServletHandler.java | 9 +- .../tools/MainDecipherAS4In.java | 3 +- .../tools/MainDecipherAS4In.java | 8 +- .../servlet/Phase4PeppolWebAppListener.java | 2 +- .../bdew/AS4BDEWProfileRegistarSPI.java | 1 - .../profile/cef/AS4CEFProfileRegistarSPI.java | 3 - .../AS4DBNAllianceProfileRegistarSPI.java | 1 - .../eespa/AS4EESPAProfileRegistarSPI.java | 31 +++---- .../entsog/AS4ENTSOGProfileRegistarSPI.java | 1 - .../euctp/AS4EuCtpProfileRegistarSPI.java | 1 - .../peppol/AS4PeppolProfileRegistarSPI.java | 1 - .../springboot/servlet/ServletConfig.java | 2 +- .../profile/AS4TestProfileRegistarSPI.java | 4 +- .../client/AS4ClientUserMessageTest.java | 5 +- .../message/AbstractUserMessageTestSetUp.java | 5 +- .../phase4/server/servlet/CEFProfileTest.java | 4 +- 32 files changed, 214 insertions(+), 287 deletions(-) rename phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/{DefaultPModeResolver.java => AS4DefaultPModeResolver.java} (59%) rename phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/{IPModeResolver.java => IAS4PModeResolver.java} (87%) diff --git a/phase4-lib/src/main/java/com/helger/phase4/dump/AS4DumpReader.java b/phase4-lib/src/main/java/com/helger/phase4/dump/AS4DumpReader.java index fc0a913b0..8e9fb2fcf 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/dump/AS4DumpReader.java +++ b/phase4-lib/src/main/java/com/helger/phase4/dump/AS4DumpReader.java @@ -59,7 +59,7 @@ import com.helger.phase4.incoming.spi.AS4SignalMessageProcessorResult; import com.helger.phase4.incoming.spi.IAS4IncomingMessageProcessorSPI; import com.helger.phase4.model.pmode.IPMode; -import com.helger.phase4.model.pmode.resolve.DefaultPModeResolver; +import com.helger.phase4.model.pmode.resolve.AS4DefaultPModeResolver; import com.helger.phase4.util.Phase4Exception; import com.helger.servlet.mock.MockServletContext; import com.helger.web.scope.mgr.WebScopeManager; @@ -97,7 +97,7 @@ private AS4DumpReader () /** * Utility method to just read and consume the leading HTTP headers from a * dump. Usually this method is not called explicitly but invoked directly by - * {@link #decryptAS4In(byte[], IAS4CryptoFactory, IAS4CryptoFactory, Consumer, IDecryptedPayloadConsumer)} + * {@link #decryptAS4In(String, byte[], IAS4CryptoFactory, IAS4CryptoFactory, Consumer, IDecryptedPayloadConsumer)} * * @param aAS4InData * The byte array with the dump. May not be null. @@ -169,6 +169,9 @@ public static void readAndSkipInitialHttpHeaders (@Nonnull final byte [] aAS4InD * Note: this method was mainly created for internal use and does not win the * prize for the most sexy piece of software in the world ;-) * + * @param sAS4ProfileID + * The AS4 profile ID to use. May neither be null nor + * empty. * @param aAS4InData * The byte array with the dumped data. * @param aCryptoFactorySign @@ -191,7 +194,8 @@ public static void readAndSkipInitialHttpHeaders (@Nonnull final byte [] aAS4InD * @throws MessagingException * In case of error */ - public static void decryptAS4In (@Nonnull final byte [] aAS4InData, + public static void decryptAS4In (@Nonnull @Nonempty final String sAS4ProfileID, + @Nonnull final byte [] aAS4InData, @Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, @Nullable final Consumer aHttpHeaderConsumer, @@ -200,6 +204,12 @@ public static void decryptAS4In (@Nonnull final byte [] aAS4InData, IOException, MessagingException { + ValueEnforcer.notEmpty (sAS4ProfileID, "AS4ProfileID"); + ValueEnforcer.notNull (aAS4InData, "AS4InData"); + ValueEnforcer.notNull (aCryptoFactorySign, "CryptoFactorySign"); + ValueEnforcer.notNull (aCryptoFactoryCrypt, "CryptoFactoryCrypt"); + ValueEnforcer.notNull (aDecryptedConsumer, "DecryptedConsumer"); + final HttpHeaderMap hm = new HttpHeaderMap (); final MutableInt aHttpEndIndex = new MutableInt (-1); readAndSkipInitialHttpHeaders (aAS4InData, hm::setAllHeaders, aHttpEndIndex::set); @@ -219,12 +229,12 @@ public static void decryptAS4In (@Nonnull final byte [] aAS4InData, } try (final WebScoped w = new WebScoped (); - final AS4RequestHandler aHandler = new AS4RequestHandler (AS4IncomingMessageMetadata.createForRequest ())) + final AS4RequestHandler aHandler = new AS4RequestHandler (AS4IncomingMessageMetadata.createForRequest ())) { // Set default values in handler aHandler.setCryptoFactorySign (aCryptoFactorySign); aHandler.setCryptoFactoryCrypt (aCryptoFactoryCrypt); - aHandler.setPModeResolver (DefaultPModeResolver.DEFAULT_PMODE_RESOLVER); + aHandler.setPModeResolver (new AS4DefaultPModeResolver (sAS4ProfileID)); aHandler.setIncomingAttachmentFactory (IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE); aHandler.setIncomingSecurityConfiguration (AS4IncomingSecurityConfiguration.createDefaultInstance ()); aHandler.setIncomingReceiverConfiguration (new AS4IncomingReceiverConfiguration ()); diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingHandler.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingHandler.java index 74b44dcb6..2b7b70fc3 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingHandler.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingHandler.java @@ -81,7 +81,7 @@ import com.helger.phase4.model.message.MessageHelperMethods; import com.helger.phase4.model.pmode.IPMode; import com.helger.phase4.model.pmode.leg.PModeLeg; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; import com.helger.phase4.profile.IAS4Profile; import com.helger.phase4.profile.IAS4ProfileValidator; import com.helger.phase4.profile.IAS4ProfileValidator.EAS4ProfileValidationMode; @@ -824,7 +824,7 @@ public static IAS4IncomingMessageState processEbmsMessage (@Nonnull @WillNotClos @Nullable private static IAS4IncomingMessageState _parseMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aAS4ProfileSelector, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @@ -910,7 +910,7 @@ private static IAS4IncomingMessageState _parseMessage (@Nonnull final IAS4Crypto @Nullable public static Ebms3SignalMessage parseSignalMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aAS4ProfileSelector, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @@ -965,7 +965,7 @@ public static Ebms3SignalMessage parseSignalMessage (@Nonnull final IAS4CryptoFa @Nullable public static Ebms3UserMessage parseUserMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aAS4ProfileSelector, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @@ -1019,7 +1019,7 @@ public static Ebms3UserMessage parseUserMessage (@Nonnull final IAS4CryptoFactor @Nonnull public static ESuccess parseUserOrSignalMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aAS4ProfileSelector, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingProfileSelectorFromGlobal.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingProfileSelectorFromGlobal.java index c1da88b5a..28629854a 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingProfileSelectorFromGlobal.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4IncomingProfileSelectorFromGlobal.java @@ -37,7 +37,7 @@ public class AS4IncomingProfileSelectorFromGlobal implements IAS4IncomingProfile @Nullable public String getAS4ProfileID (@Nonnull final IAS4IncomingMessageState aIncomingState) { - return AS4ProfileSelector.getAS4ProfileID (); + return AS4ProfileSelector.getDefaultAS4ProfileID (); } public boolean validateAgainstProfile () diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4RequestHandler.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4RequestHandler.java index f78f558b6..290887efe 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4RequestHandler.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/AS4RequestHandler.java @@ -104,7 +104,7 @@ import com.helger.phase4.model.pmode.leg.EPModeSendReceiptReplyPattern; import com.helger.phase4.model.pmode.leg.PModeLeg; import com.helger.phase4.model.pmode.leg.PModeLegSecurity; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; import com.helger.phase4.profile.IAS4Profile; import com.helger.phase4.util.AS4ResourceHelper; import com.helger.phase4.util.AS4XMLHelper; @@ -351,7 +351,7 @@ public boolean hasAsyncResponseURL () private final IAS4IncomingMessageMetadata m_aMessageMetadata; private IAS4CryptoFactory m_aCryptoFactorySign; private IAS4CryptoFactory m_aCryptoFactoryCrypt; - private IPModeResolver m_aPModeResolver; + private IAS4PModeResolver m_aPModeResolver; private IAS4IncomingAttachmentFactory m_aIncomingAttachmentFactory; private IAS4IncomingSecurityConfiguration m_aIncomingSecurityConfig; private IAS4IncomingReceiverConfiguration m_aIncomingReceiverConfig; @@ -469,12 +469,12 @@ public final AS4RequestHandler setCryptoFactory (@Nonnull final IAS4CryptoFactor } /** - * @return The {@link IPModeResolver} to be used. May be null if + * @return The {@link IAS4PModeResolver} to be used. May be null if * not initialized. * @since 3.0.0 */ @Nullable - public final IPModeResolver getPModeResolver () + public final IAS4PModeResolver getPModeResolver () { return m_aPModeResolver; } @@ -486,7 +486,7 @@ public final IPModeResolver getPModeResolver () * @since 3.0.0 */ @Nonnull - public final AS4RequestHandler setPModeResolver (@Nonnull final IPModeResolver aPModeResolver) + public final AS4RequestHandler setPModeResolver (@Nonnull final IAS4PModeResolver aPModeResolver) { ValueEnforcer.notNull (aPModeResolver, "PModeResolver"); m_aPModeResolver = aPModeResolver; diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/IAS4IncomingProfileSelector.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/IAS4IncomingProfileSelector.java index 61630a42c..fd1ee64da 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/IAS4IncomingProfileSelector.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/IAS4IncomingProfileSelector.java @@ -19,12 +19,15 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import com.helger.phase4.v3.ChangePhase4V3; + /** * Callback interface to determine the used AS4 profile of an incoming message. * * @author Philip Helger * @since 0.13.0 */ +@ChangePhase4V3 ("As the AS4 profile ID is part of the sending process, reuse it for receiving") public interface IAS4IncomingProfileSelector { /** @@ -35,6 +38,7 @@ public interface IAS4IncomingProfileSelector * The message state of processing. Never null. * @return The AS4 profile ID or null if none was found. */ + // TODO remove? @Nullable String getAS4ProfileID (@Nonnull IAS4IncomingMessageState aIncomingState); diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/mgr/AS4ProfileSelector.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/mgr/AS4ProfileSelector.java index d935fa4d7..fc9a9bb36 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/mgr/AS4ProfileSelector.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/mgr/AS4ProfileSelector.java @@ -21,21 +21,16 @@ import javax.annotation.concurrent.ThreadSafe; import com.helger.commons.concurrent.SimpleReadWriteLock; +import com.helger.commons.string.StringHelper; import com.helger.phase4.config.AS4Configuration; -import com.helger.phase4.mgr.MetaAS4Manager; -import com.helger.phase4.profile.IAS4Profile; -import com.helger.phase4.profile.IAS4ProfileManager; -import com.helger.phase4.v3.ChangePhase4V3; /** - * Static helper class to make the AS4 profile selection more deterministic and - * flexible. + * Static helper class for the fallback AS4 profile selection. * * @author Philip Helger * @since 0.9.13 */ @ThreadSafe -@ChangePhase4V3 ("this class looks a bit superflowous and global. Think about a better solution") public final class AS4ProfileSelector { private static final SimpleReadWriteLock RW_LOCK = new SimpleReadWriteLock (); @@ -45,43 +40,48 @@ public final class AS4ProfileSelector private AS4ProfileSelector () {} + /** + * @return The custom default AS4 profile ID. Defaults to null. + */ @Nullable - public static String getCustomAS4ProfileID () + public static String getCustomDefaultAS4ProfileID () { return RW_LOCK.readLockedGet ( () -> s_sAS4ProfileID); } - public static void setCustomAS4ProfileID (@Nullable final String sAS4ProfileID) + /** + * Set the custom default AS4 profile ID. This has precedence over the + * configured AS4 default profile ID, to allow for a runtime change. + * + * @param sAS4ProfileID + * The AS4 profile ID to set. May be null. + */ + public static void setCustomDefaultAS4ProfileID (@Nullable final String sAS4ProfileID) { RW_LOCK.writeLocked ( () -> s_sAS4ProfileID = sAS4ProfileID); } /** - * Get the AS4 profile ID to be used in the following order: + * Get the default AS4 profile ID to be used in the following order: *
    - *
  1. From {@link #getCustomAS4ProfileID()}
  2. - *
  3. from the configuration properties
  4. - *
  5. from the current {@link IAS4ProfileManager} (since v2.3.0)
  6. + *
  7. From {@link #getCustomDefaultAS4ProfileID()}
  8. + *
  9. from the configuration properties.
  10. *
* * @return The AS4 profile ID to be used. May be null. */ @Nullable - public static String getAS4ProfileID () + public static String getDefaultAS4ProfileID () { - String ret = getCustomAS4ProfileID (); - if (ret == null) + // Is a custom default provided? + String ret = getCustomDefaultAS4ProfileID (); + if (StringHelper.hasNoText (ret)) { // Fall back to the configuration file // The profile ID from the configuration file is optional + // This should be the only place, where this method is called for + // evaluation - all other occurrences should use this method instead. ret = AS4Configuration.getDefaultAS4ProfileID (); - if (ret == null) - { - // Fall back to the default profile ID - final IAS4Profile aDefProfile = MetaAS4Manager.getProfileMgr ().getDefaultProfileOrNull (); - if (aDefProfile != null) - ret = aDefProfile.getID (); - } } return ret; } diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorExtractEbms3Messaging.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorExtractEbms3Messaging.java index 8316b144a..2d1e2c3a4 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorExtractEbms3Messaging.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorExtractEbms3Messaging.java @@ -67,7 +67,7 @@ import com.helger.phase4.model.mpc.IMPCManager; import com.helger.phase4.model.pmode.IPMode; import com.helger.phase4.model.pmode.leg.PModeLeg; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; import com.helger.xml.XMLHelper; /** @@ -85,7 +85,7 @@ public class SoapHeaderElementProcessorExtractEbms3Messaging implements ISoapHea private static final Logger LOGGER = LoggerFactory.getLogger (SoapHeaderElementProcessorExtractEbms3Messaging.class); - private final IPModeResolver m_aPModeResolver; + private final IAS4PModeResolver m_aPModeResolver; private final Consumer m_aPModeConsumer; private final IAS4IncomingReceiverConfiguration m_aIncomingReceiverConfiguration; @@ -101,7 +101,7 @@ public class SoapHeaderElementProcessorExtractEbms3Messaging implements ISoapHea * The incoming receiver configuration. May not be null. * Since v3.0.0. */ - public SoapHeaderElementProcessorExtractEbms3Messaging (@Nonnull final IPModeResolver aPModeResolver, + public SoapHeaderElementProcessorExtractEbms3Messaging (@Nonnull final IAS4PModeResolver aPModeResolver, @Nullable final Consumer aPModeConsumer, @Nonnull final IAS4IncomingReceiverConfiguration aIRC) { @@ -345,6 +345,8 @@ public ESuccess processHeaderElement (@Nonnull final Document aSoapDoc, { // Find PMode String sPModeID = null; + final String sService = aCollaborationInfo.getService ().getValue (); + final String sAction = aCollaborationInfo.getAction (); String sAgreementRef = null; if (aCollaborationInfo.getAgreementRef () != null) { @@ -356,8 +358,8 @@ public ESuccess processHeaderElement (@Nonnull final Document aSoapDoc, final String sAddress = m_aIncomingReceiverConfiguration.getReceiverEndpointAddress (); aPMode = m_aPModeResolver.findPMode (sPModeID, - aCollaborationInfo.getService ().getValue (), - aCollaborationInfo.getAction (), + sService, + sAction, sInitiatorID, sResponderID, sAgreementRef, @@ -366,7 +368,22 @@ public ESuccess processHeaderElement (@Nonnull final Document aSoapDoc, // Should be screened by the XSD conversion already if (aPMode == null) { - final String sDetails = "Failed to resolve PMode '" + sPModeID + "' using resolver " + m_aPModeResolver; + final String sDetails = "Failed to resolve PMode '" + + sPModeID + + "' / '" + + sService + + "' / '" + + sAction + + "' / '" + + sInitiatorID + + "' / '" + + sResponderID + + "' / '" + + sAgreementRef + + "' / '" + + sAddress + + "' using resolver " + + m_aPModeResolver; LOGGER.error (sDetails); aProcessingErrorMessagesTarget.add (EEbmsError.EBMS_PROCESSING_MODE_MISMATCH.errorBuilder (aLocale) .errorDetail (sDetails) diff --git a/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorRegistry.java b/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorRegistry.java index 8cae8053a..c0deca689 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorRegistry.java +++ b/phase4-lib/src/main/java/com/helger/phase4/incoming/soap/SoapHeaderElementProcessorRegistry.java @@ -40,7 +40,7 @@ import com.helger.phase4.incoming.IAS4IncomingReceiverConfiguration; import com.helger.phase4.incoming.crypto.IAS4IncomingSecurityConfiguration; import com.helger.phase4.model.pmode.IPMode; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; /** * This class manages the SOAP header element processors. This is used to @@ -94,7 +94,7 @@ public ICommonsOrderedMap getAllElementProc } @Nonnull - public static SoapHeaderElementProcessorRegistry createDefault (@Nonnull final IPModeResolver aPModeResolver, + public static SoapHeaderElementProcessorRegistry createDefault (@Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, @Nullable final IPMode aFallbackPMode, diff --git a/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/DefaultPModeResolver.java b/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/AS4DefaultPModeResolver.java similarity index 59% rename from phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/DefaultPModeResolver.java rename to phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/AS4DefaultPModeResolver.java index ab7dac8e7..69a1e92d2 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/DefaultPModeResolver.java +++ b/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/AS4DefaultPModeResolver.java @@ -19,38 +19,39 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.helger.commons.annotation.Nonempty; import com.helger.commons.annotation.OverrideOnDemand; import com.helger.commons.string.StringHelper; import com.helger.commons.string.ToStringGenerator; import com.helger.phase4.mgr.MetaAS4Manager; -import com.helger.phase4.model.pmode.DefaultPMode; import com.helger.phase4.model.pmode.IPMode; import com.helger.phase4.model.pmode.IPModeManager; import com.helger.phase4.profile.IAS4Profile; -import com.helger.phase4.profile.IAS4ProfileManager; /** - * Default implementation of {@link IPModeResolver} using the fixed ID only. If - * no ID is provided the default PMode is used. + * Default implementation of {@link IAS4PModeResolver} based on an AS4 Profile + * ID. If no PMode is present, the respective PMode template from the selected + * AS4 profile is used instead. * * @author bayerlma * @author Philip Helger */ -public class DefaultPModeResolver implements IPModeResolver +public class AS4DefaultPModeResolver implements IAS4PModeResolver { - public static boolean DEFAULT_CREATE_DEFAULT_PMODE = false; - - public static final IPModeResolver DEFAULT_PMODE_RESOLVER = new DefaultPModeResolver (null, - DEFAULT_CREATE_DEFAULT_PMODE); + private static final Logger LOGGER = LoggerFactory.getLogger (AS4DefaultPModeResolver.class); private final String m_sAS4ProfileID; - private final boolean m_bUseDefaultAsFallback; + private final IAS4Profile m_aAS4Profile; - public DefaultPModeResolver (@Nullable final String sAS4ProfileID, final boolean bUseDefaultAsFallback) + public AS4DefaultPModeResolver (@Nullable final String sAS4ProfileID) { m_sAS4ProfileID = sAS4ProfileID; - m_bUseDefaultAsFallback = bUseDefaultAsFallback; + m_aAS4Profile = MetaAS4Manager.getProfileMgr ().getProfileOfID (sAS4ProfileID); + if (m_aAS4Profile == null && StringHelper.hasText (sAS4ProfileID)) + LOGGER.error ("Failed to resolved the AS4 profile ID '" + sAS4ProfileID + "'"); } /** @@ -65,12 +66,14 @@ public final String getAS4ProfileID () } /** - * @return true if a "dummy default PMode" should be created, if - * no other options worked. + * @return The resolved AS4 profile based on the ID provided in the + * constructor. May be null. + * @since 3.0.0 */ - public final boolean isUseDefaultAsFallback () + @Nullable + protected final IAS4Profile getAS4Profile () { - return m_bUseDefaultAsFallback; + return m_aAS4Profile; } @Nullable @@ -79,30 +82,14 @@ protected IPMode createDefaultPMode (@Nonnull @Nonempty final String sInitiatorI @Nonnull @Nonempty final String sResponderID, @Nullable final String sAddress) { - // Use default pmode based on profile - final IAS4ProfileManager aProfileMgr = MetaAS4Manager.getProfileMgr (); - IAS4Profile aProfile = null; - if (StringHelper.hasText (m_sAS4ProfileID)) - { - // Try to resolve ID from constructor - aProfile = aProfileMgr.getProfileOfID (m_sAS4ProfileID); - } - if (aProfile == null) + if (m_aAS4Profile != null) { - // ID not provided or non-existing - use default - aProfile = aProfileMgr.getDefaultProfileOrNull (); + // Create a default PMode template + return m_aAS4Profile.createPModeTemplate (sInitiatorID, sResponderID, sAddress); } - if (aProfile != null) - return aProfile.createPModeTemplate (sInitiatorID, sResponderID, sAddress); - if (!m_bUseDefaultAsFallback) - { - // Not found and no default -> null - return null; - } - - // 2. Default default PMode - return DefaultPMode.getOrCreateDefaultPMode (sInitiatorID, sResponderID, sAddress, true); + // Nothing to create + return null; } @Nullable @@ -115,6 +102,7 @@ public IPMode findPMode (@Nullable final String sPModeID, @Nullable final String sAddress) { final IPModeManager aPModeMgr = MetaAS4Manager.getPModeMgr (); + IPMode ret = null; if (StringHelper.hasText (sPModeID)) { @@ -124,12 +112,13 @@ public IPMode findPMode (@Nullable final String sPModeID, return ret; } - // the PMode id field is empty or null (or invalid) - // try a combination of service and action + // the PMode ID field is empty or null or invalid + // try a combination of Service and Action ret = aPModeMgr.getPModeOfServiceAndAction (sService, sAction); if (ret != null) return ret; + // No existing PMode was found // Try to resolve a default PMode from the other parameters return createDefaultPMode (sInitiatorID, sResponderID, sAddress); } @@ -137,8 +126,6 @@ public IPMode findPMode (@Nullable final String sPModeID, @Override public String toString () { - return new ToStringGenerator (null).append ("AS4ProfileID", m_sAS4ProfileID) - .append ("UseDefaultAsFallback", m_bUseDefaultAsFallback) - .getToString (); + return new ToStringGenerator (null).append ("AS4ProfileID", m_sAS4ProfileID).getToString (); } } diff --git a/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IPModeResolver.java b/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IAS4PModeResolver.java similarity index 87% rename from phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IPModeResolver.java rename to phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IAS4PModeResolver.java index df3bb723d..5f7a30727 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IPModeResolver.java +++ b/phase4-lib/src/main/java/com/helger/phase4/model/pmode/resolve/IAS4PModeResolver.java @@ -28,7 +28,7 @@ * @author Philip Helger */ @FunctionalInterface -public interface IPModeResolver +public interface IAS4PModeResolver { /** * Get the PMode from the passed parameters. @@ -42,14 +42,16 @@ public interface IPModeResolver * The action as specified in the EBMS CollaborationInformation. May * not be null. * @param sInitiatorID - * Initiator ID from user message + * Initiator ID from user message. May neither be null nor + * empty. * @param sResponderID - * Responder ID from user message + * Responder ID from user message. May neither be null nor + * empty. * @param sAgreementRef * The agreement reference from the user message. May be * null. * @param sAddress - * Endpoint address + * Endpoint address. May be null. * @return null if resolution failed. */ @Nullable diff --git a/phase4-lib/src/main/java/com/helger/phase4/profile/AS4ProfileManager.java b/phase4-lib/src/main/java/com/helger/phase4/profile/AS4ProfileManager.java index 25a8bd3f9..0b025db98 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/profile/AS4ProfileManager.java +++ b/phase4-lib/src/main/java/com/helger/phase4/profile/AS4ProfileManager.java @@ -31,9 +31,7 @@ import com.helger.commons.collection.impl.ICommonsList; import com.helger.commons.collection.impl.ICommonsMap; import com.helger.commons.concurrent.SimpleReadWriteLock; -import com.helger.commons.equals.EqualsHelper; import com.helger.commons.lang.ServiceLoaderHelper; -import com.helger.commons.state.EChange; import com.helger.commons.string.StringHelper; import com.helger.commons.string.ToStringGenerator; @@ -50,15 +48,12 @@ public class AS4ProfileManager implements IAS4ProfileManager private final SimpleReadWriteLock m_aRWLock = new SimpleReadWriteLock (); @GuardedBy ("m_aRWLock") - private final ICommonsMap m_aMap = new CommonsHashMap <> (); - @GuardedBy ("m_aRWLock") - private IAS4Profile m_aDefaultProfile; + private final ICommonsMap m_aProfiles = new CommonsHashMap <> (); private void _registerAll () { m_aRWLock.writeLocked ( () -> { - m_aMap.clear (); - m_aDefaultProfile = null; + m_aProfiles.clear (); }); for (final IAS4ProfileRegistrarSPI aSPI : ServiceLoaderHelper.getAllSPIImplementations (IAS4ProfileRegistrarSPI.class)) aSPI.registerAS4Profile (this); @@ -79,13 +74,13 @@ public AS4ProfileManager () @ReturnsMutableCopy public ICommonsList getAllProfiles () { - return m_aRWLock.readLockedGet (m_aMap::copyOfValues); + return m_aRWLock.readLockedGet (m_aProfiles::copyOfValues); } @Nonnegative public final int getProfileCount () { - return m_aRWLock.readLockedInt (m_aMap::size); + return m_aRWLock.readLockedInt (m_aProfiles::size); } @Nullable @@ -94,7 +89,7 @@ public IAS4Profile getProfileOfID (@Nullable final String sID) if (StringHelper.hasNoText (sID)) return null; - return m_aRWLock.readLockedGet ( () -> m_aMap.get (sID)); + return m_aRWLock.readLockedGet ( () -> m_aProfiles.get (sID)); } public void registerProfile (@Nonnull final IAS4Profile aAS4Profile) @@ -103,79 +98,15 @@ public void registerProfile (@Nonnull final IAS4Profile aAS4Profile) final String sID = aAS4Profile.getID (); m_aRWLock.writeLocked ( () -> { - if (m_aMap.containsKey (sID)) + if (m_aProfiles.containsKey (sID)) throw new IllegalStateException ("An AS4 profile with ID '" + sID + "' is already registered!"); - m_aMap.put (sID, aAS4Profile); - - // Make the first the default as fallback - if (m_aMap.size () == 1) - m_aDefaultProfile = aAS4Profile; + m_aProfiles.put (sID, aAS4Profile); }); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("Registered" + (aAS4Profile.isDeprecated () ? " deprecated" : "") + " AS4 profile '" + sID + "'"); } - public boolean hasDefaultProfile () - { - return m_aRWLock.readLockedBoolean ( () -> m_aDefaultProfile != null); - } - - @Nullable - public IAS4Profile getDefaultProfileOrNull () - { - return m_aRWLock.readLockedGet ( () -> { - IAS4Profile ret = m_aDefaultProfile; - if (ret == null) - { - final int nCount = m_aMap.size (); - if (nCount == 1) - ret = m_aMap.getFirstValue (); - } - return ret; - }); - } - - @Nonnull - public IAS4Profile getDefaultProfile () - { - return m_aRWLock.readLockedGet ( () -> { - IAS4Profile ret = m_aDefaultProfile; - if (ret == null) - { - final int nCount = m_aMap.size (); - if (nCount == 1) - ret = m_aMap.getFirstValue (); - else - { - if (nCount == 0) - throw new IllegalStateException ("No AS4 profile is present, so no default profile can be determined!"); - throw new IllegalStateException (nCount + " AS4 profiles are present, but none is declared default!"); - } - } - return ret; - }); - } - - public void setDefaultProfile (@Nullable final IAS4Profile aAS4Profile) - { - final EChange eChanged = m_aRWLock.writeLockedGet ( () -> { - if (EqualsHelper.equals (aAS4Profile, m_aDefaultProfile)) - return EChange.UNCHANGED; - m_aDefaultProfile = aAS4Profile; - return EChange.CHANGED; - }); - - if (eChanged.isChanged ()) - if (aAS4Profile == null) - LOGGER.info ("Removed the default AS4 profile"); - else - LOGGER.info ("Set the default AS4 profile to '" + - aAS4Profile.getID () + - "'" + - (aAS4Profile.isDeprecated () ? " which is deprecated" : "")); - } - public void reloadAll () { _registerAll (); @@ -184,8 +115,6 @@ public void reloadAll () @Override public String toString () { - return new ToStringGenerator (this).append ("Map", m_aMap) - .append ("DefaultProfile", m_aDefaultProfile) - .getToString (); + return new ToStringGenerator (this).append ("Profiles", m_aProfiles).getToString (); } } diff --git a/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileManager.java b/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileManager.java index 50e16b778..26069eb2c 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileManager.java +++ b/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileManager.java @@ -47,44 +47,4 @@ public interface IAS4ProfileManager extends IAS4ProfileRegistrar */ @Nullable IAS4Profile getProfileOfID (@Nullable String sID); - - /** - * @return true if an explicit default profile is present, - * false if not. - */ - boolean hasDefaultProfile (); - - /** - * @return The default profile. If none is set, and exactly one profile is - * present, it is used. null if no default is present and - * more than one profile is registered - */ - @Nullable - IAS4Profile getDefaultProfileOrNull (); - - /** - * @return The default profile. If none is set, and exactly one profile is - * present, it is used. If no default profile is present and more than - * one profile is present an Exception is thrown. - * @throws IllegalStateException - * If no default is present and more than one profile is registered - */ - @Nonnull - IAS4Profile getDefaultProfile (); - - /** - * Set the default profile to be used. - * - * @param sDefaultProfileID - * The ID of the default profile. May be null. - * @return null if no such profile is registered, the resolve - * profile otherwise. - */ - @Nullable - default IAS4Profile setDefaultProfileID (@Nullable final String sDefaultProfileID) - { - final IAS4Profile aDefault = getProfileOfID (sDefaultProfileID); - setDefaultProfile (aDefault); - return aDefault; - } } diff --git a/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileRegistrar.java b/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileRegistrar.java index c03e6873e..d6c7556d9 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileRegistrar.java +++ b/phase4-lib/src/main/java/com/helger/phase4/profile/IAS4ProfileRegistrar.java @@ -17,7 +17,6 @@ package com.helger.phase4.profile; import javax.annotation.Nonnull; -import javax.annotation.Nullable; /** * Base interface for AS4 profile registrar @@ -33,12 +32,4 @@ public interface IAS4ProfileRegistrar * The AS4 profile to be registered. May not be null. */ void registerProfile (@Nonnull IAS4Profile aAS4Profile); - - /** - * Set the provided AS4 profile as the default - * - * @param aAS4Profile - * The default AS4 profile to be used. May be null. - */ - void setDefaultProfile (@Nullable IAS4Profile aAS4Profile); } diff --git a/phase4-lib/src/main/java/com/helger/phase4/sender/AS4BidirectionalClientHelper.java b/phase4-lib/src/main/java/com/helger/phase4/sender/AS4BidirectionalClientHelper.java index 9b0afd684..f8c27da9c 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/sender/AS4BidirectionalClientHelper.java +++ b/phase4-lib/src/main/java/com/helger/phase4/sender/AS4BidirectionalClientHelper.java @@ -52,7 +52,7 @@ import com.helger.phase4.incoming.IAS4UserMessageConsumer; import com.helger.phase4.incoming.crypto.IAS4IncomingSecurityConfiguration; import com.helger.phase4.model.pmode.IPMode; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; import com.helger.phase4.util.Phase4Exception; import jakarta.mail.MessagingException; @@ -71,7 +71,7 @@ private AS4BidirectionalClientHelper () public static void sendAS4UserMessageAndReceiveAS4SignalMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aIncomingProfileSelector, @Nonnull final AS4ClientUserMessage aClientUserMsg, @@ -187,7 +187,7 @@ public static void sendAS4UserMessageAndReceiveAS4SignalMessage (@Nonnull final public static void sendAS4PullRequestAndReceiveAS4UserMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aIncomingProfileSelector, @Nonnull final AS4ClientPullRequestMessage aClientPullRequest, @@ -273,7 +273,7 @@ public static void sendAS4PullRequestAndReceiveAS4UserMessage (@Nonnull final IA public static void sendAS4PullRequestAndReceiveAS4UserOrSignalMessage (@Nonnull final IAS4CryptoFactory aCryptoFactorySign, @Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt, - @Nonnull final IPModeResolver aPModeResolver, + @Nonnull final IAS4PModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aIncomingProfileSelector, @Nonnull final AS4ClientPullRequestMessage aClientPullRequest, diff --git a/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4MessageBuilder.java b/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4MessageBuilder.java index 3250b3505..f80456d71 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4MessageBuilder.java +++ b/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4MessageBuilder.java @@ -51,8 +51,8 @@ import com.helger.phase4.messaging.http.HttpRetrySettings; import com.helger.phase4.messaging.http.IHttpPoster; import com.helger.phase4.model.ESoapVersion; -import com.helger.phase4.model.pmode.resolve.DefaultPModeResolver; -import com.helger.phase4.model.pmode.resolve.IPModeResolver; +import com.helger.phase4.model.pmode.resolve.AS4DefaultPModeResolver; +import com.helger.phase4.model.pmode.resolve.IAS4PModeResolver; import com.helger.phase4.util.Phase4Exception; /** @@ -85,7 +85,7 @@ public abstract class AbstractAS4MessageBuilder * {@link #cryptoFactory(IAS4CryptoFactory)}
* {@link #soapVersion(ESoapVersion)} - * {@link #pmodeResolver(IPModeResolver)}
* {@link #incomingAttachmentFactory(IAS4IncomingAttachmentFactory)}
* {@link #incomingProfileSelector(IAS4IncomingProfileSelector)}
*/ @@ -116,7 +115,6 @@ protected AbstractAS4MessageBuilder () // By default set the same for sign and crypt cryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstance ()); soapVersion (ESoapVersion.SOAP_12); - pmodeResolver (DefaultPModeResolver.DEFAULT_PMODE_RESOLVER); incomingAttachmentFactory (IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE); incomingProfileSelector (AS4IncomingProfileSelectorFromGlobal.INSTANCE); } @@ -508,8 +506,7 @@ public final IMPLTYPE locale (@Nullable final Locale a) } /** - * @return The selected AS4 profile to use. May be null to - * indicate the default profile. + * @return The selected AS4 profile to use. May be null. * @since 2.8.2 */ @Nullable @@ -519,30 +516,26 @@ public final String as4ProfileID () } /** - * Set the AS4 profile to be used. + * Set the AS4 profile to be used. Must be provided for the builder to work. * * @param sAS4ProfileID - * The AS4 profile to be used. May be null to indicate the - * usage of the default profile. This internally changes the PMode - * resolver! + * The AS4 profile ID to be used. * @return this for chaining - * @see #pmodeResolver(IPModeResolver) * @since 2.8.2 */ @Nonnull public final IMPLTYPE as4ProfileID (@Nullable final String sAS4ProfileID) { m_sAS4ProfileID = sAS4ProfileID; - return pmodeResolver (StringHelper.hasNoText (sAS4ProfileID) ? DefaultPModeResolver.DEFAULT_PMODE_RESOLVER - : new DefaultPModeResolver (sAS4ProfileID, false)); + return thisAsT (); } /** - * @return The currently set {@link IPModeResolver}. May not be + * @return The currently set {@link IAS4PModeResolver}. May be * null. */ - @Nonnull - public final IPModeResolver pmodeResolver () + @Nullable + public final IAS4PModeResolver pmodeResolver () { return m_aPModeResolver; } @@ -552,13 +545,12 @@ public final IPModeResolver pmodeResolver () * of an eventually received synchronous response message. * * @param aPModeResolver - * The PMode resolver to be used. May not be null. + * The PMode resolver to be used. May be null. * @return this for chaining */ @Nonnull - public final IMPLTYPE pmodeResolver (@Nonnull final IPModeResolver aPModeResolver) + public final IMPLTYPE pmodeResolver (@Nullable final IAS4PModeResolver aPModeResolver) { - ValueEnforcer.notNull (aPModeResolver, "PModeResolver"); m_aPModeResolver = aPModeResolver; return thisAsT (); } @@ -849,6 +841,9 @@ public final IMPLTYPE rawResponseConsumer (@Nullable final IAS4RawResponseConsum @OverridingMethodsMustInvokeSuper protected ESuccess finishFields () throws Phase4Exception { + if (m_aPModeResolver == null && StringHelper.hasText (m_sAS4ProfileID)) + pmodeResolver (new AS4DefaultPModeResolver (m_sAS4ProfileID)); + return ESuccess.SUCCESS; } @@ -883,7 +878,11 @@ public boolean isEveryRequiredFieldSet () LOGGER.warn ("The field 'locale' is not set"); return false; } - // m_sAS4ProfileID may be null + if (StringHelper.hasNoText (m_sAS4ProfileID)) + { + LOGGER.warn ("The field 'as4ProfileID' is not set"); + return false; + } if (m_aPModeResolver == null) { diff --git a/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4PullRequestBuilder.java b/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4PullRequestBuilder.java index c723c0578..e1f9da932 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4PullRequestBuilder.java +++ b/phase4-lib/src/main/java/com/helger/phase4/sender/AbstractAS4PullRequestBuilder.java @@ -53,6 +53,7 @@ public abstract class AbstractAS4PullRequestBuilder null. + * @see #pmodeID(String) + * @since 3.0.0 + */ + @Nullable + public final String pmodeID () + { + return m_sPModeID; + } + + /** + * Set the optional PMode ID for packaging in the pull request. + * + * @param s + * PMode ID. May be null. + * @return this for chaining + * @since 3.0.0 + */ + @Nonnull + public final IMPLTYPE pmodeID (@Nullable final String s) + { + m_sPModeID = s; + return thisAsT (); + } + /** * @return true if PMode leg 1 is used, false if leg * 2 is used. @@ -231,7 +258,7 @@ protected ESuccess finishFields () throws Phase4Exception if (m_aPMode == null && pmodeResolver () != null) { // Create a default PMode template - m_aPMode = pmodeResolver ().findPMode (null, "s", "a", "i", "r", "a", null); + m_aPMode = pmodeResolver ().findPMode (m_sPModeID, "s", "a", "i", "r", "a", null); if (m_aPMode == null) LOGGER.warn ("No PMode was provided, and the PMode Resolver delivered a null-PMode as well"); } diff --git a/phase4-lib/src/main/java/com/helger/phase4/servlet/AS4XServletHandler.java b/phase4-lib/src/main/java/com/helger/phase4/servlet/AS4XServletHandler.java index 0a775f5d7..83fb3484d 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/servlet/AS4XServletHandler.java +++ b/phase4-lib/src/main/java/com/helger/phase4/servlet/AS4XServletHandler.java @@ -40,8 +40,9 @@ import com.helger.phase4.incoming.IAS4IncomingMessageMetadata; import com.helger.phase4.incoming.IAS4ResponseAbstraction; import com.helger.phase4.incoming.crypto.AS4IncomingSecurityConfiguration; +import com.helger.phase4.incoming.mgr.AS4ProfileSelector; import com.helger.phase4.messaging.http.AS4HttpDebug; -import com.helger.phase4.model.pmode.resolve.DefaultPModeResolver; +import com.helger.phase4.model.pmode.resolve.AS4DefaultPModeResolver; import com.helger.phase4.util.Phase4Exception; import com.helger.servlet.response.UnifiedResponse; import com.helger.web.scope.IRequestWebScope; @@ -181,8 +182,7 @@ public void setStatus (final int nStatusCode) * Handle an incoming request. Compared to * {@link #handleRequest(IRequestWebScopeWithoutResponse, UnifiedResponse)} * all the member variables are resolved into parameters to make overriding - * simpler and also avoid the risk of race conditions on members that use the - * Supplier pattern. + * simpler. * * @param aRequestScope * The request scope. May not be null. @@ -207,7 +207,8 @@ protected void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRe { // Set default values in handler aHandler.setCryptoFactory (AS4CryptoFactoryConfiguration.getDefaultInstance ()); - aHandler.setPModeResolver (DefaultPModeResolver.DEFAULT_PMODE_RESOLVER); + // No specific AS4 profile is available here + aHandler.setPModeResolver (new AS4DefaultPModeResolver (AS4ProfileSelector.getDefaultAS4ProfileID ())); aHandler.setIncomingAttachmentFactory (IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE); aHandler.setIncomingSecurityConfiguration (AS4IncomingSecurityConfiguration.createDefaultInstance ()); aHandler.setIncomingReceiverConfiguration (new AS4IncomingReceiverConfiguration ()); diff --git a/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java b/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java index 7a32fc4fa..4f04c2ca0 100644 --- a/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java +++ b/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java @@ -63,7 +63,8 @@ public static void main (final String [] args) throws Exception if (aBytes == null) throw new IllegalStateException ("Failed to read file content as byte array"); - AS4DumpReader.decryptAS4In (aBytes, + AS4DumpReader.decryptAS4In ("as4-profileid", + aBytes, aCryptoFactory, aCryptoFactory, null, diff --git a/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/supplementary/tools/MainDecipherAS4In.java b/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/supplementary/tools/MainDecipherAS4In.java index a632d6c17..453c0f159 100644 --- a/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/supplementary/tools/MainDecipherAS4In.java +++ b/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/supplementary/tools/MainDecipherAS4In.java @@ -26,6 +26,7 @@ import com.helger.phase4.crypto.IAS4CryptoFactory; import com.helger.phase4.dump.AS4DumpReader; import com.helger.phase4.dump.AS4DumpReader.IDecryptedPayloadConsumer; +import com.helger.phase4.profile.peppol.AS4PeppolProfileRegistarSPI; /** * This is a small tool that demonstrates how the "as4in" files can be decrypted @@ -60,6 +61,11 @@ public static void main (final String [] args) throws Exception aDecryptedBytes); // Do it - AS4DumpReader.decryptAS4In (aBytes, aCryptoFactory, aCryptoFactory, null, aDecryptedConsumer); + AS4DumpReader.decryptAS4In (AS4PeppolProfileRegistarSPI.AS4_PROFILE_ID, + aBytes, + aCryptoFactory, + aCryptoFactory, + null, + aDecryptedConsumer); } } diff --git a/phase4-peppol-server-webapp/src/main/java/com/helger/phase4/peppol/server/servlet/Phase4PeppolWebAppListener.java b/phase4-peppol-server-webapp/src/main/java/com/helger/phase4/peppol/server/servlet/Phase4PeppolWebAppListener.java index 22d875216..33c5fef7e 100644 --- a/phase4-peppol-server-webapp/src/main/java/com/helger/phase4/peppol/server/servlet/Phase4PeppolWebAppListener.java +++ b/phase4-peppol-server-webapp/src/main/java/com/helger/phase4/peppol/server/servlet/Phase4PeppolWebAppListener.java @@ -168,7 +168,7 @@ protected void initSecurity () private static void _initAS4 () { // Enforce Peppol profile usage - AS4ProfileSelector.setCustomAS4ProfileID (AS4PeppolProfileRegistarSPI.AS4_PROFILE_ID); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (AS4PeppolProfileRegistarSPI.AS4_PROFILE_ID); // Start duplicate check AS4ServerInitializer.initAS4Server (); diff --git a/phase4-profile-bdew/src/main/java/com/helger/phase4/profile/bdew/AS4BDEWProfileRegistarSPI.java b/phase4-profile-bdew/src/main/java/com/helger/phase4/profile/bdew/AS4BDEWProfileRegistarSPI.java index 47a3ae3e7..33d5eda25 100644 --- a/phase4-profile-bdew/src/main/java/com/helger/phase4/profile/bdew/AS4BDEWProfileRegistarSPI.java +++ b/phase4-profile-bdew/src/main/java/com/helger/phase4/profile/bdew/AS4BDEWProfileRegistarSPI.java @@ -77,7 +77,6 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, true); aRegistrar.registerProfile (aProfile); - aRegistrar.setDefaultProfile (aProfile); } } diff --git a/phase4-profile-cef/src/main/java/com/helger/phase4/profile/cef/AS4CEFProfileRegistarSPI.java b/phase4-profile-cef/src/main/java/com/helger/phase4/profile/cef/AS4CEFProfileRegistarSPI.java index 53ad22879..9237157c3 100644 --- a/phase4-profile-cef/src/main/java/com/helger/phase4/profile/cef/AS4CEFProfileRegistarSPI.java +++ b/phase4-profile-cef/src/main/java/com/helger/phase4/profile/cef/AS4CEFProfileRegistarSPI.java @@ -75,8 +75,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aProfileTwoCorner); - - // The four corner profile is the default - aRegistrar.setDefaultProfile (aProfileFourCorner); } } diff --git a/phase4-profile-dbnalliance/src/main/java/com/helger/phase4/profile/dbnalliance/AS4DBNAllianceProfileRegistarSPI.java b/phase4-profile-dbnalliance/src/main/java/com/helger/phase4/profile/dbnalliance/AS4DBNAllianceProfileRegistarSPI.java index 9e6fc0503..16e943e44 100644 --- a/phase4-profile-dbnalliance/src/main/java/com/helger/phase4/profile/dbnalliance/AS4DBNAllianceProfileRegistarSPI.java +++ b/phase4-profile-dbnalliance/src/main/java/com/helger/phase4/profile/dbnalliance/AS4DBNAllianceProfileRegistarSPI.java @@ -63,6 +63,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aProfile); - aRegistrar.setDefaultProfile (aProfile); } } diff --git a/phase4-profile-eespa/src/main/java/com/helger/phase4/profile/eespa/AS4EESPAProfileRegistarSPI.java b/phase4-profile-eespa/src/main/java/com/helger/phase4/profile/eespa/AS4EESPAProfileRegistarSPI.java index 26f871430..b437a0d2a 100644 --- a/phase4-profile-eespa/src/main/java/com/helger/phase4/profile/eespa/AS4EESPAProfileRegistarSPI.java +++ b/phase4-profile-eespa/src/main/java/com/helger/phase4/profile/eespa/AS4EESPAProfileRegistarSPI.java @@ -49,18 +49,22 @@ public final class AS4EESPAProfileRegistarSPI implements IAS4ProfileRegistrarSPI public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) { - final IAS4ProfilePModeProvider aDefaultPModeProviderAcc = (i, r, a) -> EESPAPMode.createEESPAPMode (i, - r, - a, - PMODE_ID_PROVIDER, - true, - false); - final IAS4ProfilePModeProvider aDefaultPModeProviderProd = (i, r, a) -> EESPAPMode.createEESPAPMode (i, - r, - a, - PMODE_ID_PROVIDER, - false, - false); + final IAS4ProfilePModeProvider aDefaultPModeProviderAcc = (i, + r, + a) -> EESPAPMode.createEESPAPMode (i, + r, + a, + PMODE_ID_PROVIDER, + true, + false); + final IAS4ProfilePModeProvider aDefaultPModeProviderProd = (i, + r, + a) -> EESPAPMode.createEESPAPMode (i, + r, + a, + PMODE_ID_PROVIDER, + false, + false); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("Registering phase4 profile '" + AS4_PROFILE_ID_ACCEPTANCE + "'"); @@ -83,8 +87,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aProfileProd); - - // Default to acc - aRegistrar.setDefaultProfile (aProfileAcc); } } diff --git a/phase4-profile-entsog/src/main/java/com/helger/phase4/profile/entsog/AS4ENTSOGProfileRegistarSPI.java b/phase4-profile-entsog/src/main/java/com/helger/phase4/profile/entsog/AS4ENTSOGProfileRegistarSPI.java index cc4b351fc..9e35c3a74 100644 --- a/phase4-profile-entsog/src/main/java/com/helger/phase4/profile/entsog/AS4ENTSOGProfileRegistarSPI.java +++ b/phase4-profile-entsog/src/main/java/com/helger/phase4/profile/entsog/AS4ENTSOGProfileRegistarSPI.java @@ -65,6 +65,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aProfile); - aRegistrar.setDefaultProfile (aProfile); } } diff --git a/phase4-profile-euctp/src/main/java/com/helger/phase4/profile/euctp/AS4EuCtpProfileRegistarSPI.java b/phase4-profile-euctp/src/main/java/com/helger/phase4/profile/euctp/AS4EuCtpProfileRegistarSPI.java index 8d09f35e7..13efd3997 100644 --- a/phase4-profile-euctp/src/main/java/com/helger/phase4/profile/euctp/AS4EuCtpProfileRegistarSPI.java +++ b/phase4-profile-euctp/src/main/java/com/helger/phase4/profile/euctp/AS4EuCtpProfileRegistarSPI.java @@ -65,7 +65,6 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aPushProfile); - aRegistrar.setDefaultProfile (aPushProfile); // pull final IAS4ProfilePModeProvider aPullPModeProvider = (i, r, a) -> EuCtpPMode.createEuCtpPullPMode (i, diff --git a/phase4-profile-peppol/src/main/java/com/helger/phase4/profile/peppol/AS4PeppolProfileRegistarSPI.java b/phase4-profile-peppol/src/main/java/com/helger/phase4/profile/peppol/AS4PeppolProfileRegistarSPI.java index 25c3b2b14..a557bb4e5 100644 --- a/phase4-profile-peppol/src/main/java/com/helger/phase4/profile/peppol/AS4PeppolProfileRegistarSPI.java +++ b/phase4-profile-peppol/src/main/java/com/helger/phase4/profile/peppol/AS4PeppolProfileRegistarSPI.java @@ -62,6 +62,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aProfile); - aRegistrar.setDefaultProfile (aProfile); } } diff --git a/phase4-spring-boot-demo/src/main/java/com/helger/phase4/springboot/servlet/ServletConfig.java b/phase4-spring-boot-demo/src/main/java/com/helger/phase4/springboot/servlet/ServletConfig.java index bf1fb8a96..286be15b8 100644 --- a/phase4-spring-boot-demo/src/main/java/com/helger/phase4/springboot/servlet/ServletConfig.java +++ b/phase4-spring-boot-demo/src/main/java/com/helger/phase4/springboot/servlet/ServletConfig.java @@ -226,7 +226,7 @@ private static void _initGlobalSettings (@Nonnull final ServletContext aSC) private static void _initAS4 () { // Enforce Peppol profile usage - AS4ProfileSelector.setCustomAS4ProfileID (AS4PeppolProfileRegistarSPI.AS4_PROFILE_ID); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (AS4PeppolProfileRegistarSPI.AS4_PROFILE_ID); AS4ServerInitializer.initAS4Server (); } diff --git a/phase4-test/src/main/java/com/helger/phase4/test/profile/AS4TestProfileRegistarSPI.java b/phase4-test/src/main/java/com/helger/phase4/test/profile/AS4TestProfileRegistarSPI.java index 2d763f589..79398e97b 100644 --- a/phase4-test/src/main/java/com/helger/phase4/test/profile/AS4TestProfileRegistarSPI.java +++ b/phase4-test/src/main/java/com/helger/phase4/test/profile/AS4TestProfileRegistarSPI.java @@ -40,7 +40,7 @@ @IsSPIImplementation public final class AS4TestProfileRegistarSPI implements IAS4ProfileRegistrarSPI { - public static final String AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT = "test"; + public static final String AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT = "phase4-unitest"; private static final Logger LOGGER = LoggerFactory.getLogger (AS4TestProfileRegistarSPI.class); @@ -62,7 +62,5 @@ public void registerAS4Profile (@Nonnull final IAS4ProfileRegistrar aRegistrar) false, false); aRegistrar.registerProfile (aTestProfile); - - aRegistrar.setDefaultProfile (aTestProfile); } } diff --git a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java index fd759eb81..0e63cec11 100644 --- a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java +++ b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java @@ -47,8 +47,8 @@ import com.helger.phase4.crypto.ECryptoAlgorithmSign; import com.helger.phase4.crypto.ECryptoAlgorithmSignDigest; import com.helger.phase4.dump.IAS4OutgoingDumper; +import com.helger.phase4.incoming.mgr.AS4ProfileSelector; import com.helger.phase4.messaging.http.AS4HttpDebug; -import com.helger.phase4.mgr.MetaAS4Manager; import com.helger.phase4.model.ESoapVersion; import com.helger.phase4.model.message.MessageHelperMethods; import com.helger.phase4.server.AbstractAS4TestSetUp; @@ -83,12 +83,13 @@ public static void beforeClass () throws Exception { MockJettySetup.startServer (); s_aResMgr = MockJettySetup.getResourceManagerInstance (); - MetaAS4Manager.getProfileMgr ().setDefaultProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); } @AfterClass public static void afterClass () throws Exception { + AS4ProfileSelector.setCustomDefaultAS4ProfileID (null); s_aResMgr = null; MockJettySetup.shutDownServer (); } diff --git a/phase4-test/src/test/java/com/helger/phase4/server/message/AbstractUserMessageTestSetUp.java b/phase4-test/src/test/java/com/helger/phase4/server/message/AbstractUserMessageTestSetUp.java index 61f02474b..8d470f451 100644 --- a/phase4-test/src/test/java/com/helger/phase4/server/message/AbstractUserMessageTestSetUp.java +++ b/phase4-test/src/test/java/com/helger/phase4/server/message/AbstractUserMessageTestSetUp.java @@ -50,10 +50,10 @@ import com.helger.phase4.crypto.AS4CryptParams; import com.helger.phase4.crypto.AS4CryptoFactoryConfiguration; import com.helger.phase4.crypto.IAS4CryptoFactory; +import com.helger.phase4.incoming.mgr.AS4ProfileSelector; import com.helger.phase4.messaging.http.AS4HttpDebug; import com.helger.phase4.messaging.http.HttpMimeMessageEntity; import com.helger.phase4.messaging.mime.AS4MimeMessageHelper; -import com.helger.phase4.mgr.MetaAS4Manager; import com.helger.phase4.server.AbstractAS4TestSetUp; import com.helger.phase4.server.MockJettySetup; import com.helger.phase4.test.profile.AS4TestProfileRegistarSPI; @@ -92,12 +92,13 @@ public static void startServer () throws Exception { MockJettySetup.startServer (); s_aResMgr = MockJettySetup.getResourceManagerInstance (); - MetaAS4Manager.getProfileMgr ().setDefaultProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); } @AfterClass public static void shutDownServer () throws Exception { + AS4ProfileSelector.setCustomDefaultAS4ProfileID (null); // s_aResMgr is closed by MockJettySetup s_aResMgr = null; MockJettySetup.shutDownServer (); diff --git a/phase4-test/src/test/java/com/helger/phase4/server/servlet/CEFProfileTest.java b/phase4-test/src/test/java/com/helger/phase4/server/servlet/CEFProfileTest.java index ad674e1de..1cfb8f58d 100644 --- a/phase4-test/src/test/java/com/helger/phase4/server/servlet/CEFProfileTest.java +++ b/phase4-test/src/test/java/com/helger/phase4/server/servlet/CEFProfileTest.java @@ -93,14 +93,14 @@ public void before () // Default MessageProperties for testing m_aEbms3UserMessage.setMessageProperties (createDefaultProperties ()); - AS4ProfileSelector.setCustomAS4ProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (AS4TestProfileRegistarSPI.AS4_PROFILE_ID_MAY_SIGN_MAY_CRYPT); } @After public void after () { // Reset to default - AS4ProfileSelector.setCustomAS4ProfileID (null); + AS4ProfileSelector.setCustomDefaultAS4ProfileID (null); } @Test