Java Code Examples for org.alfresco.repo.content.filestore.FileContentStore

The following examples show how to use org.alfresco.repo.content.filestore.FileContentStore. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Check that the given store name is in the list. 
 * Also check it's an instance of {@link FileContentStore}. If it's not, output a warning
 * as non-file-based implementations have not been tested and may be unsupported.
 * 
 * @param storeName	the store name to check
 */
public ContentStore checkAndGetStore(String storeName)
{
	ContentStore store = storeMap.get(storeName);
   	if(store == null)
   	{
   		String validStores ="";
        	Iterator<String> it = storeMap.keySet().iterator();
        	while (it.hasNext()) 
        	{
        		validStores += "'" + it.next() + "'" + (it.hasNext() ? " , " : "");
        	}
        	throw new IllegalArgumentException("given store name : '" + storeName + "' is not part of the registered stores : " + validStores);
        }
        if(!(store instanceof FileContentStore))
        {
       	 // letting you off with a warning :)
       	 // some people may have a custom content store for which the import could work in this case too ...
       	 if(logger.isWarnEnabled())
       	 {
       		 logger.warn("selected store '" + storeName + "' is not a FileContentStore. Is the implementation based on local files ?");
       	 }
        }
        
        return store;
}
 
Example 2
/**
 * Service initialise 
 */
public void init()
{
    // Set up a temporary store
    this.tempStore = new FileContentStore(this.applicationContext, TempFileProvider.getTempDir().getAbsolutePath());

    // Bind on update properties behaviour
    this.policyComponent.bindClassBehaviour(
            NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME,
            this,
            new JavaBehaviour(this, "onUpdateProperties"));
    
    // Register on content update policy
    this.onContentUpdateDelegate = this.policyComponent.registerClassPolicy(OnContentUpdatePolicy.class);
    this.onContentPropertyUpdateDelegate = this.policyComponent.registerClassPolicy(OnContentPropertyUpdatePolicy.class);
    this.onContentReadDelegate = this.policyComponent.registerClassPolicy(OnContentReadPolicy.class);
}
 
Example 3
protected ContentStore initContentStore(ApplicationContext ctx, String contentRoot)
{
	Map<String, Serializable> extendedEventParams = new HashMap<String, Serializable>();
	if (!TenantService.DEFAULT_DOMAIN.equals(tenantService.getCurrentUserDomain()))
	{
	    extendedEventParams.put("Tenant", tenantService.getCurrentUserDomain());
	}

    FileContentStore fileContentStore = new FileContentStore(ctx, new File(contentRoot), extendedEventParams);
    
    // Set the content filesize limiter if there is one.
    if (this.contentLimitProvider != null)
    {
        fileContentStore.setContentLimitProvider(contentLimitProvider);
    }
    
    if(fileContentUrlProvider != null)
    {
        fileContentStore.setFileContentUrlProvider(fileContentUrlProvider);
    }
    return fileContentStore;
}
 
Example 4
@Before
public void before() throws Exception
{
    File tempDir = TempFileProvider.getTempDir();
    // create a primary file store
    String storeDir = tempDir.getAbsolutePath() + File.separatorChar + GUID.generate();
    primaryStore = new FileContentStore(ctx, storeDir);
    // create some secondary file stores
    secondaryStores = new ArrayList<ContentStore>(3);
    for (int i = 0; i < 4; i++)
    {
        storeDir = tempDir.getAbsolutePath() + File.separatorChar + GUID.generate();
        FileContentStore store = new FileContentStore(ctx, storeDir);
        secondaryStores.add(store);
    }
    // Create the aggregating store
    aggregatingStore = new AggregatingContentStore();
    aggregatingStore.setPrimaryStore(primaryStore);
    aggregatingStore.setSecondaryStores(secondaryStores);
}
 
