Search in sources :

Example 1 with UnsupportedContentUrlException

use of org.alfresco.repo.content.UnsupportedContentUrlException in project alfresco-repository by Alfresco.

the class FileContentStore method getReader.

/**
 * This implementation requires that the URL start with
 * {@link FileContentStore#STORE_PROTOCOL } or {@link FileContentStore#SPOOF_PROTOCOL }
 */
public ContentReader getReader(String contentUrl) {
    // Handle the spoofed URL
    if (contentUrl.startsWith(SPOOF_PROTOCOL)) {
        return new SpoofedTextContentReader(contentUrl);
    }
    // else, it's a real file we are after
    try {
        File file = makeFile(contentUrl);
        ContentReader reader = null;
        if (file.exists()) {
            FileContentReader fileContentReader = new FileContentReader(file, contentUrl);
            fileContentReader.setAllowRandomAccess(allowRandomAccess);
            reader = fileContentReader;
        } else {
            reader = new EmptyContentReader(contentUrl);
        }
        // done
        if (logger.isDebugEnabled()) {
            logger.debug("Created content reader: \n" + "   url: " + contentUrl + "\n" + "   file: " + file + "\n" + "   reader: " + reader);
        }
        return reader;
    } catch (UnsupportedContentUrlException e) {
        // This can go out directly
        throw e;
    } catch (Throwable e) {
        throw new ContentIOException("Failed to get reader for URL: " + contentUrl, e);
    }
}
Also used : EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) File(java.io.File) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException)

Example 2 with UnsupportedContentUrlException

use of org.alfresco.repo.content.UnsupportedContentUrlException in project alfresco-repository by Alfresco.

the class AggregatingContentStoreTest method testGetDirectAccessUrl.

