throws RepositoryException {
final String METHOD = "createAttachmentDoc";
String AttachmentURL = null;
LOGGER.entering(CLASS_NAME, METHOD);
NotesEmbeddedObject eo = null;
NotesDocument attachDoc = null;
try {
// Error access the attachment
eo = srcDoc.getAttachment(AttachmentName);
if (eo == null) {
LOGGER.log(Level.FINER, "Attachment could not be accessed {0}",
AttachmentName);
return null;
}
if (eo.getType() != NotesEmbeddedObject.EMBED_ATTACHMENT) {
// The object is not an attachment - could be an OLE object or link
LOGGER.logp(Level.FINER, CLASS_NAME, METHOD,
"Ignoring embedded object " + AttachmentName);
eo.recycle();
return null;
}
// Don't send attachments larger than the limit
if (eo.getFileSize() > ncs.getMaxFileSize()) {
LOGGER.logp(Level.FINER, CLASS_NAME, METHOD,
"Attachment larger than the configured limit and content " +
"will not be sent. " + AttachmentName);
}
attachDoc = cdb.createDocument();
crawlDoc.copyAllItems(attachDoc, true);
// Store the filename of this attachment in the attachment crawl doc.
attachDoc.replaceItemValue(NCCONST.ITM_GMETAATTACHMENTFILENAME,
AttachmentName);
attachDoc.save();
// Compute display URL
String encodedAttachmentName = null;
try {
encodedAttachmentName = URLEncoder.encode(AttachmentName, "UTF-8");
} catch (Exception e) {
attachDoc.recycle();
eo.recycle();
return null;
}
AttachmentURL = String.format(NCCONST.SITM_ATTACHMENTDISPLAYURL,
getHTTPURL(crawlDoc), encodedAttachmentName);
attachDoc.replaceItemValue(NCCONST.ITM_DISPLAYURL, AttachmentURL);
LOGGER.log(Level.FINEST, "Attachment display url: {0}", AttachmentURL);
// Compute docid
String attachNameHash = Util.hash(AttachmentName);
if (attachNameHash == null) {
return null;
}
String docURL = String.format(NCCONST.SITM_ATTACHMENTDOCID,
getHTTPURL(crawlDoc), attachNameHash);
attachDoc.replaceItemValue(NCCONST.ITM_DOCID, docURL);
LOGGER.log(Level.FINEST, "Attachment document docid: {0}", docURL);
// Only if we have a supported mime type and file size is not exceeding
// the limit do we send the content, or only metadata and file name will
// be sent.
if ((0 != MimeType.length()) &&
eo.getFileSize() <= ncs.getMaxFileSize()) {
attachDoc.replaceItemValue(NCCONST.ITM_MIMETYPE, MimeType);
String attachmentPath = getAttachmentFilePath(crawlDoc, attachNameHash);
eo.extractFile(attachmentPath);
attachDoc.replaceItemValue(NCCONST.ITM_CONTENTPATH, attachmentPath);
} else {
// Not a supported attachment so sending meta data only
// with the filename as content
attachDoc.replaceItemValue(NCCONST.ITM_CONTENT, AttachmentName);
attachDoc.replaceItemValue(NCCONST.ITM_MIMETYPE,
NCCONST.DEFAULT_MIMETYPE);
}
eo.recycle();
// Set the state of this document to be fetched
attachDoc.replaceItemValue(NCCONST.ITM_ACTION, ActionType.ADD.toString());
attachDoc.replaceItemValue(NCCONST.NCITM_STATE, NCCONST.STATEFETCHED);
attachDoc.save();
attachDoc.recycle();
LOGGER.exiting(CLASS_NAME, METHOD);
return attachNameHash;
} catch (Exception e) {
LOGGER.logp(Level.SEVERE, CLASS_NAME, METHOD,
"Error pre-fetching attachment: " + AttachmentName +
" in document: " + srcDoc.getNotesURL(), e);
if (null != eo) {
eo.recycle();
}
if (null != attachDoc) {
attachDoc.replaceItemValue(NCCONST.NCITM_STATE, NCCONST.STATEERROR);
attachDoc.save();
attachDoc.recycle();
}
return null;
}
}