Example 5
@Before
public void before() throws Exception
{
    File tempDir = TempFileProvider.getTempDir();
    // Create a subdirectory for A
    File storeADir = new File(tempDir, "A");
    storeA = new FileContentStore(ctx, storeADir);
    // Create a subdirectory for B
    File storeBDir = new File(tempDir, "B");
    storeB = new FileContentStore(ctx, storeBDir);
    // Create a subdirectory for C
    File storeCDir = new File(tempDir, "C");
    storeC = new DumbReadOnlyFileStore(new FileContentStore(ctx, storeCDir));
    // No subdirectory for D
    storeD = new SupportsNoUrlFormatStore();
    // Create the routing store
    routingStore = new RandomRoutingContentStore(storeA, storeB, storeC, storeD);
}
 
Example 6
/**
 * Checks that requests for missing content URLs are served.
 */
@Test
public void testMissingUrl()
{
    String missingContentUrl = FileContentStore.createNewFileStoreUrl();
    
    ContentReader reader = routingStore.getReader(missingContentUrl);
    assertNotNull("Missing URL should not return null", reader);
    assertFalse("Empty reader should say content doesn't exist.", reader.exists());
    try
    {
        reader.getContentString();
        fail("Empty reader cannot return content.");
    }
    catch (Throwable e)
    {
        // Expected
    }
}
 
Example 7
@Test
public void writeToCacheWithExistingReader()
{   
    ContentWriter oldWriter = store.getWriter(ContentContext.NULL_CONTEXT);
    oldWriter.putContent("Old content for " + getClass().getSimpleName());
    ContentReader existingReader = oldWriter.getReader();
    
    // Write through the caching content store - cache during the process.
    final String proposedUrl = FileContentStore.createNewFileStoreUrl();
    ContentWriter writer = store.getWriter(new ContentContext(existingReader, proposedUrl));
    final String content = makeContent();
    writer.putContent(content);
    assertEquals("Writer should have correct URL", proposedUrl, writer.getContentUrl());
    
    assertFalse("Old and new writers must have different URLs",
                oldWriter.getContentUrl().equals(writer.getContentUrl()));
    
    ContentReader reader = store.getReader(writer.getContentUrl());
    assertEquals("Reader and writer should have same URLs", writer.getContentUrl(), reader.getContentUrl());
    assertEquals("Reader should get correct content", content, reader.getContentString());
}
 
Example 8
/**
 * {@inheritDoc}
 * <p>
 * For {@link #SPOOF_PROTOCOL spoofed} URLs, the URL always exists.
 */
@Override
public boolean exists(String contentUrl)
{
    if (contentUrl.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        return true;
    }
    else
    {
        return backingStore.exists(contentUrl);
    }
}
 
Example 9
/**
 * {@inheritDoc}
 * <p>
 * This store handles the {@link FileContentStore#SPOOF_PROTOCOL} so that underlying stores do not need
 * to implement anything <a href="https://issues.alfresco.com/jira/browse/ACE-4516">related to spoofing</a>.
 */
@Override
public ContentReader getReader(String contentUrl)
{
    // Handle the spoofed URL
    if (contentUrl.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        return new SpoofedTextContentReader(contentUrl);
    }

    // Use pool of locks - which one is determined by a hash of the URL.
    // This will stop the content from being read/cached multiple times from the backing store
    // when it should only be read once - cached versions should be returned after that.
    ReadLock readLock = readWriteLock(contentUrl).readLock();
    readLock.lock();
    try
    {
        if (cache.contains(contentUrl))
        {
            return cache.getReader(contentUrl);
        }
    }
    catch(CacheMissException e)
    {
        // Fall through to cacheAndRead(url);
    }
    finally
    {
        readLock.unlock();
    }
    
    return cacheAndRead(contentUrl);
}
 
