diff --git a/source/java/org/alfresco/repo/content/caching/CachingContentStore.java b/source/java/org/alfresco/repo/content/caching/CachingContentStore.java index 25c0c87101..9b5efc3bf1 100644 --- a/source/java/org/alfresco/repo/content/caching/CachingContentStore.java +++ b/source/java/org/alfresco/repo/content/caching/CachingContentStore.java @@ -273,9 +273,16 @@ public class CachingContentStore implements ContentStore, ApplicationEventPublis bsWriter.setLocale(cacheWriter.getLocale()); bsWriter.setMimetype(cacheWriter.getMimetype()); bsWriter.putContent(cacheWriter.getReader()); + boolean contentUrlChanged = !url.equals(bsWriter.getContentUrl()); - if (!quota.afterWritingCacheFile(cacheWriter.getSize())) + // MNT-11758 fix, re-cache files for which content url has changed after write to backing store (e.g. XAM, Centera) + if (!quota.afterWritingCacheFile(cacheWriter.getSize()) || contentUrlChanged) { + if (contentUrlChanged) + { + // MNT-11758 fix, cache file with new and correct contentUrl after write operation to backing store completed + cache.put(bsWriter.getContentUrl(), cacheWriter.getReader()); + } // Quota manager has requested that the new cache file is not kept. cache.deleteFile(url); cache.remove(url); diff --git a/source/test-java/org/alfresco/repo/content/caching/CachingContentStoreSpringTest.java b/source/test-java/org/alfresco/repo/content/caching/CachingContentStoreSpringTest.java index 49718663cd..4acc725d49 100644 --- a/source/test-java/org/alfresco/repo/content/caching/CachingContentStoreSpringTest.java +++ b/source/test-java/org/alfresco/repo/content/caching/CachingContentStoreSpringTest.java @@ -34,6 +34,7 @@ import org.alfresco.repo.content.ContentContext; import org.alfresco.repo.content.ContentStore; import org.alfresco.repo.content.caching.quota.QuotaManagerStrategy; import org.alfresco.repo.content.filestore.FileContentStore; +import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.test_category.OwnJVMTestsCategory; import org.alfresco.util.TempFileProvider; @@ -54,6 +55,7 @@ public class CachingContentStoreSpringTest extends AbstractWritableContentStoreT { private CachingContentStore store; private FileContentStore backingStore; + private TestCenteraLikeContentStore centeraLikeBackingStore; private ContentCacheImpl cache; @@ -194,6 +196,46 @@ public class CachingContentStoreSpringTest extends AbstractWritableContentStoreT assertEquals("Cache writer should still return actual size", overLimitSize, writer.getContentData().getSize()); } + /* + * MNT-11758 case test + */ + public void testBackingStoreBehavesLikeCenteraContentStore() + { + String content1 = "test content 1"; + String content2 = "test content 2"; + + // create backing store that behaves like centera content store, i.e. contentUrl is known only after content is written + centeraLikeBackingStore = new TestCenteraLikeContentStore(ctx, + TempFileProvider.getTempDir().getAbsolutePath() + + File.separatorChar + + getName() + "_centera"); + + // create caching content store with centera like backing store + store = new CachingContentStore(centeraLikeBackingStore, cache, true); + + ContentWriter writer1 = store.getWriter(new ContentContext(null, null)); + // for centera like backing content store all writers have the same content url before content was written + assertEquals(TestCenteraLikeContentWriter.UNKNOWN_ID, writer1.getContentData().getContentUrl()); + writer1.putContent(content1); + // after content was written into the backing store, content url should change + assertNotSame(TestCenteraLikeContentWriter.UNKNOWN_ID, writer1.getContentData().getContentUrl()); + + // get another writer + ContentWriter writer2 = store.getWriter(new ContentContext(null, null)); + // for centera like backing content store all writers have the same content url before content was written + assertEquals(TestCenteraLikeContentWriter.UNKNOWN_ID, writer2.getContentData().getContentUrl()); + writer2.putContent(content2); + // after content was written into the backing store, content url should change + assertNotSame(TestCenteraLikeContentWriter.UNKNOWN_ID, writer2.getContentData().getContentUrl()); + + // writers should have different content urls + assertNotSame(writer1.getContentData().getContentUrl(), writer2.getContentData().getContentUrl()); + + ContentReader reader = store.getReader(writer1.getContentData().getContentUrl()); + + assertEquals(content1, reader.getContentString()); + } + /* * @see org.alfresco.repo.content.AbstractReadOnlyContentStoreTest#getStore() */ diff --git a/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentStore.java b/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentStore.java new file mode 100644 index 0000000000..d70896e049 --- /dev/null +++ b/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentStore.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2005-2014 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.caching; + +import org.alfresco.repo.content.filestore.FileContentStore; +import org.alfresco.repo.content.filestore.FileContentWriter; +import org.alfresco.service.cmr.repository.ContentReader; +import org.alfresco.service.cmr.repository.ContentWriter; +import org.springframework.context.ApplicationContext; + +/** + * Test content store that behaves like Centera content store + * + * @author pavel.yurkevich + */ +public class TestCenteraLikeContentStore extends FileContentStore +{ + + public TestCenteraLikeContentStore(ApplicationContext context, String rootDirectoryStr) + { + super(context, rootDirectoryStr); + } + + @Override + public ContentWriter getWriterInternal(ContentReader existingContentReader, String newContentUrl) + { + FileContentWriter fileContentWriter = (FileContentWriter)super.getWriterInternal(existingContentReader, newContentUrl); + + return new TestCenteraLikeContentWriter(fileContentWriter.getFile(), fileContentWriter.getContentUrl(), existingContentReader); + } +} diff --git a/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentWriter.java b/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentWriter.java new file mode 100644 index 0000000000..ad458f504f --- /dev/null +++ b/source/test-java/org/alfresco/repo/content/caching/TestCenteraLikeContentWriter.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2005-2014 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.caching; + +import java.io.File; + +import org.alfresco.repo.content.ContentStore; +import org.alfresco.repo.content.filestore.FileContentStore; +import org.alfresco.repo.content.filestore.FileContentWriter; +import org.alfresco.service.cmr.repository.ContentIOException; +import org.alfresco.service.cmr.repository.ContentReader; +import org.alfresco.service.cmr.repository.ContentStreamListener; + +/** + * Test content writer that changes its content url after content was written (like centera content writer) + * + * @author pavel.yurkevich + */ +public class TestCenteraLikeContentWriter extends FileContentWriter implements ContentStreamListener +{ + public static final String UNKNOWN_ID = FileContentStore.STORE_PROTOCOL + ContentStore.PROTOCOL_DELIMITER + "UNKNOWN_ID"; + + private String originalContentUrl; + + public TestCenteraLikeContentWriter(File file, String url, ContentReader existingContentReader) + { + super(file, UNKNOWN_ID, existingContentReader); + + this.originalContentUrl = url; + + this.addListener(this); + } + + @Override + public void contentStreamClosed() throws ContentIOException + { + setContentUrl(originalContentUrl); + } + +}