use of com.helger.commons.io.stream.HasInputStream in project phase4 by phax.
the class AS4IncomingHandler method _decompressAttachments.
private static void _decompressAttachments(@Nonnull final ICommonsList<WSS4JAttachment> aIncomingDecryptedAttachments, @Nonnull final Ebms3UserMessage aUserMessage, @Nonnull final IAS4MessageState aState) {
// For all incoming attachments
for (final WSS4JAttachment aIncomingAttachment : aIncomingDecryptedAttachments.getClone()) {
final EAS4CompressionMode eCompressionMode = aState.getAttachmentCompressionMode(aIncomingAttachment.getId());
if (eCompressionMode != null) {
final IHasInputStream aOldISP = aIncomingAttachment.getInputStreamProvider();
aIncomingAttachment.setSourceStreamProvider(new HasInputStream(() -> {
try {
final InputStream aSrcIS = aOldISP.getInputStream();
if (aSrcIS == null)
throw new IllegalStateException("Failed to create InputStream from " + aOldISP);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Decompressing attachment with ID '" + aIncomingAttachment.getId() + "' using " + eCompressionMode);
return eCompressionMode.getDecompressStream(aSrcIS);
} catch (final IOException ex) {
// invalid payload
throw new AS4DecompressException(ex);
}
}, aOldISP.isReadMultiple()));
// Remember the compression mode
aIncomingAttachment.setCompressionMode(eCompressionMode);
final String sAttachmentContentID = StringHelper.trimStart(aIncomingAttachment.getId(), "attachment=");
// x.getHref() != null needed since, if a message contains a payload and
// an attachment, it would throw a NullPointerException since a payload
// does not have anything written in its partinfo therefore also now
// href
final Ebms3PartInfo aPartInfo = CollectionHelper.findFirst(aUserMessage.getPayloadInfo().getPartInfo(), x -> x.getHref() != null && x.getHref().contains(sAttachmentContentID));
if (aPartInfo != null && aPartInfo.getPartProperties() != null) {
// Find MimeType property
final Ebms3Property aProperty = CollectionHelper.findFirst(aPartInfo.getPartProperties().getProperty(), x -> x.getName().equalsIgnoreCase(MessageHelperMethods.PART_PROPERTY_MIME_TYPE));
if (aProperty != null) {
final String sMimeType = aProperty.getValue();
if (MimeTypeParser.safeParseMimeType(sMimeType) == null)
LOGGER.warn("Value '" + sMimeType + "' of property '" + MessageHelperMethods.PART_PROPERTY_MIME_TYPE + "' is not a valid MIME type");
aIncomingAttachment.overwriteMimeType(sMimeType);
}
}
}
}
}
Aggregations