parts = new Part[msg.getOutboundAttachmentNames().size()];
}
else
{
parts = new Part[msg.getOutboundAttachmentNames().size() + 1];
parts[i++] = new FilePart("payload", new ByteArrayPartSource("payload", msg.getPayloadAsBytes()));
}
for (final Iterator<String> iterator = msg.getOutboundAttachmentNames().iterator(); iterator.hasNext(); i++)
{
final String attachmentNames = iterator.next();
String fileName = attachmentNames;
final DataHandler dh = msg.getOutboundAttachment(attachmentNames);
if (dh.getDataSource() instanceof StringDataSource)
{
final StringDataSource ds = (StringDataSource) dh.getDataSource();
parts[i] = new StringPart(ds.getName(), IOUtils.toString(ds.getInputStream()));
}
else
{
if (dh.getDataSource() instanceof FileDataSource)
{
fileName = ((FileDataSource) dh.getDataSource()).getFile().getName();
}
else if (dh.getDataSource() instanceof URLDataSource)
{
fileName = ((URLDataSource) dh.getDataSource()).getURL().getFile();
// Don't use the whole file path, just the file name
final int x = fileName.lastIndexOf("/");
if (x > -1)
{
fileName = fileName.substring(x + 1);
}
}
parts[i] = new FilePart(fileName, new ByteArrayPartSource(fileName,
IOUtils.toByteArray(dh.getInputStream())), dh.getContentType(), null);
}
}
return new MultipartRequestEntity(parts, method.getParams());