Skip to content

Commit

Permalink
Added support for automatic Peppol wildcard resolution in the
Browse files Browse the repository at this point in the history
Sender; #203
  • Loading branch information
phax committed Jan 2, 2024
1 parent ccff246 commit c58e62d
Showing 1 changed file with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.ToStringGenerator;
import com.helger.peppol.smp.ESMPTransportProfile;
import com.helger.peppol.smp.ISMPTransportProfile;
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IParticipantIdentifier;
import com.helger.peppolid.IProcessIdentifier;
import com.helger.peppolid.peppol.PeppolIdentifierHelper;
import com.helger.phase4.util.Phase4Exception;
import com.helger.smpclient.exception.SMPClientException;
import com.helger.smpclient.peppol.ISMPServiceMetadataProvider;
import com.helger.smpclient.peppol.PeppolWildcardSelector;
import com.helger.smpclient.peppol.PeppolWildcardSelector.EMode;
import com.helger.smpclient.peppol.SMPClientReadOnly;
import com.helger.xsds.peppol.smp1.EndpointType;
import com.helger.xsds.peppol.smp1.SignedServiceMetadataType;

/**
* Implementation of {@link IAS4EndpointDetailProvider} using a Peppol SMP
Expand All @@ -48,11 +53,13 @@
*/
public class AS4EndpointDetailProviderPeppol implements IAS4EndpointDetailProvider
{
public static final EMode DEFAULT_WILDCARD_SELECTION_MODE = EMode.WILDCARD_ONLY;
public static final ISMPTransportProfile DEFAULT_TRANSPORT_PROFILE = ESMPTransportProfile.TRANSPORT_PROFILE_PEPPOL_AS4_V2;

private static final Logger LOGGER = LoggerFactory.getLogger (AS4EndpointDetailProviderPeppol.class);

private final ISMPServiceMetadataProvider m_aSMPClient;
private PeppolWildcardSelector.EMode m_eWildcardSelectionMode = EMode.WILDCARD_ONLY;
private ISMPTransportProfile m_aTP = DEFAULT_TRANSPORT_PROFILE;
private EndpointType m_aEndpoint;

Expand All @@ -72,6 +79,35 @@ public final ISMPServiceMetadataProvider getServiceMetadataProvider ()
return m_aSMPClient;
}

/**
* @return The transport profile to be used. Defaults to
* {@link #DEFAULT_WILDCARD_SELECTION_MODE}.
*/
@Nonnull
public final PeppolWildcardSelector.EMode getWildcardSelectionMode ()
{
return m_eWildcardSelectionMode;
}

/**
* Change the Peppol wildcard selection to be used for document type
* resolution, if a wildcard document type identifier is used. This only has
* an effect if it is called prior to
* {@link #init(IDocumentTypeIdentifier, IProcessIdentifier, IParticipantIdentifier)}.
*
* @param eWildcardSelectionMode
* The wildcard selection mode to be used. May not be
* <code>null</code>.
* @return this for chaining.
*/
@Nonnull
public final AS4EndpointDetailProviderPeppol setWildcardSelectionMode (@Nonnull final PeppolWildcardSelector.EMode eWildcardSelectionMode)
{
ValueEnforcer.notNull (eWildcardSelectionMode, "WildcardSlectionMode");
m_eWildcardSelectionMode = eWildcardSelectionMode;
return this;
}

/**
* @return The transport profile to be used. Defaults to
* {@link #DEFAULT_TRANSPORT_PROFILE}.
Expand Down Expand Up @@ -129,12 +165,28 @@ public void init (@Nonnull final IDocumentTypeIdentifier aDocTypeID,
aDocTypeID.getURIEncoded () +
", " +
aProcID.getURIEncoded () +
", " +
m_aTP.getID () +
")");

// Perform SMP lookup
try
{
m_aEndpoint = m_aSMPClient.getEndpoint (aReceiverID, aDocTypeID, aProcID, m_aTP);
final boolean bWildcard = PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD.equals (aDocTypeID.getScheme ()) &&
m_aSMPClient instanceof SMPClientReadOnly;
if (bWildcard)
{
// Wildcard lookup
final SignedServiceMetadataType aSSM = ((SMPClientReadOnly) m_aSMPClient).getWildcardServiceMetadataOrNull (aReceiverID,
aDocTypeID,
m_eWildcardSelectionMode);
m_aEndpoint = aSSM == null ? null : SMPClientReadOnly.getEndpoint (aSSM, aProcID, m_aTP);
}
else
{
// Direct match
m_aEndpoint = m_aSMPClient.getEndpoint (aReceiverID, aDocTypeID, aProcID, m_aTP);
}
if (m_aEndpoint == null)
throw new Phase4SMPException ("Failed to resolve SMP endpoint (" +
aReceiverID.getURIEncoded () +
Expand All @@ -144,7 +196,8 @@ public void init (@Nonnull final IDocumentTypeIdentifier aDocTypeID,
aProcID.getURIEncoded () +
", " +
m_aTP.getID () +
")");
")" +
(bWildcard ? " [wildcard]" : " [static]"));

if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Successfully resolved SMP endpoint (" +
Expand All @@ -155,7 +208,8 @@ public void init (@Nonnull final IDocumentTypeIdentifier aDocTypeID,
aProcID.getURIEncoded () +
", " +
m_aTP.getID () +
")");
")" +
(bWildcard ? " [wildcard]" : " [static]"));
}
catch (final SMPClientException ex)
{
Expand Down Expand Up @@ -195,4 +249,14 @@ public String getReceiverAPEndpointURL () throws Phase4Exception
throw new Phase4Exception ("Failed to determine the destination URL from the SMP endpoint: " + m_aEndpoint);
return sDestURL;
}

@Override
public String toString ()
{
return new ToStringGenerator (null).append ("SMPClient", m_aSMPClient)
.append ("TransportProfile", m_aTP)
.append ("EndpointSelectionMode", m_eWildcardSelectionMode)
.appendIfNotNull ("Endpoint", m_aEndpoint)
.getToString ();
}
}

0 comments on commit c58e62d

Please sign in to comment.