Example 10
private void loadData(final int maxCount)
{
    final MutableInt doneCount = new MutableInt(0);
    // Batches of 1000 objects
    RetryingTransactionCallback<Integer> makeNodesCallback = new RetryingTransactionCallback<Integer>()
    {
        public Integer execute() throws Throwable
        {
            for (int i = 0; i < 1000; i++)
            {
                // We don't need to write anything
                String contentUrl = FileContentStore.createNewFileStoreUrl();
                ContentData contentData = new ContentData(contentUrl, MimetypeMap.MIMETYPE_TEXT_PLAIN, 10, "UTF-8");
                nodeHelper.makeNode(contentData);
                
                int count = doneCount.intValue();
                count++;
                doneCount.setValue(count);
                
                // Do some reporting
                if (count % 1000 == 0)
                {
                    System.out.println(String.format("   " + (new Date()) + "Total created: %6d", count));
                }
                
                // Double check for shutdown
                if (vmShutdownListener.isVmShuttingDown())
                {
                    break;
                }
            }
            return maxCount;
        }
    };
    int repetitions = (int) Math.floor((double)maxCount / 1000.0);
    for (int i = 0; i < repetitions; i++)
    {
        transactionService.getRetryingTransactionHelper().doInTransaction(makeNodesCallback);
    }
}
 
Example 11
@Before
public void before() throws Exception
{
    File tempDir = TempFileProvider.getTempDir();
    
    backingStore = new FileContentStore(ctx,
            tempDir.getAbsolutePath() +
            File.separatorChar +
            getName());
    
    cache = new ContentCacheImpl();
    cache.setCacheRoot(TempFileProvider.getLongLifeTempDir("cached_content_test"));
    cache.setMemoryStore(createMemoryStore());
    store = new CachingContentStore(backingStore, cache, false);
}
 
Example 12
public void testCacheOnInbound()
{
    store = new CachingContentStore(backingStore, cache, true);
    final String content = "Content for " + getName() + " test.";
    final String contentUrl = FileContentStore.createNewFileStoreUrl();
    
    assertFalse("Content shouldn't be cached yet", cache.contains(contentUrl));
    
    // Write some content using the caching store
    ContentWriter writer = store.getWriter(new ContentContext(null, contentUrl));
    writer.putContent(content);
    
    assertTrue("Cache should contain content after write", cache.contains(contentUrl));
    // Check DIRECTLY with the cache, since a getReader() from the CachingContentStore would result
    // in caching, but we're checking that caching was caused by the write operation.
    String retrievedContent = cache.getReader(contentUrl).getContentString(); 
    assertEquals(content, retrievedContent);
    
    // The content should have been written through to the backing store.
    String fromBackingStore = backingStore.getReader(contentUrl).getContentString();
    assertEquals("Content should be in backing store", content, fromBackingStore);
    
    // Remove the original content from the backing store.
    backingStore.delete(contentUrl);
    assertFalse("Original content should have been deleted", backingStore.exists(contentUrl));
    
    // The cached version is still available
    String contentAfterDelete = store.getReader(contentUrl).getContentString();
    assertEquals(content, contentAfterDelete);
}
 
Example 13
public void testQuotaOnOverlimitCacheFile()
{
    store = new CachingContentStore(backingStore, cache, true);
    
    final int overLimitSize = 1;
    QuotaManagerStrategy quota = mock(QuotaManagerStrategy.class);
    store.setQuota(quota);
    when(quota.beforeWritingCacheFile(0)).thenReturn(true);
    when(quota.afterWritingCacheFile(overLimitSize)).thenReturn(false);
    
    char[] chars = new char[overLimitSize];
    // Optional step - unnecessary if you're happy with the array being full of \0
    Arrays.fill(chars, 'f');
    final String content = new String(chars);
    final String contentUrl = FileContentStore.createNewFileStoreUrl();
    assertFalse("Content shouldn't be cached yet", cache.contains(contentUrl));
    
    // Write some content using the caching store
    ContentWriter writer = store.getWriter(new ContentContext(null, contentUrl));
    writer.putContent(content);
    
    assertFalse("Overlimit content should be deleted from cache", cache.contains(contentUrl));
    assertFalse("Overlimit content should be deleted from cache", writer.getReader().exists());

    // The content should have been written through to the backing store.
    String fromBackingStore = backingStore.getReader(contentUrl).getContentString();
    assertEquals("Content should be in backing store", content, fromBackingStore);
    assertEquals("Content should be in backing store", overLimitSize, fromBackingStore.length());
    
    // cache writer should still return actual size
    // so that listeners could be executed correctly
    assertEquals("Cache writer should still return actual size", overLimitSize, writer.getSize());
    assertEquals("Cache writer should still return actual size", overLimitSize, writer.getContentData().getSize());
}
 
