mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Merged HEAD-QA to HEAD (4.2) (including moving test classes into separate folders)
51903 to 54309 git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54310 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
*/
|
||||
package org.alfresco.repo.imap;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
@@ -32,16 +34,20 @@ import javax.mail.internet.MimeUtility;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.model.ImapModel;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.model.FileFolderService;
|
||||
import org.alfresco.service.cmr.model.FileInfo;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.MimetypeService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.util.config.RepositoryFolderConfigBean;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.poi.hmef.HMEFMessage;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
@@ -76,6 +82,7 @@ public class AttachmentsExtractor
|
||||
private RepositoryFolderConfigBean attachmentsFolder;
|
||||
private NodeRef attachmentsFolderRef;
|
||||
private AttachmentsExtractorMode attachmentsExtractorMode;
|
||||
private MimetypeService mimetypeService;
|
||||
|
||||
public void setFileFolderService(FileFolderService fileFolderService)
|
||||
{
|
||||
@@ -107,15 +114,32 @@ public class AttachmentsExtractor
|
||||
this.attachmentsExtractorMode = AttachmentsExtractorMode.valueOf(attachmentsExtractorMode);
|
||||
}
|
||||
|
||||
public void setMimetypeService(MimetypeService mimetypeService)
|
||||
{
|
||||
this.mimetypeService = mimetypeService;
|
||||
}
|
||||
|
||||
public void init()
|
||||
{
|
||||
attachmentsFolderRef = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>()
|
||||
{
|
||||
public NodeRef doWork() throws Exception
|
||||
{
|
||||
NodeRef attFolderRef = attachmentsFolder.getOrCreateFolderPath(serviceRegistry.getNamespaceService(), nodeService, serviceRegistry.getSearchService(), fileFolderService);
|
||||
serviceRegistry.getPermissionService().setPermission(attFolderRef , PermissionService.ALL_AUTHORITIES, PermissionService.FULL_CONTROL, true);
|
||||
return attFolderRef;
|
||||
RetryingTransactionHelper helper = serviceRegistry.getTransactionService().getRetryingTransactionHelper();
|
||||
helper.setForceWritable(true);
|
||||
RetryingTransactionCallback<NodeRef> getDescriptorCallback = new RetryingTransactionCallback<NodeRef>()
|
||||
{
|
||||
public NodeRef execute()
|
||||
{
|
||||
NodeRef attFolderRef = attachmentsFolder.getOrCreateFolderPath(serviceRegistry.getNamespaceService(), nodeService, serviceRegistry.getSearchService(), fileFolderService);
|
||||
if (attachmentsExtractorMode!=null && attachmentsExtractorMode==AttachmentsExtractorMode.COMMON)
|
||||
{
|
||||
serviceRegistry.getPermissionService().setPermission(attFolderRef , PermissionService.ALL_AUTHORITIES, PermissionService.FULL_CONTROL, true);
|
||||
}
|
||||
return attFolderRef;
|
||||
}
|
||||
};
|
||||
return helper.doInTransaction(getDescriptorCallback, false, false);
|
||||
}
|
||||
}, AuthenticationUtil.getSystemUserName());
|
||||
}
|
||||
@@ -123,6 +147,8 @@ public class AttachmentsExtractor
|
||||
public void extractAttachments(NodeRef messageRef, MimeMessage originalMessage) throws IOException, MessagingException
|
||||
{
|
||||
NodeRef attachmentsFolderRef = null;
|
||||
String attachmentsFolderName = null;
|
||||
boolean createFolder = false;
|
||||
switch (attachmentsExtractorMode)
|
||||
{
|
||||
case SAME:
|
||||
@@ -133,14 +159,15 @@ public class AttachmentsExtractor
|
||||
break;
|
||||
case SEPARATE:
|
||||
default:
|
||||
NodeRef parentFolder = nodeService.getPrimaryParent(messageRef).getParentRef();
|
||||
String messageName = (String) nodeService.getProperty(messageRef, ContentModel.PROP_NAME);
|
||||
String attachmentsFolderName = messageName + "-attachments";
|
||||
attachmentsFolderRef = fileFolderService.create(parentFolder, attachmentsFolderName, ContentModel.TYPE_FOLDER).getNodeRef();
|
||||
attachmentsFolderName = messageName + "-attachments";
|
||||
createFolder = true;
|
||||
break;
|
||||
}
|
||||
|
||||
nodeService.createAssociation(messageRef, attachmentsFolderRef, ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
|
||||
if (!createFolder)
|
||||
{
|
||||
nodeService.createAssociation(messageRef, attachmentsFolderRef, ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
|
||||
}
|
||||
|
||||
Object content = originalMessage.getContent();
|
||||
if (content instanceof Multipart)
|
||||
@@ -152,6 +179,11 @@ public class AttachmentsExtractor
|
||||
Part part = multipart.getBodyPart(i);
|
||||
if ("attachment".equalsIgnoreCase(part.getDisposition()))
|
||||
{
|
||||
if (createFolder)
|
||||
{
|
||||
attachmentsFolderRef = createAttachmentFolder(messageRef, attachmentsFolderName);
|
||||
createFolder = false;
|
||||
}
|
||||
createAttachment(messageRef, attachmentsFolderRef, part);
|
||||
}
|
||||
}
|
||||
@@ -159,6 +191,24 @@ public class AttachmentsExtractor
|
||||
|
||||
}
|
||||
|
||||
private NodeRef createAttachmentFolder(NodeRef messageRef, String attachmentsFolderName)
|
||||
{
|
||||
NodeRef attachmentsFolderRef = null;
|
||||
NodeRef parentFolder = nodeService.getPrimaryParent(messageRef).getParentRef();
|
||||
attachmentsFolderRef = fileFolderService.create(parentFolder, attachmentsFolderName, ContentModel.TYPE_FOLDER).getNodeRef();
|
||||
nodeService.createAssociation(messageRef, attachmentsFolderRef, ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
|
||||
return attachmentsFolderRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an attachment given a mime part
|
||||
*
|
||||
* @param messageFile the file containing the message
|
||||
* @param destinationFolder where to put the attachment
|
||||
* @param part the mime part
|
||||
* @throws MessagingException
|
||||
* @throws IOException
|
||||
*/
|
||||
private void createAttachment(NodeRef messageFile, NodeRef attachmentsFolderRef, Part part) throws MessagingException, IOException
|
||||
{
|
||||
String fileName = part.getFileName();
|
||||
@@ -179,42 +229,79 @@ public class AttachmentsExtractor
|
||||
}
|
||||
|
||||
ContentType contentType = new ContentType(part.getContentType());
|
||||
NodeRef attachmentFile = fileFolderService.searchSimple(attachmentsFolderRef, fileName);
|
||||
// The one possible behaviour
|
||||
/*
|
||||
if (result.size() > 0)
|
||||
|
||||
if (contentType.getBaseType().equalsIgnoreCase("application/ms-tnef"))
|
||||
{
|
||||
for (FileInfo fi : result)
|
||||
// The content is TNEF
|
||||
HMEFMessage hmef = new HMEFMessage(part.getInputStream());
|
||||
|
||||
// hmef.getBody();
|
||||
List<org.apache.poi.hmef.Attachment> attachments = hmef.getAttachments();
|
||||
for (org.apache.poi.hmef.Attachment attachment : attachments)
|
||||
{
|
||||
fileFolderService.delete(fi.getNodeRef());
|
||||
String subName = attachment.getLongFilename();
|
||||
|
||||
NodeRef attachmentNode = fileFolderService.searchSimple(attachmentsFolderRef, subName);
|
||||
if (attachmentNode == null)
|
||||
{
|
||||
/*
|
||||
* If the node with the given name does not already exist Create the content node to contain the attachment
|
||||
*/
|
||||
FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, subName, ContentModel.TYPE_CONTENT);
|
||||
|
||||
attachmentNode = createdFile.getNodeRef();
|
||||
|
||||
serviceRegistry.getNodeService().createAssociation(messageFile, attachmentNode, ImapModel.ASSOC_IMAP_ATTACHMENT);
|
||||
|
||||
byte[] bytes = attachment.getContents();
|
||||
ContentWriter writer = fileFolderService.getWriter(attachmentNode);
|
||||
|
||||
// TODO ENCODING - attachment.getAttribute(TNEFProperty.);
|
||||
String extension = attachment.getExtension();
|
||||
String mimetype = mimetypeService.getMimetype(extension);
|
||||
if (mimetype != null)
|
||||
{
|
||||
writer.setMimetype(mimetype);
|
||||
}
|
||||
|
||||
OutputStream os = writer.getContentOutputStream();
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
FileCopyUtils.copy(is, os);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
// And another one behaviour which will overwrite the content of the existing file. It is performance preferable.
|
||||
if (attachmentFile == null)
|
||||
{
|
||||
FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, fileName, ContentModel.TYPE_CONTENT);
|
||||
nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
|
||||
attachmentFile = createdFile.getNodeRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
else
|
||||
{
|
||||
// not TNEF
|
||||
NodeRef attachmentFile = fileFolderService.searchSimple(attachmentsFolderRef, fileName);
|
||||
// The one possible behaviour
|
||||
/*
|
||||
* if (result.size() > 0) { for (FileInfo fi : result) { fileFolderService.delete(fi.getNodeRef()); } }
|
||||
*/
|
||||
// And another one behaviour which will overwrite the content of the existing file. It is performance preferable.
|
||||
if (attachmentFile == null)
|
||||
{
|
||||
FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, fileName, ContentModel.TYPE_CONTENT);
|
||||
nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
|
||||
attachmentFile = createdFile.getNodeRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
String newFileName = imapService.generateUniqueFilename(attachmentsFolderRef, fileName);
|
||||
|
||||
String newFileName = imapService.generateUniqueFilename(attachmentsFolderRef, fileName);
|
||||
|
||||
FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, newFileName, ContentModel.TYPE_CONTENT);
|
||||
nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
|
||||
attachmentFile = createdFile.getNodeRef();
|
||||
FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, newFileName, ContentModel.TYPE_CONTENT);
|
||||
nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
|
||||
attachmentFile = createdFile.getNodeRef();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
nodeService.setProperty(attachmentFile, ContentModel.PROP_DESCRIPTION, nodeService.getProperty(messageFile, ContentModel.PROP_NAME));
|
||||
|
||||
ContentWriter writer = fileFolderService.getWriter(attachmentFile);
|
||||
writer.setMimetype(contentType.getBaseType());
|
||||
OutputStream os = writer.getContentOutputStream();
|
||||
FileCopyUtils.copy(part.getInputStream(), os);
|
||||
}
|
||||
nodeService.setProperty(attachmentFile, ContentModel.PROP_DESCRIPTION, nodeService.getProperty(messageFile, ContentModel.PROP_NAME));
|
||||
|
||||
ContentWriter writer = fileFolderService.getWriter(attachmentFile);
|
||||
writer.setMimetype(contentType.getBaseType());
|
||||
OutputStream os = writer.getContentOutputStream();
|
||||
FileCopyUtils.copy(part.getInputStream(), os);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user