use of com.helger.web.multipart.MultipartStream in project phase4 by phax.
the class AS4IncomingHandler method parseAS4Message.
public static void parseAS4Message(@Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @Nonnull final IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull @WillClose final InputStream aPayloadIS, @Nonnull final HttpHeaderMap aHttpHeaders, @Nonnull final IAS4ParsedMessageCallback aCallback, @Nullable final IAS4IncomingDumper aIncomingDumper) throws Phase4Exception, IOException, MessagingException, WSSecurityException {
// Determine content type
final String sContentType = aHttpHeaders.getFirstHeaderValue(CHttpHeader.CONTENT_TYPE);
if (StringHelper.hasNoText(sContentType))
throw new Phase4Exception("Content-Type header is missing");
if (LOGGER.isDebugEnabled())
LOGGER.debug("Received Content-Type string: '" + sContentType + "'");
final IMimeType aContentType = MimeTypeParser.safeParseMimeType(sContentType);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Received Content-Type object: " + aContentType);
if (aContentType == null)
throw new Phase4Exception("Failed to parse Content-Type '" + sContentType + "'");
final IMimeType aPlainContentType = aContentType.getCopyWithoutParameters();
// Fallback to global dumper if none is provided
final IAS4IncomingDumper aRealIncomingDumper = aIncomingDumper != null ? aIncomingDumper : AS4DumpManager.getIncomingDumper();
Document aSoapDocument = null;
ESoapVersion eSoapVersion = null;
final ICommonsList<WSS4JAttachment> aIncomingAttachments = new CommonsArrayList<>();
final Wrapper<OutputStream> aDumpOSHolder = new Wrapper<>();
if (aPlainContentType.equals(AS4RequestHandler.MT_MULTIPART_RELATED)) {
// MIME message
if (LOGGER.isDebugEnabled())
LOGGER.debug("Received MIME message");
final String sBoundary = aContentType.getParameterValueWithName("boundary");
if (StringHelper.hasNoText(sBoundary))
throw new Phase4Exception("Content-Type '" + sContentType + "' misses 'boundary' parameter");
if (LOGGER.isDebugEnabled())
LOGGER.debug("MIME Boundary: '" + sBoundary + "'");
// Ensure the stream gets closed correctly
try (final InputStream aRequestIS = AS4DumpManager.getIncomingDumpAwareInputStream(aRealIncomingDumper, aPayloadIS, aMessageMetadata, aHttpHeaders, aDumpOSHolder)) {
// PARSING MIME Message via MultipartStream
final MultipartStream aMulti = new MultipartStream(aRequestIS, sBoundary.getBytes(StandardCharsets.ISO_8859_1), (MultipartProgressNotifier) null);
int nIndex = 0;
while (true) {
final boolean bHasNextPart = nIndex == 0 ? aMulti.skipPreamble() : aMulti.readBoundary();
if (!bHasNextPart)
break;
if (LOGGER.isDebugEnabled())
LOGGER.debug("Found MIME part #" + nIndex);
try (final MultipartItemInputStream aBodyPartIS = aMulti.createInputStream()) {
// Read headers AND content
final MimeBodyPart aBodyPart = new MimeBodyPart(aBodyPartIS);
if (nIndex == 0) {
// First MIME part -> SOAP document
if (LOGGER.isDebugEnabled())
LOGGER.debug("Parsing first MIME part as SOAP document");
// Read SOAP document
aSoapDocument = DOMReader.readXMLDOM(aBodyPart.getInputStream());
IMimeType aPlainPartMT = MimeTypeParser.safeParseMimeType(aBodyPart.getContentType());
if (aPlainPartMT != null)
aPlainPartMT = aPlainPartMT.getCopyWithoutParameters();
// Determine SOAP version from MIME part content type
eSoapVersion = ESoapVersion.getFromMimeTypeOrNull(aPlainPartMT);
if (eSoapVersion != null && LOGGER.isDebugEnabled())
LOGGER.debug("Determined SOAP version " + eSoapVersion + " from Content-Type");
if (eSoapVersion == null && aSoapDocument != null) {
// Determine SOAP version from the read document
eSoapVersion = ESoapVersion.getFromNamespaceURIOrNull(XMLHelper.getNamespaceURI(aSoapDocument));
if (eSoapVersion != null && LOGGER.isDebugEnabled())
LOGGER.debug("Determined SOAP version " + eSoapVersion + " from XML root element namespace URI");
}
} else {
// MIME Attachment (index is gt 0)
if (LOGGER.isDebugEnabled())
LOGGER.debug("Parsing MIME part #" + nIndex + " as attachment");
final WSS4JAttachment aAttachment = aIAF.createAttachment(aBodyPart, aResHelper);
aIncomingAttachments.add(aAttachment);
}
}
nIndex++;
}
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Read MIME message with " + aIncomingAttachments.size() + " attachment(s)");
} else {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Received plain message");
// Expect plain SOAP - read whole request to DOM
// Note: this may require a huge amount of memory for large requests
aSoapDocument = DOMReader.readXMLDOM(AS4DumpManager.getIncomingDumpAwareInputStream(aRealIncomingDumper, aPayloadIS, aMessageMetadata, aHttpHeaders, aDumpOSHolder));
if (LOGGER.isDebugEnabled())
if (aSoapDocument != null)
LOGGER.debug("Successfully parsed payload as XML");
else
LOGGER.debug("Failed to parse payload as XML");
if (aSoapDocument != null) {
// Determine SOAP version from the read document
eSoapVersion = ESoapVersion.getFromNamespaceURIOrNull(XMLHelper.getNamespaceURI(aSoapDocument));
if (eSoapVersion != null && LOGGER.isDebugEnabled())
LOGGER.debug("Determined SOAP version " + eSoapVersion + " from XML root element namespace URI");
}
if (eSoapVersion == null) {
// Determine SOAP version from content type
eSoapVersion = ESoapVersion.getFromMimeTypeOrNull(aPlainContentType);
if (eSoapVersion != null && LOGGER.isDebugEnabled())
LOGGER.debug("Determined SOAP version " + eSoapVersion + " from Content-Type");
}
}
try {
if (aSoapDocument == null) {
// We don't have a SOAP document
throw new Phase4Exception(eSoapVersion == null ? "Failed to parse incoming message!" : "Failed to parse incoming SOAP " + eSoapVersion.getVersion() + " document!");
}
if (eSoapVersion == null) {
// We're missing a SOAP version
throw new Phase4Exception("Failed to determine SOAP version of XML document!");
}
aCallback.handle(aHttpHeaders, aSoapDocument, eSoapVersion, aIncomingAttachments);
} finally {
// Here, the incoming dump is finally ready closed and usable
if (aRealIncomingDumper != null && aDumpOSHolder.isSet())
try {
aRealIncomingDumper.onEndRequest(aMessageMetadata);
} catch (final Exception ex) {
LOGGER.error("IncomingDumper.onEndRequest failed. Dumper=" + aRealIncomingDumper + "; MessageMetadata=" + aMessageMetadata, ex);
}
}
}
Aggregations