Merged DEV/SWIFT to HEAD (FTP Tests, Tika and Poi)

26059: ALF-5900 - IMAP creates winmail.dat in attachment folder (Add support for Microsoft Transport Neutral Encapsulation Format.)
          - added attachment extraction for TNEF documents - goodbye winmail.dat !
   26063: javadoc for imap.
   26088: ALF-7408 - addition of commons-net for ftp client library.
          First test of end to end ftp.   Just a simple test of connection now, will be followed by more detailed tests.
   26176: ALF-7408 - FTP tests + disabled failing test case for ALF-7618
   26180: ALF-7618 - correction of unit test error.
   26188: ALF-7618 - added a test of paths
   26229: Added back simple '\~.*' pattern
   26288: ALF-7676 - Test to stress different user rights.
          - FTPServerTest.testTwoUserUpdate added for the FTP server.
   26304: Corrected spelling name in private class.
   26408: addming minimal package infos.
   26416: ALF-5082 / ALF-2183 / ALF-4448 - When guessing the mimetype for a file, add the option to supply a ContentReader to enhance the accuracy.
          Enable this for a few key places that do mimetype guessing,
          which should avoid issues for files with the wrong extension (either renamed accidently, or for .TMP)
   26433: Re-order the mimetype guess step to ensure that the Content Reader is always valid
   26440: Added another test for word 2003 save as.
   26441: Test resource for ContentDiskDriver
   26446: ALF-5082 - Back out a FileFolderService change to mimetype guessing, which had broken things, pending a better way to do it with ContentWriter
   26490: Small change for ContentDiskDriverTes.fileExists.  Leaky transaction causing problems in automated build.
   26497: ContentDiskDriver - commented out two of the problematic leaky transaction tests.
   26503: Add new interface methods + documentation for asking a ContentWriter to guess the mimetype and encoding for you.
          (Code will be migrated from places that currently do this themselves later)
   26504: Add an extension interface in the DataModel project for some of the extra ContentReader methods that FileContentReader provides
   26505: When ContentWriter.putContent(String) is called with no encoding specified, record what the system default encoding was that was used.
          (Prevents issues if the system default is ever changed)
   26509: When calling Tika to do file detection, if we have a file based reader then give Tika the File rather than an InputStream
   26522: More debug logging while debugging ALF-5260
   26546: Have one copy of the Tika Config in spring, rather than several places fetching their own copy of the default one (either explicitly or implicitly).
   26522: More debug logging while diagnosing ALF-5260
   26548: Add another mimetype check - ensures that truncated/corrup container files which can't be fully processed can still get the container type without failure
   26549: Implement the mimetype and encoding guessers on ContentWriter (either immediately or as a listener, as required), and update FileFolderServer to make use of this (+test this)
   26553: Replace explicit mimetype and encoding guess calls with ContentWriter requests to have the work done
   26554: Replace explicit mimetype and encoding guess calls with ContentWriter requests to have the work done
   26579: Switch the transformer to use Tika

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28224 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2011-06-07 07:36:37 +00:00
parent 04aef409a8
commit e118211bd3
50 changed files with 2269 additions and 365 deletions

View File

