Skip to content

Commit

Permalink
Storing created DS references; #220
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 26, 2024
1 parent 6445d5e commit d18b5b0
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import org.apache.hc.core5.http.HttpEntity;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.http.HttpHeaderMap;
import com.helger.commons.string.ToStringGenerator;
import com.helger.phase4.messaging.http.HttpMimeMessageEntity;
import com.helger.phase4.messaging.http.HttpXMLEntity;
import com.helger.phase4.messaging.mime.AS4MimeMessageHelper;
import com.helger.xsds.xmldsig.ReferenceType;

import jakarta.mail.MessagingException;

Expand All @@ -37,25 +40,32 @@
*
* @author Philip Helger
*/
@NotThreadSafe
public final class AS4ClientBuiltMessage
{
private final String m_sMessageID;
private final HttpEntity m_aHttpEntity;
private final HttpHeaderMap m_aCustomHeaders;
private final HttpHeaderMap m_aCustomHttpHeaders;
private final ICommonsList <ReferenceType> m_aDSReferences;

public AS4ClientBuiltMessage (@Nonnull @Nonempty final String sMessageID, @Nonnull final HttpXMLEntity aHttpEntity)
public AS4ClientBuiltMessage (@Nonnull @Nonempty final String sMessageID,
@Nonnull final HttpXMLEntity aHttpEntity,
@Nullable final ICommonsList <ReferenceType> aCreatedDSReferences)
{
m_sMessageID = ValueEnforcer.notEmpty (sMessageID, "MessageID");
m_aHttpEntity = ValueEnforcer.notNull (aHttpEntity, "HttpEntity");
m_aCustomHeaders = null;
m_aCustomHttpHeaders = null;
m_aDSReferences = aCreatedDSReferences;
}

public AS4ClientBuiltMessage (@Nonnull @Nonempty final String sMessageID,
@Nonnull final HttpMimeMessageEntity aHttpEntity) throws MessagingException
@Nonnull final HttpMimeMessageEntity aHttpEntity,
@Nullable final ICommonsList <ReferenceType> aCreatedDSReferences) throws MessagingException
{
m_sMessageID = ValueEnforcer.notEmpty (sMessageID, "MessageID");
m_aHttpEntity = ValueEnforcer.notNull (aHttpEntity, "HttpEntity");
m_aCustomHeaders = AS4MimeMessageHelper.getAndRemoveAllHeaders (aHttpEntity.getMimeMessage ());
m_aCustomHttpHeaders = AS4MimeMessageHelper.getAndRemoveAllHeaders (aHttpEntity.getMimeMessage ());
m_aDSReferences = aCreatedDSReferences;
}

@Nonnull
Expand All @@ -78,17 +88,35 @@ public HttpEntity getHttpEntity ()

@Nullable
@ReturnsMutableCopy
public HttpHeaderMap getCustomHeaders ()
public HttpHeaderMap getAllCustomHttpHeaders ()
{
return m_aCustomHttpHeaders == null ? null : m_aCustomHttpHeaders.getClone ();
}

public boolean hasCustomHttpHeaders ()
{
return m_aCustomHttpHeaders != null && m_aCustomHttpHeaders.isNotEmpty ();
}

@Nullable
@ReturnsMutableCopy
public ICommonsList <ReferenceType> getAllDSReferences ()
{
return m_aDSReferences == null ? null : m_aDSReferences.getClone ();
}

public boolean hasDSReferences ()
{
return m_aCustomHeaders == null ? null : m_aCustomHeaders.getClone ();
return m_aDSReferences != null && m_aDSReferences.isNotEmpty ();
}

