Skip to main content
Package: com.helger.phase4.sender
Maven artifact: com.helger.phase4:phase4-lib
AS4Sender is the entry point for sending generic AS4 messages without applying any profile-specific settings. Use the profile-specific senders (e.g. Phase4PeppolSender) when you need profile enforcement.

Factory methods

// Create a builder for a User Message (sends a payload)
AS4Sender.BuilderUserMessage builder = AS4Sender.builderUserMessage();

// Create a builder for a Pull Request (since 0.12.0)
AS4Sender.BuilderPullRequest pullBuilder = AS4Sender.builderPullRequest();

BuilderUserMessage

BuilderUserMessage extends AbstractAS4UserMessageBuilderMIMEPayload. The payload is always placed in a MIME part rather than in the SOAP body.

Required fields

body.fromPartyID
String
required
The AS4 sender party identifier value. Maps to eb:PartyInfo/eb:From/eb:PartyId.
body.fromRole
String
required
The sender role URI. Maps to eb:PartyInfo/eb:From/eb:Role.
body.toPartyID
String
required
The AS4 receiver party identifier value. Maps to eb:PartyInfo/eb:To/eb:PartyId.
body.toRole
String
required
The receiver role URI. Maps to eb:PartyInfo/eb:To/eb:Role.
body.endpointURL
String
required
The destination AS4 endpoint URL to send to.
body.pmode
IPMode
The P-Mode governing this message exchange. Optional — if not set explicitly, the pmodeResolver will attempt to find a matching PMode automatically based on the message metadata.

Optional fields

body.fromPartyIDType
String
The party ID type (scheme) for the sender party. Optional.
body.toPartyIDType
String
The party ID type (scheme) for the receiver party. Optional.
body.serviceType
String
The service type of the eb:Service element. Optional.
body.service
String
The service value of the eb:Service element. Optional.
body.action
String
The eb:Action value. Optional.
body.agreementRef
String
The eb:AgreementRef value. Optional.
body.agreementType
String
The eb:AgreementRef type attribute value. Optional. Since 2.7.8.
body.pmodeID
String
The PMode ID to be included in the user message. Optional.
body.conversationID
String
The AS4 conversation ID. If not set, a random ID is generated.
body.payload
AS4OutgoingAttachment
The main payload sent as the first MIME attachment. Use AS4OutgoingAttachment.Builder to construct.
body.receiverCertificate
X509Certificate
The X.509 certificate of the receiver used for message encryption. Mutually exclusive with receiverCertificateAlias.
body.receiverCertificateAlias
String
Alias into the crypto factory keystore identifying the receiver certificate. Mutually exclusive with receiverCertificate. Since 2.1.4.
body.forceMimeMessage
boolean
default:"false"
Force the message to be wrapped in a MIME envelope even when there are no attachments. Since 2.5.1.
body.signalMsgConsumer
IAS4SignalMessageConsumer
Optional callback receiving the synchronous AS4 Signal Message response.
body.signalMsgValidationResultHdl
IAS4SignalMessageValidationResultHandler
Optional informational callback invoked after signal message reference validation. Since 3.0.1.

Attachment management

// Add a single additional attachment
builder.addAttachment(AS4OutgoingAttachment.builder()
    .data(myBytes)
    .mimeType(CMimeType.APPLICATION_XML)
    .build());

// Replace all attachments
builder.attachments(attachment1, attachment2);

// Clear and set a single attachment
builder.attachment(singleAttachment);

Message properties

// Add a single property (accumulates)
builder.addMessageProperty(
    MessageProperty.builder().name("originalSender").value("urn:oasis:tc:ebcore:partyid-type:unregistered:C1")
);

// Replace all properties
builder.messageProperties(prop1, prop2);

Sending

// Low-level: returns ESuccess
ESuccess result = builder.sendMessage();

// High-level: checks for a valid AS4 Receipt
EAS4UserMessageSendResult result = builder.sendMessageAndCheckForReceipt();

// High-level with exception consumer
EAS4UserMessageSendResult result = builder.sendMessageAndCheckForReceipt(
    ex -> LOGGER.error("Send failed", ex)
);

EAS4UserMessageSendResult

Return value of sendMessageAndCheckForReceipt().
Enum valueIDDescriptionRetry feasible
SUCCESSsuccessReceipt received — message delivered successfully
INVALID_PARAMETERSinvalid-parametersMandatory builder fields were missing or incorrectNo
TRANSPORT_ERRORtransport-errorNetwork or HTTP(S) level failureYes
TRANSPORT_ERROR_NO_RETRYtransport-error-no-retryNetwork/HTTP failure where retry is not recommended (since 3.2.0)No
NO_SIGNAL_MESSAGE_RECEIVEDno-signal-msg-receivedNo valid AS4 Signal Message in the responseYes
AS4_ERROR_MESSAGE_RECEIVEDas4-error-msg-receivedAS4 Error Message returned by the other sideNo
INVALID_SIGNAL_MESSAGE_RECEIVEDinvalid-signal-message-receivedSignal Message contained neither error nor receiptYes
if (result.isSuccess()) {
    // message delivered
} else if (result.isRetryFeasible()) {
    // consider retrying
}

BuilderPullRequest

BuilderPullRequest extends AbstractAS4PullRequestBuilder. Available since 0.12.0.

Required fields

body.mpc
String
required
The Message Partition Channel (MPC) to reference in the Pull Request.
body.endpointURL
String
required
The destination AS4 endpoint URL.

Optional fields

body.pmode
IPMode
The P-Mode to use. Optional.
body.pmodeID
String
Optional PMode ID to include. Since 3.0.0.
body.useLeg1
boolean
default:"true"
Whether to use Leg 1 (true) or Leg 2 (false) of the PMode. Since 2.7.8.
body.userMsgConsumer
IAS4UserMessageConsumer
Optional consumer for the AS4 User Message received in response.
body.signalMsgConsumer
IAS4SignalMessageConsumer
Optional consumer for the AS4 Signal Message received in response.

Complete example

import com.helger.phase4.sender.AS4Sender;
import com.helger.phase4.sender.EAS4UserMessageSendResult;
import com.helger.phase4.attachment.AS4OutgoingAttachment;
import com.helger.mime.CMimeType;

EAS4UserMessageSendResult result = AS4Sender.builderUserMessage()
    .cryptoFactorySign(cryptoFactory)
    .cryptoFactoryCrypt(cryptoFactory)
    .pmode(myPMode)
    .fromPartyID("sender-party-id")
    .fromRole(CAS4.DEFAULT_INITIATOR_URL)
    .toPartyID("receiver-party-id")
    .toRole(CAS4.DEFAULT_RESPONDER_URL)
    .endpointURL("https://receiver.example.com/as4")
    .receiverCertificate(receiverX509Cert)
    .payload(
        AS4OutgoingAttachment.builder()
            .data(invoiceXmlBytes)
            .mimeType(CMimeType.APPLICATION_XML)
            .build()
    )
    .sendMessageAndCheckForReceipt();

if (result.isSuccess()) {
    System.out.println("Message sent successfully");
} else {
    System.err.println("Failed: " + result.getID());
}

Build docs developers (and LLMs) love