Search in sources :

Example 1 with HasInputStream

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);
                }
            }
        }
    }
}
Also used : HasInputStream(com.helger.commons.io.stream.HasInputStream) IHasInputStream(com.helger.commons.io.IHasInputStream) AS4DecompressException(com.helger.phase4.attachment.AS4DecompressException) MultipartItemInputStream(com.helger.web.multipart.MultipartStream.MultipartItemInputStream) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) HasInputStream(com.helger.commons.io.stream.HasInputStream) IHasInputStream(com.helger.commons.io.IHasInputStream) InputStream(java.io.InputStream) IHasInputStream(com.helger.commons.io.IHasInputStream) EAS4CompressionMode(com.helger.phase4.attachment.EAS4CompressionMode) IOException(java.io.IOException) Ebms3PartInfo(com.helger.phase4.ebms3header.Ebms3PartInfo) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property)

Aggregations

IHasInputStream (com.helger.commons.io.IHasInputStream)1 HasInputStream (com.helger.commons.io.stream.HasInputStream)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)1 EAS4CompressionMode (com.helger.phase4.attachment.EAS4CompressionMode)1 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)1 Ebms3PartInfo (com.helger.phase4.ebms3header.Ebms3PartInfo)1 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)1 MultipartItemInputStream (com.helger.web.multipart.MultipartStream.MultipartItemInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1