Example 14
@Test
public void writeToCacheWithContentContext()
{
    // Write through the caching content store - cache during the process.
    final String proposedUrl = FileContentStore.createNewFileStoreUrl();
    ContentWriter writer = store.getWriter(new ContentContext(null, proposedUrl));
    final String content = makeContent();
    writer.putContent(content);
    assertEquals("Writer should have correct URL", proposedUrl, writer.getContentUrl());
    
    ContentReader reader = store.getReader(writer.getContentUrl());
    assertEquals("Reader and writer should have same URLs", writer.getContentUrl(), reader.getContentUrl());
    assertEquals("Reader should get correct content", content, reader.getContentString());
}
 
Example 15
@Override
protected ContentWriter getWriterInternal(ContentReader existingContentReader, String newContentUrl)
{
    if (newContentUrl == null)
        newContentUrl = FileContentStore.createNewFileStoreUrl() + ".slow";
    
    return new SlowWriter(newContentUrl, existingContentReader);
}
 
Example 16
@Override
public void setUp() throws Exception
{
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    transactionService = serviceRegistry.getTransactionService();
    txnHelper = transactionService.getRetryingTransactionHelper();
    
    contentDataDAO = (ContentDataDAO) ctx.getBean("contentDataDAO");
    contentStore = new FileContentStore(ctx, TempFileProvider.getTempDir());
}
 
Example 17
@Override
public boolean delete(String contentUrl)
{
    if (contentUrl.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        // This is not a failure but the content can never actually be deleted
        return false;
    }

    ReentrantReadWriteLock readWriteLock = readWriteLock(contentUrl);
    ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try
    {
        if (!cache.contains(contentUrl))
        {
            // The item isn't in the cache, so simply delete from the backing store
            return backingStore.delete(contentUrl);
        }
    }
    finally
    {
        readLock.unlock();
    }
    
    WriteLock writeLock = readWriteLock.writeLock();
    writeLock.lock();
    try
    {
        // Double check the content still exists in the cache
        if (cache.contains(contentUrl))
        {
            // The item is in the cache, so remove.
            cache.remove(contentUrl);
            
        }
        // Whether the item was in the cache or not, it must still be deleted from the backing store.
        return backingStore.delete(contentUrl);
    }
    finally
    {
        writeLock.unlock();
    }
}
 
Example 18
public DumbReadOnlyFileStore(FileContentStore fileStore)
{
    this.fileStore = fileStore;
}
 
Example 19
/**
 * Load 15 files with default sizes
 */
@SuppressWarnings("unchecked")
public void testLoad_15_default() throws Exception
{
    JSONObject body = new JSONObject();
    body.put(FileFolderLoaderPost.KEY_FOLDER_PATH, loadHomePath);
    body.put(FileFolderLoaderPost.KEY_FILE_COUNT, 15);
    body.put(FileFolderLoaderPost.KEY_FILES_PER_TXN, 10);
    
    Response response = null;
    try
    {
        AuthenticationUtil.pushAuthentication();
        AuthenticationUtil.setFullyAuthenticatedUser("hhoudini");
        response = sendRequest(
                new PostRequest(URL,  body.toString(), "application/json"),
                Status.STATUS_OK,
                "hhoudini");
    }
    finally
    {
        AuthenticationUtil.popAuthentication();
    }
    assertEquals("{\"count\":15}", response.getContentAsString());
    
    // Check file(s)
    assertEquals(15, nodeService.countChildAssocs(loadHomeNodeRef, true));
    // Size should be default
    List<FileInfo> fileInfos = fileFolderService.list(loadHomeNodeRef);
    for (FileInfo fileInfo : fileInfos)
    {
        NodeRef fileNodeRef = fileInfo.getNodeRef();
        ContentReader reader = fileFolderService.getReader(fileNodeRef);
        // Expect spoofing by default
        assertTrue(reader.getContentUrl().startsWith(FileContentStore.SPOOF_PROTOCOL));
        assertTrue(
                "Default file size not correct: " + reader,
                FileFolderLoaderPost.DEFAULT_MIN_FILE_SIZE < reader.getSize() &&
                    reader.getSize() < FileFolderLoaderPost.DEFAULT_MAX_FILE_SIZE);
        // Check creator
        assertEquals("hhoudini", nodeService.getProperty(fileNodeRef, ContentModel.PROP_CREATOR));
        // We also expect the default language description to be present
        String description = (String) nodeService.getProperty(fileNodeRef, ContentModel.PROP_DESCRIPTION);
        assertNotNull("No description", description);
        assertEquals("Description length incorrect: \"" + description + "\"", 128L, description.getBytes("UTF-8").length);
    }
}
 