@@ -20,7 +20,11 @@ package org.alfresco.repo.imap;
import static org.alfresco.repo.imap.AlfrescoImapConst.DICTIONARY_TEMPLATE_PREFIX;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@@ -34,6 +38,12 @@ import java.util.Set;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
@@ -64,7 +74,9 @@ import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.model.SubFolderFilter;
import org.alfresco.service.cmr.preference.PreferenceService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
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.repository.StoreRef;
@@ -80,9 +92,11 @@ import org.alfresco.util.Utf7;
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.context.ApplicationEvent;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
import org.springframework.extensions.surf.util.I18NUtil;
import org.springframework.util.FileCopyUtils;
/**
* @author Dmitry Vaserin
@@ -108,6 +122,7 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
private PermissionService permissionService;
private ServiceRegistry serviceRegistry;
private BehaviourFilter policyBehaviourFilter;
private MimetypeService mimetypeService;
/**
* Folders cache
@@ -201,11 +216,6 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
this.foldersCache = foldersCache;
}
public SimpleCache<Serializable, Object> getFoldersCache()
{
return foldersCache;
}
public FileFolderService getFileFolderService()
{
return fileFolderService;
@@ -216,9 +226,9 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
this.fileFolderService = fileFolderService;
}
public NodeService getNodeService()
public void setMimetypeService(MimetypeService mimetypeService)
{
return nodeService;
this.mimetypeService = mimetypeService;
}
public void setNodeService(NodeService nodeService)
@@ -226,21 +236,11 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
this.nodeService = nodeService;
}
public PermissionService getPermissionService()
{
return permissionService;
}
public void setPermissionService(PermissionService permissionService)
{
this.permissionService = permissionService;
}
public ServiceRegistry getServiceRegistry()
{
return serviceRegistry;
}
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
@@ -320,6 +320,7 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
PropertyCheck.mandatory(this, "defaultFromAddress", defaultFromAddress);
PropertyCheck.mandatory(this, "repositoryTemplatePath", repositoryTemplatePath);
PropertyCheck.mandatory(this, "policyBehaviourFilter", policyBehaviourFilter);
PropertyCheck.mandatory(this, "mimetypeService", mimetypeService);
}
public void startup()
@@ -900,7 +901,7 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
{
if (logger.isDebugEnabled())
{
logger.debug("[searchByPattern] Start. nodeRef=" + contextNodeRef + ", namePattern=" + namePattern);
logger.debug("[searchByPattern] Start. nodeRef=" + contextNodeRef + ", viewMode=" + viewMode + " namePattern=" + namePattern);
}
List<FileInfo> searchResult;
@@ -913,13 +914,13 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
/**
* This is a simple listing of all folders below contextNodeRef
*/
logger.debug("call file folder service to list folders");
searchResult = fileFolderService.listFolders(contextNodeRef);
}
else
{
// MER TODO I'm not sure we ever get here in real use of IMAP. But if we do then the use of this
// deprecated method needs to be re-worked.
// searchResult = fileFolderService.search(contextNodeRef, namePattern, false, true, false);
logger.debug("call listDeepFolders");
searchResult = fileFolderService.listDeepFolders(contextNodeRef, new ImapSubFolderFilter(viewMode, namePattern));
}
@@ -1000,7 +1001,10 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
if (logger.isDebugEnabled())
{
logger.debug("[searchByPattern] End. namePattern=" + namePattern);
if (logger.isDebugEnabled())
{
logger.debug("[searchByPattern] End. nodeRef=" + contextNodeRef + ", viewMode=" + viewMode + ", namePattern=" + namePattern + ", searchResult=" +searchResult.size());
}
}
return searchResult;
@@ -1271,7 +1275,7 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
{
if (logger.isDebugEnabled())
{
logger.debug("List folder: mailboxPattern=" + mailboxPattern);
logger.debug("expand folder: root:" + root + " user: " + user + " :mailboxPattern=" + mailboxPattern);
}
if (mailboxPattern == null)
return null;
@@ -2015,7 +2019,7 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
ImapSubFolderFilter(ImapViewMode imapViewMode)
{
this.imapViewMode = imapViewMode;
this.typesToExclude = getServiceRegistry().getDictionaryService().getSubTypes(SiteModel.TYPE_SITE, true);
this.typesToExclude = serviceRegistry.getDictionaryService().getSubTypes(SiteModel.TYPE_SITE, true);
this.favs = getFavouriteSites(getCurrentUser());
}
@@ -2192,5 +2196,173 @@ public class ImapServiceImpl implements ImapService, OnCreateChildAssociationPol
{
return nodeService.getType(parent).equals(SiteModel.TYPE_SITE) && isInDocLibrary;
}
}
}
/**
* Extract attachments from a MimeMessage
*
* Puts the attachments into a subfolder below the parent folder.
*
* @return the node ref of the folder containing the attachments or null if there are no
* attachments.
*/
public NodeRef extractAttachments(
NodeRef parentFolder,
NodeRef messageFile,
MimeMessage originalMessage)
throws IOException, MessagingException
{
String messageName = (String)nodeService.getProperty(messageFile, ContentModel.PROP_NAME);
String attachmentsFolderName = messageName + "-attachments";
FileInfo attachmentsFolderFileInfo = null;
Object content = originalMessage.getContent();
if (content instanceof Multipart)
{
Multipart multipart = (Multipart) content;
for (int i = 0, n = multipart.getCount(); i < n; i++)
{
Part part = multipart.getBodyPart(i);
if ("attachment".equalsIgnoreCase(part.getDisposition()))
{
if (attachmentsFolderFileInfo == null)
{
attachmentsFolderFileInfo = fileFolderService.create(
parentFolder,
attachmentsFolderName,
ContentModel.TYPE_FOLDER);
nodeService.createAssociation(
messageFile,
attachmentsFolderFileInfo.getNodeRef(),
ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
}
createAttachment(messageFile, attachmentsFolderFileInfo.getNodeRef(), part);
}
}
}
if(attachmentsFolderFileInfo != null)
{
return attachmentsFolderFileInfo.getNodeRef();
}
else
{
return null;
}
}
/**
* 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 destinationFolder, Part part) throws MessagingException, IOException
{
String fileName = part.getFileName();
try
{
fileName = MimeUtility.decodeText(fileName);
}
catch (UnsupportedEncodingException e)
{
if (logger.isWarnEnabled())
{
logger.warn("Cannot decode file name '" + fileName + "'", e);
}
}
ContentType contentType = new ContentType(part.getContentType());
if(contentType.getBaseType().equalsIgnoreCase("application/ms-tnef"))
{
// 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)
{
String subName = attachment.getLongFilename();
NodeRef attachmentNode = fileFolderService.searchSimple(destinationFolder, 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(
destinationFolder,
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);
}
}
}
else
{
// not TNEF
NodeRef attachmentNode = fileFolderService.searchSimple(destinationFolder, fileName);
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(
destinationFolder,
fileName,
ContentModel.TYPE_CONTENT);
attachmentNode = createdFile.getNodeRef();
serviceRegistry.getNodeService().createAssociation(
messageFile,
attachmentNode,
ImapModel.ASSOC_IMAP_ATTACHMENT);
// the part is a normal IMAP attachment
ContentWriter writer = fileFolderService.getWriter(attachmentNode);
writer.setMimetype(contentType.getBaseType());
String charset = contentType.getParameter("charset");
if(charset != null)
{
writer.setEncoding(charset);
}
OutputStream os = writer.getContentOutputStream();
FileCopyUtils.copy(part.getInputStream(), os);
}
}
}
}