@Override
public String toString ()
{
return new ToStringGenerator (this).append ("MessageID", m_sMessageID)
.append ("HttpEntity", m_aHttpEntity)
.appendIfNotNull ("CustomHeaders", m_aCustomHeaders)
.appendIfNotNull ("CustomHttpHeaders", m_aCustomHttpHeaders)
.appendIfNotNull ("DSReferences", m_aDSReferences)
.getToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.helger.phase4.model.message.EAS4MessageType;
import com.helger.phase4.model.message.MessageHelperMethods;
import com.helger.phase4.util.AS4ResourceHelper;
import com.helger.xsds.xmldsig.ReferenceType;

/**
* AS4 client for {@link AS4ErrorMessage} objects.
Expand Down Expand Up @@ -112,37 +113,43 @@ public AS4ClientBuiltMessage buildMessage (@Nonnull @Nonempty final String sMess
if (aCallback != null)
aCallback.onAS4Message (aErrorMsg);

final Document aPureDoc = aErrorMsg.getAsSoapDocument ();
final Document aPureSoapDoc = aErrorMsg.getAsSoapDocument ();

if (aCallback != null)
aCallback.onSoapDocument (aPureDoc);
aCallback.onSoapDocument (aPureSoapDoc);

final Document aDoc;
ICommonsList <ReferenceType> aCreatedDSReferences = null;
if (m_bErrorShouldBeSigned && signingParams ().isSigningEnabled ())
{
final IAS4CryptoFactory aCryptoFactorySign = internalGetCryptoFactorySign ();

final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureDoc,
getSoapVersion (),
aErrorMsg.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());
final Document aSignedSoapDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureSoapDoc,
getSoapVersion (),
aErrorMsg.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());

// Extract the created references
aCreatedDSReferences = MessageHelperMethods.getAllDSigReferences (aSignedSoapDoc);

if (aCallback != null)
aCallback.onSignedSoapDocument (aSignedDoc);
aCallback.onSignedSoapDocument (aSignedSoapDoc);

aDoc = aSignedDoc;
aDoc = aSignedSoapDoc;
}
else
{
aDoc = aPureDoc;
aDoc = aPureSoapDoc;
}

// Wrap SOAP XML
return new AS4ClientBuiltMessage (sMessageID, new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()));
return new AS4ClientBuiltMessage (sMessageID,
new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()),
aCreatedDSReferences);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.w3c.dom.Document;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.string.StringHelper;
import com.helger.phase4.crypto.IAS4CryptoFactory;
import com.helger.phase4.ebms3header.Ebms3MessageInfo;
Expand All @@ -33,6 +34,7 @@
import com.helger.phase4.model.message.EAS4MessageType;
import com.helger.phase4.model.message.MessageHelperMethods;
import com.helger.phase4.util.AS4ResourceHelper;
import com.helger.xsds.xmldsig.ReferenceType;

/**
* AS4 client for {@link AS4PullRequestMessage} objects.
Expand Down Expand Up @@ -86,37 +88,43 @@ public AS4ClientBuiltMessage buildMessage (@Nonnull @Nonempty final String sMess
if (aCallback != null)
aCallback.onAS4Message (aPullRequest);

final Document aPureDoc = aPullRequest.getAsSoapDocument ();
final Document aPureSoapDoc = aPullRequest.getAsSoapDocument ();

if (aCallback != null)
aCallback.onSoapDocument (aPureDoc);
aCallback.onSoapDocument (aPureSoapDoc);

final Document aDoc;
ICommonsList <ReferenceType> aCreatedDSReferences = null;
if (signingParams ().isSigningEnabled ())
{
final IAS4CryptoFactory aCryptoFactorySign = internalGetCryptoFactorySign ();

final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureDoc,
getSoapVersion (),
aPullRequest.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());
final Document aSignedSoapDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureSoapDoc,
getSoapVersion (),
aPullRequest.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());

// Extract the created references
aCreatedDSReferences = MessageHelperMethods.getAllDSigReferences (aSignedSoapDoc);

if (aCallback != null)
aCallback.onSignedSoapDocument (aSignedDoc);
aCallback.onSignedSoapDocument (aSignedSoapDoc);

aDoc = aSignedDoc;
aDoc = aSignedSoapDoc;
}
else
{
aDoc = aPureDoc;
aDoc = aPureSoapDoc;
}

// Wrap SOAP XML
return new AS4ClientBuiltMessage (sMessageID, new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()));
return new AS4ClientBuiltMessage (sMessageID,
new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()),
aCreatedDSReferences);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import org.w3c.dom.Node;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.phase4.crypto.IAS4CryptoFactory;
import com.helger.phase4.ebms3header.Ebms3UserMessage;
import com.helger.phase4.messaging.crypto.AS4Signer;
import com.helger.phase4.messaging.http.HttpXMLEntity;
import com.helger.phase4.model.message.AS4ReceiptMessage;
import com.helger.phase4.model.message.EAS4MessageType;
import com.helger.phase4.model.message.MessageHelperMethods;
import com.helger.phase4.util.AS4ResourceHelper;
import com.helger.xsds.xmldsig.ReferenceType;

/**
* AS4 client for {@link AS4ReceiptMessage} objects.
Expand Down Expand Up @@ -155,37 +158,43 @@ public AS4ClientBuiltMessage buildMessage (@Nonnull @Nonempty final String sMess
if (aCallback != null)
aCallback.onAS4Message (aReceiptMsg);

final Document aPureDoc = aReceiptMsg.getAsSoapDocument ();
final Document aPureSoapDoc = aReceiptMsg.getAsSoapDocument ();

if (aCallback != null)
aCallback.onSoapDocument (aPureDoc);
aCallback.onSoapDocument (aPureSoapDoc);

final Document aDoc;
ICommonsList <ReferenceType> aCreatedDSReferences = null;
if (m_bReceiptShouldBeSigned && signingParams ().isSigningEnabled ())
{
final IAS4CryptoFactory aCryptoFactorySign = internalGetCryptoFactorySign ();

final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureDoc,
getSoapVersion (),
aReceiptMsg.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());
final Document aSignedSoapDoc = AS4Signer.createSignedMessage (aCryptoFactorySign,
aPureSoapDoc,
getSoapVersion (),
aReceiptMsg.getMessagingID (),
null,
getAS4ResourceHelper (),
bMustUnderstand,
signingParams ().getClone ());

// Extract the created references
aCreatedDSReferences = MessageHelperMethods.getAllDSigReferences (aSignedSoapDoc);

if (aCallback != null)
aCallback.onSignedSoapDocument (aSignedDoc);
aCallback.onSignedSoapDocument (aSignedSoapDoc);

aDoc = aSignedDoc;
aDoc = aSignedSoapDoc;
}
else
{
aDoc = aPureDoc;
aDoc = aPureSoapDoc;
}

// Wrap SOAP XML
return new AS4ClientBuiltMessage (sMessageID, new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()));
return new AS4ClientBuiltMessage (sMessageID,
new HttpXMLEntity (aDoc, getSoapVersion ().getMimeType ()),
aCreatedDSReferences);
}
}
Loading

0 comments on commit d18b5b0

Please sign in to comment.