use of com.helger.as2lib.params.AS2InvalidParameterException in project as2-lib by phax.
the class AS2Message method generateMessageID.
@Override
@Nonnull
@Nonempty
public String generateMessageID() {
final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(this)).add("rand", new RandomParameters());
final String sIDFormat = partnership().getMessageIDFormat(DEFAULT_ID_FORMAT);
final StringBuilder aSB = new StringBuilder();
aSB.append('<');
try {
aSB.append(aParams.format(sIDFormat));
} catch (final AS2InvalidParameterException ex) {
// useless, but what to do?
aSB.append(sIDFormat);
}
aSB.append('>');
return aSB.toString();
}
use of com.helger.as2lib.params.AS2InvalidParameterException in project as2-lib by phax.
the class MainTestClient method checkRequired.
protected static void checkRequired(@Nonnull final IMessage aMsg) throws AS2InvalidParameterException {
final Partnership aPartnership = aMsg.partnership();
try {
AS2InvalidParameterException.checkValue(aMsg, "ContentType", aMsg.getContentType());
AS2InvalidParameterException.checkValue(aMsg, "Attribute: " + CPartnershipIDs.PA_AS2_URL, aPartnership.getAS2URL());
AS2InvalidParameterException.checkValue(aMsg, "Receiver: " + CPartnershipIDs.PID_AS2, aPartnership.getReceiverAS2ID());
AS2InvalidParameterException.checkValue(aMsg, "Sender: " + CPartnershipIDs.PID_AS2, aPartnership.getSenderAS2ID());
AS2InvalidParameterException.checkValue(aMsg, "Subject", aMsg.getSubject());
AS2InvalidParameterException.checkValue(aMsg, "Sender: " + CPartnershipIDs.PID_EMAIL, aPartnership.getSenderEmail());
AS2InvalidParameterException.checkValue(aMsg, "Message Data", aMsg.getData());
} catch (final AS2InvalidParameterException ex) {
ex.setSourceMsg(aMsg);
throw ex;
}
}
use of com.helger.as2lib.params.AS2InvalidParameterException in project as2-lib by phax.
the class DirectoryResenderModule method scanDirectory.
/**
* @return A list with all files that are ready to be resend.
* @throws AS2InvalidParameterException
* In case the directory listing fails
*/
@Nonnull
@ReturnsMutableCopy
protected ICommonsList<File> scanDirectory() throws AS2InvalidParameterException {
final File aResendDir = AS2IOHelper.getDirectoryFile(getAttributeAsStringRequired(ATTR_RESEND_DIRECTORY));
final File[] aFiles = aResendDir.listFiles();
if (aFiles == null) {
throw new AS2InvalidParameterException("Error getting list of files in directory", this, ATTR_RESEND_DIRECTORY, aResendDir.getAbsolutePath());
}
final ICommonsList<File> ret = new CommonsArrayList<>();
if (aFiles.length > 0)
for (final File aCurrentFile : aFiles) if (aCurrentFile.exists() && aCurrentFile.isFile() && aCurrentFile.canWrite() && isTimeToSend(aCurrentFile))
ret.add(aCurrentFile);
return ret;
}
use of com.helger.as2lib.params.AS2InvalidParameterException in project as2-lib by phax.
the class AbstractDirectoryPollingModule method scanDirectory.
protected void scanDirectory(final String sDirectory) throws AS2InvalidParameterException {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Polling - scanning directory '" + sDirectory + "'");
final File aDir = AS2IOHelper.getDirectoryFile(sDirectory);
// get a list of entries in the directory
final File[] aFiles = aDir.listFiles();
if (aFiles == null) {
throw new AS2InvalidParameterException("Error getting list of files in directory", this, ATTR_OUTBOX_DIRECTORY, aDir.getAbsolutePath());
}
// iterator through each entry, and start tracking new files
if (aFiles.length > 0)
for (final File aCurrentFile : aFiles) if (checkFile(aCurrentFile)) {
// start watching the file's size if it's not already being watched
trackFile(aCurrentFile);
}
}
use of com.helger.as2lib.params.AS2InvalidParameterException 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