Skip to content

Commit

Permalink
Added customizable SBDH identifier factory
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 5, 2024
1 parent 264ecf5 commit 52fff60
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.helger.commons.ValueEnforcer;
import com.helger.commons.string.StringHelper;
import com.helger.peppol.sbdh.read.PeppolSBDHDocumentReader;
import com.helger.peppolid.factory.IIdentifierFactory;
import com.helger.peppolid.factory.SimpleIdentifierFactory;
import com.helger.phase4.CAS4;
import com.helger.phase4.peppol.servlet.Phase4PeppolReceiverConfiguration.Phase4PeppolReceiverConfigurationBuilder;
import com.helger.smpclient.peppol.ISMPExtendedServiceMetadataProvider;
Expand All @@ -50,6 +52,7 @@
@NotThreadSafe
public final class Phase4PeppolDefaultReceiverConfiguration
{
public static final IIdentifierFactory DEFAULT_SBDH_IDENTIFIER_FACTORY = SimpleIdentifierFactory.INSTANCE;
public static final boolean DEFAULT_RECEIVER_CHECK_ENABLED = true;
@Pfuoi420
public static final EMode DEFAULT_WILDCARD_SELECTION_MODE = EMode.WILDCARD_ONLY;
Expand All @@ -63,6 +66,7 @@ public final class Phase4PeppolDefaultReceiverConfiguration
private static PeppolWildcardSelector.EMode s_eWildcardSelectionMode = DEFAULT_WILDCARD_SELECTION_MODE;
private static String s_sAS4EndpointURL;
private static X509Certificate s_aAPCertificate;
private static IIdentifierFactory s_aSBDHIdentifierFactory = DEFAULT_SBDH_IDENTIFIER_FACTORY;
private static boolean s_bPerformSBDHValueChecks = PeppolSBDHDocumentReader.DEFAULT_PERFORM_VALUE_CHECKS;
private static boolean s_bCheckSBDHForMandatoryCountryC1 = PeppolSBDHDocumentReader.DEFAULT_CHECK_FOR_COUNTRY_C1;
private static boolean s_bCheckSigningCertificateRevocation = DEFAULT_CHECK_SIGNING_CERTIFICATE_REVOCATION;
Expand Down Expand Up @@ -92,7 +96,6 @@ public static boolean isReceiverCheckEnabled ()
* @see #setAS4EndpointURL(String)
* @see #setAPCertificate(X509Certificate)
*/
@SuppressWarnings ("javadoc")
public static void setReceiverCheckEnabled (final boolean bReceiverCheckEnabled)
{
s_bReceiverCheckEnabled = bReceiverCheckEnabled;
Expand Down Expand Up @@ -197,6 +200,30 @@ public static void setAPCertificate (@Nullable final X509Certificate aAPCertific
s_aAPCertificate = aAPCertificate;
}

/**
* @return The default identifier factory used to parse SBDH data. Never
* <code>null</code>.
* @since 3.0.1
*/
@Nonnull
public static IIdentifierFactory getSBDHIdentifierFactory ()
{
return s_aSBDHIdentifierFactory;
}

/**
* Set the default identifier factory used to parse SBDH data.
*
* @param a
* The identifier factory to use. May not be <code>null</code>.
* @since 3.0.1
*/
public static void setSBDHIdentifierFactory (@Nonnull final IIdentifierFactory a)
{
ValueEnforcer.notNull (a, "SBDHIdentifierFactory");
s_aSBDHIdentifierFactory = a;
}

/**
* @return <code>true</code> if SBDH value checks are enabled,
* <code>false</code> if they are disabled.
Expand Down Expand Up @@ -314,6 +341,7 @@ public static Phase4PeppolReceiverConfigurationBuilder getAsReceiverCheckDataBui
.wildcardSelectionMode (getWildcardSelectionMode ())
.as4EndpointUrl (sAS4EndpointURL)
.apCertificate (aAPCertificate)
.sbdhIdentifierFactory (getSBDHIdentifierFactory ())
.performSBDHValueChecks (isPerformSBDHValueChecks ())
.checkSBDHForMandatoryCountryC1 (isCheckSBDHForMandatoryCountryC1 ())
.checkSigningCertificateRevocation (isCheckSigningCertificateRevocation ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.helger.commons.builder.IBuilder;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.ToStringGenerator;
import com.helger.peppolid.factory.IIdentifierFactory;
import com.helger.peppolid.factory.PeppolIdentifierFactory;
import com.helger.peppolid.factory.SimpleIdentifierFactory;
import com.helger.smpclient.peppol.ISMPExtendedServiceMetadataProvider;
import com.helger.smpclient.peppol.PeppolWildcardSelector;
import com.helger.smpclient.peppol.Pfuoi420;
Expand All @@ -47,6 +50,7 @@ public final class Phase4PeppolReceiverConfiguration
private final PeppolWildcardSelector.EMode m_eWildcardSelectionMode;
private final String m_sAS4EndpointURL;
private final X509Certificate m_aAPCertificate;
private final IIdentifierFactory m_aSBDHIdentifierFactory;
private final boolean m_bPerformSBDHValueChecks;
private final boolean m_bCheckSBDHForMandatoryCountryC1;
private final boolean m_bCheckSigningCertificateRevocation;
Expand All @@ -69,6 +73,9 @@ public final class Phase4PeppolReceiverConfiguration
* @param aAPCertificate
* The AP certificate to be used for compatibility. May not be
* <code>null</code> if receiver checks are enabled.
* @param aSBDHIdentifierFactory
* The identifier factory to be used for SBDH parsing. May not be
* <code>null</code>.
* @param bPerformSBDHValueChecks
* <code>true</code> if SBDH value checks should be performed.
* @param bCheckSBDHForMandatoryCountryC1
Expand All @@ -84,6 +91,7 @@ public Phase4PeppolReceiverConfiguration (final boolean bReceiverCheckEnabled,
@Nonnull final PeppolWildcardSelector.EMode eWildcardSelectionMode,
@Nullable final String sAS4EndpointURL,
@Nullable final X509Certificate aAPCertificate,
@Nonnull final IIdentifierFactory aSBDHIdentifierFactory,
final boolean bPerformSBDHValueChecks,
final boolean bCheckSBDHForMandatoryCountryC1,
final boolean bCheckSigningCertificateRevocation)
Expand All @@ -95,11 +103,13 @@ public Phase4PeppolReceiverConfiguration (final boolean bReceiverCheckEnabled,
ValueEnforcer.notEmpty (sAS4EndpointURL, "AS4EndpointURL");
if (bReceiverCheckEnabled)
ValueEnforcer.notNull (aAPCertificate, "APCertificate");
ValueEnforcer.notNull (aSBDHIdentifierFactory, "SBDHIdentifierFactory");
m_bReceiverCheckEnabled = bReceiverCheckEnabled;
m_aSMPClient = aSMPClient;
m_eWildcardSelectionMode = eWildcardSelectionMode;
m_sAS4EndpointURL = sAS4EndpointURL;
m_aAPCertificate = aAPCertificate;
m_aSBDHIdentifierFactory = aSBDHIdentifierFactory;
m_bPerformSBDHValueChecks = bPerformSBDHValueChecks;
m_bCheckSBDHForMandatoryCountryC1 = bCheckSBDHForMandatoryCountryC1;
m_bCheckSigningCertificateRevocation = bCheckSigningCertificateRevocation;
Expand Down Expand Up @@ -158,6 +168,16 @@ public X509Certificate getAPCertificate ()
return m_aAPCertificate;
}

/**
* @return The identifier factory to be used for SBDH parsing.
* @since 3.0.1
*/
@Nonnull
public IIdentifierFactory getSBDHIdentifierFactory ()
{
return m_aSBDHIdentifierFactory;
}

public boolean isPerformSBDHValueChecks ()
{
return m_bPerformSBDHValueChecks;
Expand All @@ -181,6 +201,7 @@ public String toString ()
.append ("WildcardSelectionMode", m_eWildcardSelectionMode)
.append ("AS4EndpointURL", m_sAS4EndpointURL)
.append ("APCertificate", m_aAPCertificate)
.append ("SBDHIdentifierFactory", m_aSBDHIdentifierFactory)
.append ("PerformSBDHValueChecks", m_bPerformSBDHValueChecks)
.append ("CheckSBDHForMandatoryCountryC1", m_bCheckSBDHForMandatoryCountryC1)
.append ("CheckSigningCertificateRevocation",
Expand Down Expand Up @@ -226,6 +247,7 @@ public static class Phase4PeppolReceiverConfigurationBuilder implements IBuilder
private PeppolWildcardSelector.EMode m_eWildcardSelectionMode;
private String m_sAS4EndpointURL;
private X509Certificate m_aAPCertificate;
private IIdentifierFactory m_aSBDHIdentifierFactory;
private boolean m_bPerformSBDHValueChecks;
private boolean m_bCheckSBDHForMandatoryCountryC1;
private boolean m_bCheckSigningCertificateRevocation;
Expand All @@ -240,6 +262,7 @@ public Phase4PeppolReceiverConfigurationBuilder (@Nonnull final Phase4PeppolRece
.wildcardSelectionMode (aSrc.getWildcardSelectionMode ())
.as4EndpointUrl (aSrc.getAS4EndpointURL ())
.apCertificate (aSrc.getAPCertificate ())
.sbdhIdentifierFactory (aSrc.getSBDHIdentifierFactory ())
.performSBDHValueChecks (aSrc.isPerformSBDHValueChecks ())
.checkSBDHForMandatoryCountryC1 (aSrc.isCheckSBDHForMandatoryCountryC1 ())
.checkSigningCertificateRevocation (aSrc.isCheckSigningCertificateRevocation ());
Expand Down Expand Up @@ -281,6 +304,25 @@ public Phase4PeppolReceiverConfigurationBuilder apCertificate (@Nullable final X
return this;
}

@Nonnull
public Phase4PeppolReceiverConfigurationBuilder sbdhIdentifierFactorySimple ()
{
return sbdhIdentifierFactory (SimpleIdentifierFactory.INSTANCE);
}

@Nonnull
public Phase4PeppolReceiverConfigurationBuilder sbdhIdentifierFactoryPeppol ()
{
return sbdhIdentifierFactory (PeppolIdentifierFactory.INSTANCE);
}

@Nonnull
public Phase4PeppolReceiverConfigurationBuilder sbdhIdentifierFactory (@Nullable final IIdentifierFactory a)
{
m_aSBDHIdentifierFactory = a;
return this;
}

@Nonnull
public Phase4PeppolReceiverConfigurationBuilder performSBDHValueChecks (final boolean b)
{
Expand Down Expand Up @@ -316,12 +358,15 @@ public Phase4PeppolReceiverConfiguration build ()
}
if (m_eWildcardSelectionMode == null)
throw new IllegalStateException ("The Wildcard Selection Mode must be provided");
if (m_aSBDHIdentifierFactory == null)
throw new IllegalStateException ("The SBDH Identifier Factory must be provided");

return new Phase4PeppolReceiverConfiguration (m_bReceiverCheckEnabled,
m_aSMPClient,
m_eWildcardSelectionMode,
m_sAS4EndpointURL,
m_aAPCertificate,
m_aSBDHIdentifierFactory,
m_bPerformSBDHValueChecks,
m_bCheckSBDHForMandatoryCountryC1,
m_bCheckSigningCertificateRevocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IParticipantIdentifier;
import com.helger.peppolid.IProcessIdentifier;
import com.helger.peppolid.factory.SimpleIdentifierFactory;
import com.helger.peppolid.factory.IIdentifierFactory;
import com.helger.peppolid.peppol.PeppolIdentifierHelper;
import com.helger.phase4.attachment.AS4DecompressException;
import com.helger.phase4.attachment.EAS4CompressionMode;
Expand Down Expand Up @@ -594,6 +594,7 @@ public AS4MessageProcessorResult processAS4UserMessage (@Nonnull final IAS4Incom
.build ());
return AS4MessageProcessorResult.createFailure ();
}

if (!aState.isSoapSignatureChecked ())
{
final String sDetails = "The received Peppol message seems not to be signed (properly).";
Expand Down Expand Up @@ -758,12 +759,11 @@ public AS4MessageProcessorResult processAS4UserMessage (@Nonnull final IAS4Incom
LOGGER.debug (sLogPrefix + "Now evaluating the SBDH against Peppol rules");

// Interpret as Peppol SBDH and eventually perform consistency checks
final IIdentifierFactory aIdentifierFactory = aReceiverCheckData.getSBDHIdentifierFactory ();
final boolean bPerformValueChecks = aReceiverCheckData.isPerformSBDHValueChecks ();
final boolean bCheckForCountryC1 = aReceiverCheckData.isCheckSBDHForMandatoryCountryC1 ();
// Read with SimpleIdentifierFactory - accepts more the
// PeppolIdentifierFactory
final PeppolSBDHDocumentReader aReader = new PeppolSBDHDocumentReader (SimpleIdentifierFactory.INSTANCE).setPerformValueChecks (bPerformValueChecks)
.setCheckForCountryC1 (bCheckForCountryC1);
final PeppolSBDHDocumentReader aReader = new PeppolSBDHDocumentReader (aIdentifierFactory).setPerformValueChecks (bPerformValueChecks)
.setCheckForCountryC1 (bCheckForCountryC1);

aPeppolSBDH = aReader.extractData (aReadAttachment.standardBusinessDocument ());

Expand Down

0 comments on commit 52fff60

Please sign in to comment.