Example 20
/**
 * Load 15 files; 1K size; 1 document sample; force binary storage
 */
@SuppressWarnings("unchecked")
public void testLoad_15_16bytes() throws Exception
{
    JSONObject body = new JSONObject();
    body.put(FileFolderLoaderPost.KEY_FOLDER_PATH, loadHomePath);
    body.put(FileFolderLoaderPost.KEY_MIN_FILE_SIZE, 16L);
    body.put(FileFolderLoaderPost.KEY_MAX_FILE_SIZE, 16L);
    body.put(FileFolderLoaderPost.KEY_MAX_UNIQUE_DOCUMENTS, 1L);
    body.put(FileFolderLoaderPost.KEY_FORCE_BINARY_STORAGE, Boolean.TRUE);
    
    Response response = null;
    try
    {
        AuthenticationUtil.pushAuthentication();
        AuthenticationUtil.setFullyAuthenticatedUser("maggi");
        response = sendRequest(
                new PostRequest(URL,  body.toString(), "application/json"),
                Status.STATUS_OK,
                "maggi");
    }
    finally
    {
        AuthenticationUtil.popAuthentication();
    }
    assertEquals("{\"count\":100}", response.getContentAsString());
    
    // Check file(s)
    assertEquals(100, nodeService.countChildAssocs(loadHomeNodeRef, true));
    
    // Consistent binary text
    String contentUrlCheck = SpoofedTextContentReader.createContentUrl(Locale.ENGLISH, 0L, 16L);
    ContentReader readerCheck = new SpoofedTextContentReader(contentUrlCheck);
    String textCheck = readerCheck.getContentString();
    
    // Size should be default
    List<FileInfo> fileInfos = fileFolderService.list(loadHomeNodeRef);
    for (FileInfo fileInfo : fileInfos)
    {
        NodeRef fileNodeRef = fileInfo.getNodeRef();
        ContentReader reader = fileFolderService.getReader(fileNodeRef);
        // Expect storage in store
        assertTrue(reader.getContentUrl().startsWith(FileContentStore.STORE_PROTOCOL));
        // Check text
        String text = reader.getContentString();
        assertEquals("Text not the same.", textCheck, text);
    }
}
 
Example 21
/**
 * Constructor that builds a URL based on the absolute path of the file.
 *
 * @param file
 *            the file for writing. This will most likely be directly
 *            related to the content URL.
 * @param existingContentReader
 *            a reader of a previous version of this content
 */
public FileContentWriterImpl(final File file, final ContentReader existingContentReader)
{
    this(file, FileContentStore.STORE_PROTOCOL + ContentStore.PROTOCOL_DELIMITER + file.getAbsolutePath(), existingContentReader);
}
 
Example 22
/**
 * Constructor that builds a URL based on the absolute path of the file.
 *
 * @param file
 *            the file for reading. This will most likely be directly
 *            related to the content URL.
 */
public FileContentReaderImpl(final File file)
{
    this(file, FileContentStore.STORE_PROTOCOL + ContentStore.PROTOCOL_DELIMITER + file.getAbsolutePath());
}