Repository navigation
Incoming Payload Streaming
Since v5.0.0 the Peppol, HR eDelivery and DBNAlliance servlets do not read an incoming payload into memory. The handler gets an object from which the payload can be read as a stream, as often as needed. This page describes how to read the payload in a handler. See GitHub issue #414 for the background and the measurements.
- The AS4 attachment is stored, so that it can be read multiple times. Decrypted attachments are always written to a temporary file. Unencrypted attachments larger than 64 KB are stored in a temporary file, smaller ones are kept in memory. The attachment stays compressed - it is decompressed on every read.
- The SBDH (Peppol, HR eDelivery) resp. the XHE (DBNAlliance) is read with a streaming StAX parser. The header is checked as before (e.g. the Peppol SBDH rules and the receiver checks). The business message is parsed completely, so it must be well-formed XML, but it is not kept in memory.
- If all checks passed, the handler SPI is invoked. It gets the header data (without the business message) and an incoming document object, from which the payload can be read again.
Every read of the incoming document starts from the beginning and decompresses the attachment again. This costs CPU time, but the heap needed does not grow with the payload size.
| Profile | Handler SPI | Incoming document | Header data |
|---|---|---|---|
| Peppol | IPhase4PeppolIncomingSBDHandlerSPI.handleIncomingSBD |
Phase4PeppolIncomingSBD |
PeppolSBDHData |
| HR eDelivery | IPhase4HREDeliveryIncomingSBDHandlerSPI.handleIncomingSBD |
Phase4HREDeliveryIncomingSBD |
HREDeliverySBDHData |
| DBNAlliance | IPhase4DBNAllianceIncomingXHEHandlerSPI.handleIncomingXHE |
Phase4DBNAllianceIncomingXHE |
DBNAllianceXHEData |
All incoming document classes implement com.helger.phase4.incoming.IAS4IncomingDocument and offer:
| Method | Description | Memory |
|---|---|---|
getInputStream() |
A new stream over the complete document (SBD resp. XHE), as received via AS4, just decrypted and decompressed. The caller must close it. | Constant |
getAllBytes() |
The complete document as a byte[] - the behaviour of phase4 4.x |
Depends on the document size |
getStandardBusinessDocument() (Peppol, HR eDelivery) resp. getXHE() (DBNAlliance) |
The parsed JAXB document. It is parsed on the first call and cached afterwards. | Depends on the document size |
Important: the data is backed by temporary files that are deleted once the message was processed. Read the payload while the handler method runs - do not keep the incoming document object, or a stream opened from it, for later use. If the payload needs to be processed asynchronously, copy it to your own storage first.
The recommended way is to copy the stream to your own storage, for example a file:
public void handleIncomingSBD (@NonNull final IAS4IncomingMessageMetadata aMessageMetadata,
@NonNull final HttpHeaderMap aHeaders,
@NonNull final Ebms3UserMessage aUserMessage,
@NonNull final Phase4PeppolIncomingSBD aIncomingSBD,
@NonNull final PeppolSBDHData aPeppolSBD,
@NonNull final IAS4IncomingMessageState aState,
@NonNull final AS4ErrorList aProcessingErrorMessages) throws Exception
{
final File aFile = ...;
// Stream the payload to disk, so that it is never held in memory as a whole
if (StreamHelper.copyByteStream ()
.from (aIncomingSBD.getInputStream ())
.closeFrom (true)
.to (FileHelper.getBufferedOutputStream (aFile))
.closeTo (true)
.build ()
.isFailure ())
throw new IllegalStateException ("Failed to write SBD to '" + aFile.getAbsolutePath () + "'");
}The HR eDelivery handler works the same, with Phase4HREDeliveryIncomingSBD and HREDeliverySBDHData.
The header data is complete, except for the business message:
-
hasBusinessMessage()returnsfalse -
getBusinessMessageNamespaceURI()andgetBusinessMessageLocalName()return the name of the business message root element (e.g.urn:oasis:names:specification:ubl:schema:xsd:Invoice-2andInvoice) -
PeppolSBDHData.isNonXMLPayload()tells whether the business message is a PeppolBinaryContentorTextContentwrapper
If the business message is needed as a DOM, call aIncomingSBD.getStandardBusinessDocument().getAny().
This needs as much memory as in phase4 4.x, so use it only for payloads of a known, limited size.
An XHE can contain several payloads.
aDBNAllianceXHE.getPayloads() returns them in document order, without their content.
DBNAlliancePayload.getPayloadContentKind() tells what the content is:
EDBNAlliancePayloadContentKind |
Meaning | Method of Phase4DBNAllianceIncomingXHE
|
|---|---|---|
XML |
The content is an XML element. getPayloadContentNamespaceURI() and getPayloadContentLocalName() return its name. |
openPayloadXMLStreamReader(int) or getPayloadElement(int)
|
TEXT |
The content is text, because the MIME type of the payload is textual | openPayloadTextReader(int) |
BINARY |
The content is Base64 encoded binary data |
openPayloadBinaryStream(int) - decodes the Base64 while reading |
NONE |
There is no PayloadContent
|
- |
The int parameter is the 0-based index of the payload in getPayloads().
Every method opens the XHE again and moves to the respective payload, so the payloads can be read in any order and more than once.
The returned reader or stream must be closed by the caller.
An IOException is thrown if the payload does not exist or its content does not match the method.
public void handleIncomingXHE (@NonNull final IAS4IncomingMessageMetadata aMessageMetadata,
@NonNull final HttpHeaderMap aHeaders,
@NonNull final Ebms3UserMessage aUserMessage,
@NonNull final Phase4DBNAllianceIncomingXHE aIncomingXHE,
@NonNull final DBNAllianceXHEData aDBNAllianceXHE,
@NonNull final IAS4IncomingMessageState aState,
@NonNull final AS4ErrorList aProcessingErrorMessages) throws Exception
{
final List <DBNAlliancePayload> aPayloads = aDBNAllianceXHE.getPayloads ();
for (int nIndex = 0; nIndex < aPayloads.size (); ++nIndex)
{
final DBNAlliancePayload aPayload = aPayloads.get (nIndex);
switch (aPayload.getPayloadContentKind ())
{
case XML:
{
final XMLStreamReader aReader = aIncomingXHE.openPayloadXMLStreamReader (nIndex);
try
{
// Positioned on the start element of the payload root element
// Process it up to the matching end element
}
finally
{
// Also closes the underlying stream
aReader.close ();
}
break;
}
case TEXT:
try (final Reader aReader = aIncomingXHE.openPayloadTextReader (nIndex))
{
// Process the text
}
break;
case BINARY:
try (final InputStream aIS = aIncomingXHE.openPayloadBinaryStream (nIndex))
{
// Process the decoded bytes, e.g. copy them to a file
}
break;
case NONE:
break;
}
}
}XMLStreamReader is not AutoCloseable, therefore the finally block.
Details on the XML payload methods:
-
openPayloadXMLStreamReaderreturns a reader on the whole XHE that is positioned on the payload root element. Read only up to the matching end element. The reader is not coalescing, so large text nodes are delivered in several chunks. -
getPayloadElementreads only this payload into a DOM - the rest of the XHE is not kept in memory. The memory needed depends on the size of this payload. - The text reader delivers the characters chunk by chunk.
If the text content contains an XML element, an
IOExceptionis thrown while reading.
To get the complete XHE as a JAXB object, call aIncomingXHE.getXHE().
This needs as much memory as in phase4 4.x.
- Streaming covers the payload processing only. Before the handler is invoked, the AES-GCM decryption and the signature verification still have memory peaks that depend on the size of the compressed attachment (see WSS-735).
- Only Peppol, HR eDelivery and DBNAlliance have a profile specific handler SPI.
For the other profiles (e.g. CEF, BDEW, ENTSOG) an
IAS4IncomingMessageProcessorSPIgets the attachments with their stream providers, as before.
My personal Coding Styleguide | It is appreciated if you star the GitHub project if you like it.