@Test
public void testGetDirectAccessUrl() {
    // Create the aggregating store
    AggregatingContentStore aggStore = new AggregatingContentStore();
    aggStore.setPrimaryStore(primaryStoreMock);
    aggStore.setSecondaryStores(List.of(secondaryStoreMock));
    UnsupportedOperationException unsupportedExc = new UnsupportedOperationException();
    UnsupportedContentUrlException unsupportedContentUrlExc = new UnsupportedContentUrlException(aggStore, "");
    // By default it is unsupported
    DirectAccessUrl directAccessUrl = aggStore.getDirectAccessUrl("url", null);
    assertNull(directAccessUrl);
    // Direct access not supported
    try {
        when(primaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedExc);
        when(secondaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedExc);
        aggStore.getDirectAccessUrl("urlDANotSupported", null);
        fail();
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    try {
        when(primaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedContentUrlExc);
        when(secondaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedExc);
        aggStore.getDirectAccessUrl("urlDANotSupported", null);
        fail();
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    try {
        when(primaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedExc);
        when(secondaryStoreMock.getDirectAccessUrl(eq("urlDANotSupported"), any())).thenThrow(unsupportedContentUrlExc);
        aggStore.getDirectAccessUrl("urlDANotSupported", null);
        fail();
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    // Content url not supported
    try {
        when(primaryStoreMock.getDirectAccessUrl(eq("urlNotSupported"), any())).thenThrow(unsupportedContentUrlExc);
        when(secondaryStoreMock.getDirectAccessUrl(eq("urlNotSupported"), any())).thenThrow(unsupportedContentUrlExc);
        aggStore.getDirectAccessUrl("urlNotSupported", null);
        fail();
    } catch (UnsupportedContentUrlException e) {
    // Expected
    }
    when(primaryStoreMock.getDirectAccessUrl(eq("urlPriSupported"), any())).thenReturn(new DirectAccessUrl());
    when(secondaryStoreMock.getDirectAccessUrl(eq("urlPriSupported"), any())).thenThrow(unsupportedExc);
    directAccessUrl = aggStore.getDirectAccessUrl("urlPriSupported", null);
    assertNotNull(directAccessUrl);
    when(primaryStoreMock.getDirectAccessUrl(eq("urlPriSupported"), any())).thenReturn(new DirectAccessUrl());
    when(secondaryStoreMock.getDirectAccessUrl(eq("urlPriSupported"), any())).thenThrow(unsupportedContentUrlExc);
    directAccessUrl = aggStore.getDirectAccessUrl("urlPriSupported", null);
    assertNotNull(directAccessUrl);
    when(primaryStoreMock.getDirectAccessUrl(eq("urlSecSupported"), any())).thenThrow(unsupportedExc);
    when(secondaryStoreMock.getDirectAccessUrl(eq("urlSecSupported"), any())).thenReturn(new DirectAccessUrl());
    directAccessUrl = aggStore.getDirectAccessUrl("urlSecSupported", null);
    assertNotNull(directAccessUrl);
    when(primaryStoreMock.getDirectAccessUrl(eq("urlSecSupported"), any())).thenThrow(unsupportedContentUrlExc);
    when(secondaryStoreMock.getDirectAccessUrl(eq("urlSecSupported"), any())).thenReturn(new DirectAccessUrl());
    directAccessUrl = aggStore.getDirectAccessUrl("urlSecSupported", null);
    assertNotNull(directAccessUrl);
    when(primaryStoreMock.getDirectAccessUrl(eq("urlPriSupported"), any())).thenReturn(new DirectAccessUrl());
    when(secondaryStoreMock.getDirectAccessUrl(eq("urlSecSupported"), any())).thenReturn(new DirectAccessUrl());
    directAccessUrl = aggStore.getDirectAccessUrl("urlPriSupported", null);
    assertNotNull(directAccessUrl);
    directAccessUrl = aggStore.getDirectAccessUrl("urlSecSupported", null);
    assertNotNull(directAccessUrl);
}
Also used : DirectAccessUrl(org.alfresco.service.cmr.repository.DirectAccessUrl) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException) Test(org.junit.Test) AbstractWritableContentStoreTest(org.alfresco.repo.content.AbstractWritableContentStoreTest)

Example 3 with UnsupportedContentUrlException

use of org.alfresco.repo.content.UnsupportedContentUrlException in project alfresco-repository by Alfresco.

the class FileContentStore method makeFile.

/**
 * Creates a file from the given relative URL.
 *
 * @param contentUrl    the content URL including the protocol prefix
 * @return              Returns a file representing the URL - the file may or may not
 *                      exist
 * @throws UnsupportedContentUrlException
 *                      if the URL is invalid and doesn't support the
 *                      {@link FileContentStore#STORE_PROTOCOL correct protocol}
 */
/*package*/
File makeFile(String contentUrl) {
    // take just the part after the protocol
    Pair<String, String> urlParts = super.getContentUrlParts(contentUrl);
    String protocol = urlParts.getFirst();
    String relativePath = urlParts.getSecond();
    // Check the protocol
    if (!protocol.equals(FileContentStore.STORE_PROTOCOL)) {
        throw new UnsupportedContentUrlException(this, protocol + PROTOCOL_DELIMITER + relativePath);
    }
    // get the file
    File file = new File(rootDirectory, relativePath);
    ensureFileInContentStore(file);
    // done
    return file;
}
Also used : File(java.io.File) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException)

Example 4 with UnsupportedContentUrlException

use of org.alfresco.repo.content.UnsupportedContentUrlException in project alfresco-repository by Alfresco.

the class AggregatingContentStore method getDirectAccessUrl.

public DirectAccessUrl getDirectAccessUrl(String contentUrl, Date expiresAt) {
    if (primaryStore == null) {
        throw new AlfrescoRuntimeException("ReplicatingContentStore not initialised");
    }
    // get a read lock so that we are sure that no replication is underway
    readLock.lock();
    try {
        // Keep track of the unsupported state of the content URL - it might be a rubbish URL
        boolean contentUrlSupported = true;
        boolean directAccessUrlSupported = true;
        DirectAccessUrl directAccessUrl = null;
        // Check the primary store
        try {
            directAccessUrl = primaryStore.getDirectAccessUrl(contentUrl, expiresAt);
        } catch (UnsupportedOperationException e) {
            // The store does not support direct access URL
            directAccessUrlSupported = false;
        } catch (UnsupportedContentUrlException e) {
            // The store can't handle the content URL
            contentUrlSupported = false;
        }
        if (directAccessUrl != null) {
            return directAccessUrl;
        }
        // the content is not in the primary store so we have to go looking for it
        for (ContentStore store : secondaryStores) {
            try {
                directAccessUrl = store.getDirectAccessUrl(contentUrl, expiresAt);
            } catch (UnsupportedOperationException e) {
                // The store does not support direct access URL
                directAccessUrlSupported = false;
            } catch (UnsupportedContentUrlException e) {
                // The store can't handle the content URL
                contentUrlSupported = false;
            }
            if (directAccessUrl != null) {
                break;
            }
        }
        if (directAccessUrl == null) {
            if (!directAccessUrlSupported) {
                // The direct access URL was not supported
                throw new UnsupportedOperationException("Retrieving direct access URLs is not supported by this content store.");
            } else if (!contentUrlSupported) {
                // The content URL was not supported
                throw new UnsupportedContentUrlException(this, contentUrl);
            }
        }
        return directAccessUrl;
    } finally {
        readLock.unlock();
    }
}
Also used : AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DirectAccessUrl(org.alfresco.service.cmr.repository.DirectAccessUrl) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException) ContentStore(org.alfresco.repo.content.ContentStore) AbstractContentStore(org.alfresco.repo.content.AbstractContentStore) CachingContentStore(org.alfresco.repo.content.caching.CachingContentStore)

