diff --git a/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolDefaultReceiverConfiguration.java b/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolDefaultReceiverConfiguration.java index 722a0ae09..19a866a66 100644 --- a/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolDefaultReceiverConfiguration.java +++ b/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolDefaultReceiverConfiguration.java @@ -29,6 +29,7 @@ import com.helger.commons.string.StringHelper; import com.helger.peppol.sbdh.read.PeppolSBDHDocumentReader; import com.helger.phase4.CAS4; +import com.helger.phase4.peppol.servlet.Phase4PeppolReceiverConfiguration.Phase4PeppolReceiverConfigurationBuilder; import com.helger.smpclient.peppol.ISMPServiceMetadataProvider; import com.helger.smpclient.peppol.PeppolWildcardSelector; import com.helger.smpclient.peppol.PeppolWildcardSelector.EMode; @@ -282,37 +283,18 @@ public static void setCheckSigningCertificateRevocation (final boolean b) /** * Get the statically configured data as a - * {@link Phase4PeppolReceiverConfiguration} instance. Returns - * null if the checks are disabled, or if at least one mandatory - * field is not set.
- * Changed to NonNull in 2.8.1 + * {@link Phase4PeppolReceiverConfigurationBuilder} instance. This allows for + * modification before building the final object. * - * @return The instance data or null. - * @since 0.9.13 - */ - @Nonnull - public static Phase4PeppolReceiverConfiguration getAsReceiverCheckData () - { - return getAsReceiverCheckData (getAPCertificate ()); - } - - /** - * Get the statically configured data as a - * {@link Phase4PeppolReceiverConfiguration} instance. Returns - * null if the checks are disabled, or if at least one mandatory - * field is not set.
- * Changed to NonNull in 2.8.1 - * - * @param aAPCertificate - * The AP certificate to use. May be null. - * @return The instance data or null. - * @since 3.0.0 + * @return Completely filled builder. Never null. + * @since 3.0.0 Beta7 */ @Nonnull - public static Phase4PeppolReceiverConfiguration getAsReceiverCheckData (@Nullable final X509Certificate aAPCertificate) + public static Phase4PeppolReceiverConfigurationBuilder getAsReceiverCheckDataBuilder () { final ISMPServiceMetadataProvider aSMPClient = getSMPClient (); final String sAS4EndpointURL = getAS4EndpointURL (); + final X509Certificate aAPCertificate = getAPCertificate (); final boolean bReceiverCheckEnabled; if (aSMPClient == null || StringHelper.hasNoText (sAS4EndpointURL) || aAPCertificate == null) @@ -320,13 +302,27 @@ public static Phase4PeppolReceiverConfiguration getAsReceiverCheckData (@Nullabl else bReceiverCheckEnabled = isReceiverCheckEnabled (); - return new Phase4PeppolReceiverConfiguration (bReceiverCheckEnabled, - aSMPClient, - getWildcardSelectionMode (), - sAS4EndpointURL, - aAPCertificate, - isPerformSBDHValueChecks (), - isCheckSBDHForMandatoryCountryC1 (), - isCheckSigningCertificateRevocation ()); + return Phase4PeppolReceiverConfiguration.builder () + .receiverCheckEnabled (bReceiverCheckEnabled) + .serviceMetadataProvider (aSMPClient) + .wildcardSelectionMode (getWildcardSelectionMode ()) + .as4EndpointUrl (sAS4EndpointURL) + .apCertificate (aAPCertificate) + .performSBDHValueChecks (isPerformSBDHValueChecks ()) + .checkSBDHForMandatoryCountryC1 (isCheckSBDHForMandatoryCountryC1 ()) + .checkSigningCertificateRevocation (isCheckSigningCertificateRevocation ()); + } + + /** + * Get the statically configured data as a + * {@link Phase4PeppolReceiverConfiguration} instance. + * + * @return The instance data and never null. + * @since 0.9.13 + */ + @Nonnull + public static Phase4PeppolReceiverConfiguration getAsReceiverCheckData () + { + return getAsReceiverCheckDataBuilder ().build (); } } diff --git a/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolReceiverConfiguration.java b/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolReceiverConfiguration.java index 4bfe8f34f..9b38d4d03 100644 --- a/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolReceiverConfiguration.java +++ b/phase4-peppol-servlet/src/main/java/com/helger/phase4/peppol/servlet/Phase4PeppolReceiverConfiguration.java @@ -23,6 +23,8 @@ import javax.annotation.concurrent.Immutable; import com.helger.commons.ValueEnforcer; +import com.helger.commons.builder.IBuilder; +import com.helger.commons.string.StringHelper; import com.helger.commons.string.ToStringGenerator; import com.helger.smpclient.peppol.ISMPServiceMetadataProvider; import com.helger.smpclient.peppol.PeppolWildcardSelector; @@ -182,4 +184,142 @@ public String toString () m_bCheckSigningCertificateRevocation) .getToString (); } + + /** + * @return An empty builder instance. Never null. + */ + @Nonnull + public static Phase4PeppolReceiverConfigurationBuilder builder () + { + return new Phase4PeppolReceiverConfigurationBuilder (); + } + + /** + * Create a builder instance with the data of the provided object already + * filled in. + * + * @param aSrc + * The source {@link Phase4PeppolReceiverConfiguration} to take the + * data from. May not be null. + * @return A non-null filled builder instance. + */ + @Nonnull + public static Phase4PeppolReceiverConfigurationBuilder builder (@Nonnull final Phase4PeppolReceiverConfiguration aSrc) + { + return new Phase4PeppolReceiverConfigurationBuilder (aSrc); + } + + /** + * A builder for class {@link Phase4PeppolReceiverConfiguration}. + * + * @author Philip Helger + * @since 3.0.0 Beta7 + */ + public static class Phase4PeppolReceiverConfigurationBuilder implements IBuilder + { + private boolean m_bReceiverCheckEnabled; + private ISMPServiceMetadataProvider m_aSMPClient; + private PeppolWildcardSelector.EMode m_eWildcardSelectionMode; + private String m_sAS4EndpointURL; + private X509Certificate m_aAPCertificate; + private boolean m_bPerformSBDHValueChecks; + private boolean m_bCheckSBDHForMandatoryCountryC1; + private boolean m_bCheckSigningCertificateRevocation; + + public Phase4PeppolReceiverConfigurationBuilder () + {} + + public Phase4PeppolReceiverConfigurationBuilder (@Nonnull final Phase4PeppolReceiverConfiguration aSrc) + { + ValueEnforcer.notNull (aSrc, "Src"); + receiverCheckEnabled (aSrc.isReceiverCheckEnabled ()).serviceMetadataProvider (aSrc.getSMPClient ()) + .wildcardSelectionMode (aSrc.getWildcardSelectionMode ()) + .as4EndpointUrl (aSrc.getAS4EndpointURL ()) + .apCertificate (aSrc.getAPCertificate ()) + .performSBDHValueChecks (aSrc.isPerformSBDHValueChecks ()) + .checkSBDHForMandatoryCountryC1 (aSrc.isCheckSBDHForMandatoryCountryC1 ()) + .checkSigningCertificateRevocation (aSrc.isCheckSigningCertificateRevocation ()); + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder receiverCheckEnabled (final boolean b) + { + m_bReceiverCheckEnabled = b; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder serviceMetadataProvider (@Nullable final ISMPServiceMetadataProvider a) + { + m_aSMPClient = a; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder wildcardSelectionMode (@Nullable final PeppolWildcardSelector.EMode e) + { + m_eWildcardSelectionMode = e; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder as4EndpointUrl (@Nullable final String s) + { + m_sAS4EndpointURL = s; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder apCertificate (@Nullable final X509Certificate a) + { + m_aAPCertificate = a; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder performSBDHValueChecks (final boolean b) + { + m_bPerformSBDHValueChecks = b; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder checkSBDHForMandatoryCountryC1 (final boolean b) + { + m_bCheckSBDHForMandatoryCountryC1 = b; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfigurationBuilder checkSigningCertificateRevocation (final boolean b) + { + m_bCheckSigningCertificateRevocation = b; + return this; + } + + @Nonnull + public Phase4PeppolReceiverConfiguration build () + { + if (m_bReceiverCheckEnabled) + { + if (m_aSMPClient == null) + throw new IllegalStateException ("The SMP Client must be provided"); + if (StringHelper.hasNoText (m_sAS4EndpointURL)) + throw new IllegalStateException ("Our AS4 Endpoint URL must be provided"); + if (m_aAPCertificate == null) + throw new IllegalStateException ("Our AS4 AP certificate must be provided"); + } + if (m_eWildcardSelectionMode == null) + throw new IllegalStateException ("The Wildcard Selection Mode must be provided"); + + return new Phase4PeppolReceiverConfiguration (m_bReceiverCheckEnabled, + m_aSMPClient, + m_eWildcardSelectionMode, + m_sAS4EndpointURL, + m_aAPCertificate, + m_bPerformSBDHValueChecks, + m_bCheckSBDHForMandatoryCountryC1, + m_bCheckSigningCertificateRevocation); + } + } }