From 05fd2f8800b617fb9c194b7475da32214d069ffb Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Fri, 11 Mar 2016 21:42:56 +0000 Subject: [PATCH] Merged 5.1.N (5.1.1) to HEAD (5.1) 121796 rneamtu: Merged 5.0.N (5.0.4) to 5.1.N (5.1.1) 121764 nsmintanca: Merged V4.2-BUG-FIX (4.2.7) to 5.0.N (5.0.4) 121709 rneamtu: Merged DEV to V4.2-BUG-FIX (4.2.7) 121670 rneamtu: MNT-13814 : Using MS word Save As pdf does not work correctly. - Added new mimetype "application/applefile" and set it for files that starts with "._" - Added new test for case git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@123651 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/content/AbstractContentWriter.java | 15 +- .../content/ContentFullContextTestSuite.java | 3 +- .../repo/content/GuessMimetypeTest.java | 136 ++++++++++++++++++ 3 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 source/test-java/org/alfresco/repo/content/GuessMimetypeTest.java diff --git a/source/java/org/alfresco/repo/content/AbstractContentWriter.java b/source/java/org/alfresco/repo/content/AbstractContentWriter.java index b9ab6e8ac7..b49f195d9d 100644 --- a/source/java/org/alfresco/repo/content/AbstractContentWriter.java +++ b/source/java/org/alfresco/repo/content/AbstractContentWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2016 Alfresco Software Limited. * * This file is part of Alfresco * @@ -613,11 +613,18 @@ public abstract class AbstractContentWriter extends AbstractContentAccessor impl guessingOnCloseListener.filename = filename; } } + private void doGuessMimetype(String filename) { - String mimetype = mimetypeService.guessMimetype( - filename, getReader() - ); + String mimetype; + if (filename.startsWith(MimetypeMap.MACOS_RESOURCE_FORK_FILE_NAME_PREFIX)) + { + mimetype = MimetypeMap.MIMETYPE_APPLEFILE; + } + else + { + mimetype = mimetypeService.guessMimetype(filename, getReader()); + } setMimetype(mimetype); } diff --git a/source/test-java/org/alfresco/repo/content/ContentFullContextTestSuite.java b/source/test-java/org/alfresco/repo/content/ContentFullContextTestSuite.java index d4cec1fdc9..78ee8d134c 100644 --- a/source/test-java/org/alfresco/repo/content/ContentFullContextTestSuite.java +++ b/source/test-java/org/alfresco/repo/content/ContentFullContextTestSuite.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2016 Alfresco Software Limited. * * This file is part of Alfresco * @@ -58,6 +58,7 @@ public class ContentFullContextTestSuite extends TestSuite //suite.addTestSuite(MimetypeMapTest.class); suite.addTestSuite(RoutingContentServiceTest.class); suite.addTest(new JUnit4TestAdapter(RoutingContentStoreTest.class)); + suite.addTestSuite(GuessMimetypeTest.class); try { diff --git a/source/test-java/org/alfresco/repo/content/GuessMimetypeTest.java b/source/test-java/org/alfresco/repo/content/GuessMimetypeTest.java new file mode 100644 index 0000000000..467ad740f9 --- /dev/null +++ b/source/test-java/org/alfresco/repo/content/GuessMimetypeTest.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.content; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.alfresco.model.ContentModel; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.transaction.RetryingTransactionHelper; +import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; +import org.alfresco.service.cmr.repository.ContentService; +import org.alfresco.service.cmr.repository.ContentWriter; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.namespace.QName; +import org.alfresco.util.ApplicationContextHelper; +import org.springframework.context.ApplicationContext; + +/** + * Tests for guess mimetype for file + * + * This includes a test for apple specific hidden files + * + * @author rneamtu + */ + +public class GuessMimetypeTest extends TestCase +{ + private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); + + private NodeService nodeService; + private ContentService contentService; + private RetryingTransactionHelper retryingTransactionHelper; + private StoreRef storeRef; + private NodeRef rootNodeRef; + private NodeRef nodeRef; + + @Override + public void setUp() throws Exception + { + this.nodeService = (NodeService) ctx.getBean("nodeService"); + this.contentService = (ContentService) ctx.getBean("ContentService"); + + this.retryingTransactionHelper = (RetryingTransactionHelper) ctx.getBean("retryingTransactionHelper"); + + retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback() + { + @Override + public Object execute() throws Throwable + { + // As system user + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); + + storeRef = StoreRef.STORE_REF_WORKSPACE_SPACESSTORE; + + rootNodeRef = nodeService.getRootNode(storeRef); + + return null; + } + }); + } + + public void testAppleMimetype() throws Exception + { + String content = "This is some content"; + String fileName = "._myfile.pdf"; + retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback() + { + @Override + public Object execute() throws Throwable + { + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); + + Map properties = new HashMap(13); + properties.put(ContentModel.PROP_NAME, (Serializable) "test.txt"); + + nodeRef = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}testnode"), + ContentModel.TYPE_CONTENT).getChildRef(); + return null; + } + }); + + ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); + + writer.putContent(content); + writer.guessMimetype(fileName); + + assertEquals(MimetypeMap.MIMETYPE_APPLEFILE, writer.getMimetype()); + + fileName = "myfile.pdf"; + retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback() + { + @Override + public Object execute() throws Throwable + { + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); + + Map properties = new HashMap(13); + properties.put(ContentModel.PROP_NAME, (Serializable) "test.txt"); + + nodeRef = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}testnode"), + ContentModel.TYPE_CONTENT).getChildRef(); + return null; + } + }); + + ContentWriter writer2 = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); + content = "This is other content"; + writer2.putContent(content); + writer2.guessMimetype(fileName); + + assertNotSame(MimetypeMap.MIMETYPE_APPLEFILE, writer2.getMimetype()); + + } +}