* @return The number of attachments added for this message.
*/
private int saveAttachments(Object messageContent, int messageID, int accountID, int ownerID, int attachmentFolderID, CVDal cvdal, HashMap embededImageMap)
{
int numberOfAttachments = 0;
CvFileFacade fileFacade = new CvFileFacade();
try {
if (messageContent instanceof Multipart) {
// we only process the attachments from Multipart messages
Multipart multipart = (Multipart) messageContent;
String insertQuery = "INSERT INTO attachment (MessageID, FileName, FileID) VALUES(?, ?, ?)";
cvdal.setSqlQuery(insertQuery);
for (int i = 0; i < multipart.getCount(); i++) {
// loop through the parts, processing each one...
Part part = (Part) multipart.getBodyPart(i);
String contentType = part.getContentType();
String disposition = part.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
// by SPEC, we should only save those parts whose disposition is "attachment" or "inline".
// disposition == null means that it is a content part (message body, readable content)
CvFileVO attachment = new CvFileVO();
String filename = (part.getFileName() != null) ? (String)part.getFileName() : "Part-" + i;
// prepend human readable date to the fileName. This is for uniqueness.
SimpleDateFormat df = new SimpleDateFormat("MMMM_dd_yyyy_hh_mm_ss_S");
String prependDate = df.format(new Date());
attachment.setName(prependDate + "-" + filename);
attachment.setTitle(filename);
attachment.setOwner(ownerID);
attachment.setAuthorId(ownerID);
attachment.setCreatedBy(ownerID);
attachment.setDescription("Email Attachment: " + filename);
attachment.setPhysical(CvFileVO.FP_PHYSICAL);
if (attachment.getTitle() == null || (attachment.getTitle()).length() <= 0) {
// this is not good, and we should never reach this point, because I
// have added some fail-proof default content for filename above
return -1;
}
int newFileID = fileFacade.addFile(ownerID, attachmentFolderID, attachment, part.getInputStream(), this.dataSource);
if (newFileID > 0) {
cvdal.clearParameters();
cvdal.setInt(1, messageID);
cvdal.setString(2, attachment.getTitle());
cvdal.setInt(3, newFileID);
int rowsAffected = cvdal.executeUpdate();
}
numberOfAttachments++;
}else if(contentType != null && contentType.toLowerCase().indexOf("rfc822") > -1){
// The attachment is itself an rfc822 message. Therefore, we need to
// create a new MailMessage object, and process all its parts too
this.saveAttachments(this.getPartContent(part), messageID, accountID, ownerID, attachmentFolderID, cvdal, embededImageMap);
} else if (contentType != null && (contentType.length() >= 10) && ((contentType.toLowerCase().substring(0, 10)).indexOf("image") != -1)) {
// The attachment is itself an rfc2387 message. Therefore, we need to
// create a new MailMessage object, and process all the embeded image parts too
CvFileVO attachment = new CvFileVO();
String contentID = null;
String partContentID[] = part.getHeader("Content-ID");
if(partContentID != null && partContentID.length != 0){
contentID = partContentID[0];
}
String filename = (part.getFileName() != null) ? (String)part.getFileName() : "Part-" + i;
// prepend human readable date to the fileName. This is for uniqueness.
SimpleDateFormat df = new SimpleDateFormat("MMMM_dd_yyyy_hh_mm_ss_S");
String prependDate = df.format(new Date());
String storedFileName = prependDate + "-" + filename;
attachment.setName(storedFileName);
attachment.setTitle(filename);
attachment.setOwner(ownerID);
attachment.setAuthorId(ownerID);
attachment.setCreatedBy(ownerID);
attachment.setDescription("Email Attachment: " + filename);
attachment.setPhysical(CvFileVO.FP_PHYSICAL);
if (attachment.getTitle() == null || (attachment.getTitle()).length() <= 0) {
// this is not good, and we should never reach this point, because I
// have added some fail-proof default content for filename above
return -1;
}
int newFileID = fileFacade.addFile(ownerID, attachmentFolderID, attachment, part.getInputStream(), this.dataSource);
if (newFileID > 0) {
//According to rfc. Some email client will not add the less than and greather than in the content-ID
//We will replace lessthen and greaterthen with blank
contentID = contentID.replaceAll("<","");