Search in sources :

Example 1 with IContentTransferEncoding

use of com.helger.mail.cte.IContentTransferEncoding in project as2-lib by phax.

the class HTTPHelper method readAndDecodeHttpRequest.

@Nonnull
public static DataSource readAndDecodeHttpRequest(@Nonnull final IAS2HttpRequestDataProvider aRDP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException {
    // Main read
    DataSource aPayload = HTTPHelper.readHttpRequest(aRDP, aResponseHandler, aMsg, aIncomingDumper);
    // Check the transfer encoding of the request. If none is provided, check
    // the partnership for a default one. If none is in the partnership used the
    // default one
    final String sCTE = aMsg.partnership().getContentTransferEncodingReceive(EContentTransferEncoding.AS2_DEFAULT.getID());
    final String sContentTransferEncoding = aMsg.getHeaderOrDefault(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
    if (StringHelper.hasText(sContentTransferEncoding)) {
        final IContentTransferEncoding aCTE = EContentTransferEncoding.getFromIDCaseInsensitiveOrNull(sContentTransferEncoding);
        if (aCTE == null) {
            if (LOGGER.isWarnEnabled())
                LOGGER.warn("Unsupported Content-Transfer-Encoding '" + sContentTransferEncoding + "' is used - ignoring!");
        } else {
            // Decode data if necessary
            final IByteArrayCodec aCodec = aCTE.createCodec();
            // TODO: Handle decoding when large file support is on
            if (!(aCodec instanceof IdentityCodec<?>) && aPayload instanceof ByteArrayDataSource) {
                byte[] aActualBytes = ((ByteArrayDataSource) aPayload).directGetBytes();
                // Remember original length before continuing
                final int nOriginalContentLength = aActualBytes.length;
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("Incoming message uses Content-Transfer-Encoding '" + sContentTransferEncoding + "' - decoding");
                aActualBytes = aCodec.getDecoded(aActualBytes);
                aPayload = new ByteArrayDataSource(aActualBytes, aPayload.getContentType(), aPayload.getName());
                // Remember that we potentially did something
                aMsg.attrs().putIn(MA_HTTP_ORIGINAL_CONTENT_TRANSFER_ENCODING, sContentTransferEncoding);
                aMsg.attrs().putIn(MA_HTTP_ORIGINAL_CONTENT_LENGTH, nOriginalContentLength);
            }
        }
    }
    return aPayload;
}
Also used : IContentTransferEncoding(com.helger.mail.cte.IContentTransferEncoding) IByteArrayCodec(com.helger.commons.codec.IByteArrayCodec) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) IExtendedDataSource(com.helger.mail.datasource.IExtendedDataSource) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) InputStreamDataSource(com.helger.mail.datasource.InputStreamDataSource) DataSource(javax.activation.DataSource) Nonnull(javax.annotation.Nonnull)

Aggregations

IByteArrayCodec (com.helger.commons.codec.IByteArrayCodec)1 IContentTransferEncoding (com.helger.mail.cte.IContentTransferEncoding)1 ByteArrayDataSource (com.helger.mail.datasource.ByteArrayDataSource)1 IExtendedDataSource (com.helger.mail.datasource.IExtendedDataSource)1 InputStreamDataSource (com.helger.mail.datasource.InputStreamDataSource)1 DataSource (javax.activation.DataSource)1 Nonnull (javax.annotation.Nonnull)1