Example 5 with UnsupportedContentUrlException

use of org.alfresco.repo.content.UnsupportedContentUrlException in project alfresco-repository by Alfresco.

the class AggregatingContentStore method exists.

public boolean exists(String contentUrl) {
    if (primaryStore == null) {
        throw new AlfrescoRuntimeException("ReplicatingContentStore not initialised");
    }
    // get a read lock so that we are sure that no replication is underway
    readLock.lock();
    try {
        // Keep track of the unsupported state of the content URL - it might be a rubbish URL
        boolean contentUrlSupported = false;
        boolean contentUrlExists = false;
        // Check the primary store
        try {
            contentUrlExists = primaryStore.exists(contentUrl);
            // At least the content URL was supported
            contentUrlSupported = true;
        } catch (UnsupportedContentUrlException e) {
        // The store can't handle the content URL
        }
        if (contentUrlExists) {
            return true;
        }
        // the content is not in the primary store so we have to go looking for it
        for (ContentStore store : secondaryStores) {
            contentUrlExists = false;
            try {
                contentUrlExists = store.exists(contentUrl);
                // At least the content URL was supported
                contentUrlSupported = true;
            } catch (UnsupportedContentUrlException e) {
            // The store can't handle the content URL
            }
            if (contentUrlExists) {
                break;
            }
        }
        // Check if the content URL was supported
        if (!contentUrlSupported) {
            throw new UnsupportedContentUrlException(this, contentUrl);
        }
        return contentUrlExists;
    } finally {
        readLock.unlock();
    }
}
Also used : AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException) ContentStore(org.alfresco.repo.content.ContentStore) AbstractContentStore(org.alfresco.repo.content.AbstractContentStore) CachingContentStore(org.alfresco.repo.content.caching.CachingContentStore)

Aggregations

UnsupportedContentUrlException (org.alfresco.repo.content.UnsupportedContentUrlException)5 File (java.io.File)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 AbstractContentStore (org.alfresco.repo.content.AbstractContentStore)2 ContentStore (org.alfresco.repo.content.ContentStore)2 CachingContentStore (org.alfresco.repo.content.caching.CachingContentStore)2 DirectAccessUrl (org.alfresco.service.cmr.repository.DirectAccessUrl)2 AbstractWritableContentStoreTest (org.alfresco.repo.content.AbstractWritableContentStoreTest)1 EmptyContentReader (org.alfresco.repo.content.EmptyContentReader)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 Test (org.junit.Test)1