Search in sources :

Example 6 with MessageParameters

use of com.helger.as2lib.params.MessageParameters in project as2-lib by phax.

the class AbstractDirectoryPollingModule method updateMessage.

public void updateMessage(@Nonnull final IMessage aMsg, @Nonnull final File aFile) throws AS2Exception {
    final MessageParameters aParams = new MessageParameters(aMsg);
    final String sDefaults = attrs().getAsString(ATTR_DEFAULTS);
    if (sDefaults != null)
        aParams.setParameters(sDefaults);
    final String sFilename = aFile.getName();
    final String sFormat = attrs().getAsString(ATTR_FORMAT);
    if (sFormat != null) {
        final String sDelimiters = attrs().getAsString(ATTR_DELIMITERS, ".-");
        aParams.setParameters(sFormat, sDelimiters, sFilename);
    }
    try {
        final byte[] aData = SimpleFileIO.getAllFileBytes(aFile);
        String sContentType = attrs().getAsString(ATTR_MIMETYPE);
        if (sContentType == null) {
            // Default to application/octet-stream
            sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
        } else {
            try {
                sContentType = aParams.format(sContentType);
            } catch (final AS2InvalidParameterException ex) {
                LOGGER.error("Bad content-type '" + sContentType + "'" + aMsg.getLoggingText());
                // Default to application/octet-stream
                sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
            }
        }
        final ByteArrayDataSource aByteSource = new ByteArrayDataSource(aData, sContentType, null);
        final MimeBodyPart aBody = new MimeBodyPart();
        aBody.setDataHandler(aByteSource.getAsDataHandler());
        // Headers must be set AFTER the DataHandler
        final String sCTE = aMsg.partnership().getContentTransferEncodingSend(EContentTransferEncoding.AS2_DEFAULT.getID());
        aBody.setHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
        // below statement is not filename related, just want to make it
        // consist with the parameter "mimetype="application/EDI-X12""
        // defined in config.xml 2007-06-01
        aBody.setHeader(CHttpHeader.CONTENT_TYPE, sContentType);
        // add below statement will tell the receiver to save the filename
        // as the one sent by sender. 2007-06-01
        final String sSendFilename = attrs().getAsString(ATTR_SENDFILENAME);
        if ("true".equals(sSendFilename)) {
            final String sMAFilename = aMsg.attrs().getAsString(CFileAttribute.MA_FILENAME);
            final String sContentDisposition = "Attachment; filename=\"" + sMAFilename + "\"";
            aBody.setHeader(CHttpHeader.CONTENT_DISPOSITION, sContentDisposition);
            aMsg.setContentDisposition(sContentDisposition);
        }
        aMsg.setData(aBody);
    } catch (final MessagingException ex) {
        throw WrappedAS2Exception.wrap(ex);
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Updating partnership for AS2 message" + aMsg.getLoggingText());
    // update the message's partnership with any stored information
    getSession().getPartnershipFactory().updatePartnership(aMsg, true);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Finished updating partnership for AS2 message");
    aMsg.updateMessageID();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Updated message ID to " + aMsg.getMessageID());
}
Also used : AS2InvalidParameterException(com.helger.as2lib.params.AS2InvalidParameterException) MessagingException(javax.mail.MessagingException) MessageParameters(com.helger.as2lib.params.MessageParameters) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource)

Aggregations

MessageParameters (com.helger.as2lib.params.MessageParameters)6 CompositeParameters (com.helger.as2lib.params.CompositeParameters)3 DateParameters (com.helger.as2lib.params.DateParameters)3 AS2Exception (com.helger.as2lib.exception.AS2Exception)2 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)2 AS2InvalidParameterException (com.helger.as2lib.params.AS2InvalidParameterException)2 Partnership (com.helger.as2lib.partner.Partnership)2 File (java.io.File)2 IOException (java.io.IOException)2 Nonnull (javax.annotation.Nonnull)2 ECryptoAlgorithmSign (com.helger.as2lib.crypto.ECryptoAlgorithmSign)1 MIC (com.helger.as2lib.crypto.MIC)1 DispositionOptions (com.helger.as2lib.disposition.DispositionOptions)1 AS2MessageMDN (com.helger.as2lib.message.AS2MessageMDN)1 IMessage (com.helger.as2lib.message.IMessage)1 RandomParameters (com.helger.as2lib.params.RandomParameters)1 AS2PartnershipNotFoundException (com.helger.as2lib.partner.AS2PartnershipNotFoundException)1 Nonempty (com.helger.commons.annotation.Nonempty)1 FileIOError (com.helger.commons.io.file.FileIOError)1 ETriState (com.helger.commons.state.ETriState)1