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());
}
Aggregations