ACS-2064 DAU: Fix response content type always PDF (#754)

* ACS-2064 ContentStore.java to require mimetype

- Updates the ContentStore interface to also require a mimetype, deprecates old method signatures
- Original interface methods maintained so code should be backwards compatible
- Implementing classes updated to reflect changes
This commit is contained in:
David Edwards
2021-10-19 13:10:17 +01:00
committed by GitHub
parent 15cb7ff44e
commit c00736c639
7 changed files with 93 additions and 54 deletions

View File

@@ -269,9 +269,25 @@ public interface ContentStore
* @return A direct access {@code URL} object for the content * @return A direct access {@code URL} object for the content
* @throws UnsupportedOperationException if the store is unable to provide the information * @throws UnsupportedOperationException if the store is unable to provide the information
*/ */
@Deprecated
default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName) default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName)
{ {
return requestContentDirectUrl(contentUrl, attachment, fileName, null); return requestContentDirectUrl(contentUrl, attachment, fileName, null, null);
}
/**
* Gets a presigned URL to directly access the content. It is up to the actual store
* implementation if it can fulfil this request with an expiry time or not.
*
* @param contentUrl A content store {@code URL}
* @param attachment {@code true} if an attachment URL is requested, {@code false} for an embedded {@code URL}.
* @param fileName File name of the content
* @return A direct access {@code URL} object for the content
* @throws UnsupportedOperationException if the store is unable to provide the information
*/
default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, String mimetype)
{
return requestContentDirectUrl(contentUrl, attachment, fileName, mimetype, null);
} }
/** /**
@@ -285,7 +301,25 @@ public interface ContentStore
* @return A direct access {@code URL} object for the content. * @return A direct access {@code URL} object for the content.
* @throws UnsupportedOperationException if the store is unable to provide the information * @throws UnsupportedOperationException if the store is unable to provide the information
*/ */
@Deprecated
default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, Long validFor) default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, Long validFor)
{
return requestContentDirectUrl(contentUrl, attachment, fileName, null, validFor);
}
/**
* Gets a presigned URL to directly access the content. It is up to the actual store
* implementation if it can fulfil this request with an expiry time or not.
*
* @param contentUrl A content store {@code URL}
* @param attachment {@code true} if an attachment URL is requested, {@code false} for an embedded {@code URL}.
* @param fileName File name of the content
* @param mimetype Mimetype of the content
* @param validFor The time at which the direct access {@code URL} will expire.
* @return A direct access {@code URL} object for the content.
* @throws UnsupportedOperationException if the store is unable to provide the information
*/
default DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, String mimetype, Long validFor)
{ {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Retrieving direct access URLs is not supported by this content store."); "Retrieving direct access URLs is not supported by this content store.");

View File

@@ -605,7 +605,7 @@ public class ContentServiceImpl implements ContentService, ApplicationContextAwa
throw new IllegalArgumentException("The supplied nodeRef " + nodeRef + " has no content."); throw new IllegalArgumentException("The supplied nodeRef " + nodeRef + " has no content.");
} }
contentDirectUrlEnabled = (store.isContentDirectUrlEnabled(getContentUrl(nodeRef))); contentDirectUrlEnabled = (store.isContentDirectUrlEnabled(contentData.getContentUrl()));
} }
return contentDirectUrlEnabled; return contentDirectUrlEnabled;
@@ -621,8 +621,17 @@ public class ContentServiceImpl implements ContentService, ApplicationContextAwa
throw new DirectAccessUrlDisabledException("Direct access url isn't available."); throw new DirectAccessUrlDisabledException("Direct access url isn't available.");
} }
String contentUrl = getContentUrl(nodeRef); ContentData contentData = getContentData(nodeRef, ContentModel.PROP_CONTENT);
// check that the content & URL is available
if (contentData == null || contentData.getContentUrl() == null)
{
throw new IllegalArgumentException("The supplied nodeRef " + nodeRef + " has no content.");
}
String contentUrl = contentData.getContentUrl();
String contentMimetype = contentData.getMimetype();
String fileName = getFileName(nodeRef); String fileName = getFileName(nodeRef);
validFor = adjustValidFor(validFor); validFor = adjustValidFor(validFor);
DirectAccessUrl directAccessUrl = null; DirectAccessUrl directAccessUrl = null;
@@ -630,7 +639,7 @@ public class ContentServiceImpl implements ContentService, ApplicationContextAwa
{ {
try try
{ {
directAccessUrl = store.requestContentDirectUrl(contentUrl, attachment, fileName, validFor); directAccessUrl = store.requestContentDirectUrl(contentUrl, attachment, fileName, contentMimetype, validFor);
} }
catch (UnsupportedOperationException ex) catch (UnsupportedOperationException ex)
{ {
@@ -640,19 +649,6 @@ public class ContentServiceImpl implements ContentService, ApplicationContextAwa
return directAccessUrl; return directAccessUrl;
} }
protected String getContentUrl(NodeRef nodeRef)
{
ContentData contentData = getContentData(nodeRef, ContentModel.PROP_CONTENT);
// check that the URL is available
if (contentData == null || contentData.getContentUrl() == null)
{
throw new IllegalArgumentException("The supplied nodeRef " + nodeRef + " has no content.");
}
return contentData.getContentUrl();
}
protected String getFileName(NodeRef nodeRef) protected String getFileName(NodeRef nodeRef)
{ {
String fileName = null; String fileName = null;

View File

@@ -509,4 +509,12 @@ public class CachingContentStore implements ContentStore, ApplicationEventPublis
{ {
return backingStore.requestContentDirectUrl(contentUrl, attachment, fileName, validFor); return backingStore.requestContentDirectUrl(contentUrl, attachment, fileName, validFor);
} }
/**
* {@inheritDoc}
*/
public DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, String mimeType, Long validFor)
{
return backingStore.requestContentDirectUrl(contentUrl, attachment, fileName, mimeType, validFor);
}
} }

View File

@@ -317,7 +317,7 @@ public class AggregatingContentStore extends AbstractContentStore
return isContentDirectUrlEnabled; return isContentDirectUrlEnabled;
} }
public DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, Long validFor) public DirectAccessUrl requestContentDirectUrl(String contentUrl, boolean attachment, String fileName, String mimetype, Long validFor)
{ {
if (primaryStore == null) if (primaryStore == null)
{ {
@@ -337,7 +337,7 @@ public class AggregatingContentStore extends AbstractContentStore
// Check the primary store // Check the primary store
try try
{ {
directAccessUrl = primaryStore.requestContentDirectUrl(contentUrl, attachment, fileName, validFor); directAccessUrl = primaryStore.requestContentDirectUrl(contentUrl, attachment, fileName, mimetype, validFor);
} }
catch (UnsupportedOperationException e) catch (UnsupportedOperationException e)
{ {
@@ -360,7 +360,7 @@ public class AggregatingContentStore extends AbstractContentStore
{ {
try try
{ {
directAccessUrl = store.requestContentDirectUrl(contentUrl, attachment, fileName, validFor); directAccessUrl = store.requestContentDirectUrl(contentUrl, attachment, fileName, mimetype, validFor);
} }
catch (UnsupportedOperationException e) catch (UnsupportedOperationException e)
{ {

View File

@@ -83,6 +83,7 @@ public class ContentServiceImplUnitTest
openMocks(this); openMocks(this);
when(mockNodeService.getProperty(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(mockContentData); when(mockNodeService.getProperty(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(mockContentData);
when(mockContentData.getContentUrl()).thenReturn("someContentUrl"); when(mockContentData.getContentUrl()).thenReturn("someContentUrl");
when(mockContentData.getMimetype()).thenReturn("someMimetype");
when(mockNodeService.getProperty(NODE_REF, ContentModel.PROP_NAME)).thenReturn("someFilename"); when(mockNodeService.getProperty(NODE_REF, ContentModel.PROP_NAME)).thenReturn("someFilename");
} }
@@ -133,7 +134,7 @@ public class ContentServiceImplUnitTest
DirectAccessUrl directAccessUrl = contentService.requestContentDirectUrl(NODE_REF, true, 20L); DirectAccessUrl directAccessUrl = contentService.requestContentDirectUrl(NODE_REF, true, 20L);
assertNull(directAccessUrl); assertNull(directAccessUrl);
verify(mockContentStore, never()).requestContentDirectUrl(anyString(), eq(true), anyString(), anyLong()); verify(mockContentStore, never()).requestContentDirectUrl(anyString(), eq(true), anyString(), anyString(), anyLong());
} }
@Test @Test
@@ -144,7 +145,7 @@ public class ContentServiceImplUnitTest
DirectAccessUrl directAccessUrl = contentService.requestContentDirectUrl(NODE_REF, true, 20L); DirectAccessUrl directAccessUrl = contentService.requestContentDirectUrl(NODE_REF, true, 20L);
assertNull(directAccessUrl); assertNull(directAccessUrl);
verify(mockContentStore, times(1)).requestContentDirectUrl(anyString(), eq(true), anyString(), anyLong()); verify(mockContentStore, times(1)).requestContentDirectUrl(anyString(), eq(true), anyString(), anyString(), anyLong());
} }
/* Helper method to set system-wide direct access url configuration settings */ /* Helper method to set system-wide direct access url configuration settings */

View File

@@ -505,8 +505,8 @@ public class CachingContentStoreTest
{ {
try try
{ {
when(backingStore.requestContentDirectUrl(anyString(), eq(true), anyString(), anyLong())).thenThrow(new UnsupportedOperationException()); when(backingStore.requestContentDirectUrl(anyString(), eq(true), anyString(), anyString(), anyLong())).thenThrow(new UnsupportedOperationException());
cachingStore.requestContentDirectUrl("url", true,"someFile", 30L); cachingStore.requestContentDirectUrl("url", true,"someFile", "someMimetype", 30L);
fail(); fail();
} }
catch (UnsupportedOperationException e) catch (UnsupportedOperationException e)
@@ -518,7 +518,7 @@ public class CachingContentStoreTest
@Test @Test
public void getRequestContentDirectUrl() public void getRequestContentDirectUrl()
{ {
when(backingStore.requestContentDirectUrl(anyString(), eq(true), anyString(), anyLong())).thenReturn(new DirectAccessUrl()); when(backingStore.requestContentDirectUrl(anyString(), eq(true), anyString(), anyString(), anyLong())).thenReturn(new DirectAccessUrl());
cachingStore.requestContentDirectUrl("url", true,"someFile", 30L); cachingStore.requestContentDirectUrl("url", true,"someFile", "someMimeType", 30L);
} }
} }

View File

@@ -227,14 +227,14 @@ public class AggregatingContentStoreTest extends AbstractWritableContentStoreTes
UnsupportedContentUrlException unsupportedContentUrlExc = new UnsupportedContentUrlException(aggStore, ""); UnsupportedContentUrlException unsupportedContentUrlExc = new UnsupportedContentUrlException(aggStore, "");
// By default it is unsupported // By default it is unsupported
DirectAccessUrl directAccessUrl = aggStore.requestContentDirectUrl("url", true, "anyfilename", 30L); DirectAccessUrl directAccessUrl = aggStore.requestContentDirectUrl("url", true, "anyfilename", "anyMimetype", 30L);
assertNull(directAccessUrl); assertNull(directAccessUrl);
// Direct access not supported // Direct access not supported
try try
{ {
when(primaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(primaryStoreMock.requestContentDirectUrl(any(), any(), any(), any(), any())).thenThrow(unsupportedExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any(), any())).thenThrow(unsupportedExc);
aggStore.requestContentDirectUrl(eq("urlDANotSupported"), true, "anyfilename", 30L); aggStore.requestContentDirectUrl(eq("urlDANotSupported"), true, "anyfilename", 30L);
fail(); fail();
} }
@@ -245,9 +245,9 @@ public class AggregatingContentStoreTest extends AbstractWritableContentStoreTes
try try
{ {
when(primaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(primaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any(), any())).thenThrow(unsupportedExc);
aggStore.requestContentDirectUrl("urlDANotSupported", true, "anyfilename", 30L); aggStore.requestContentDirectUrl("urlDANotSupported", true, "anyfilename", "anyMimetype", 30L);
fail(); fail();
} }
catch (UnsupportedOperationException e) catch (UnsupportedOperationException e)
@@ -257,9 +257,9 @@ public class AggregatingContentStoreTest extends AbstractWritableContentStoreTes
try try
{ {
when(primaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(primaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any(), any())).thenThrow(unsupportedExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlDANotSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
aggStore.requestContentDirectUrl("urlDANotSupported", true, "anyfilename", 30L); aggStore.requestContentDirectUrl("urlDANotSupported", true, "anyfilename", "anyMimetype", 30L);
fail(); fail();
} }
catch (UnsupportedOperationException e) catch (UnsupportedOperationException e)
@@ -270,9 +270,9 @@ public class AggregatingContentStoreTest extends AbstractWritableContentStoreTes
// Content url not supported // Content url not supported
try try
{ {
when(primaryStoreMock.requestContentDirectUrl(eq("urlNotSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(primaryStoreMock.requestContentDirectUrl(eq("urlNotSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlNotSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlNotSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
aggStore.requestContentDirectUrl("urlNotSupported", true, "anyfilename", 30L); aggStore.requestContentDirectUrl("urlNotSupported", true, "anyfilename", "anyMimetype", 30L);
fail(); fail();
} }
catch (UnsupportedContentUrlException e) catch (UnsupportedContentUrlException e)
@@ -280,31 +280,31 @@ public class AggregatingContentStoreTest extends AbstractWritableContentStoreTes
// Expected // Expected
} }
when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
when(secondaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any(), any())).thenThrow(unsupportedExc);
directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
when(secondaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(secondaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
when(primaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any())).thenThrow(unsupportedExc); when(primaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any(), any())).thenThrow(unsupportedExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
when(primaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any())).thenThrow(unsupportedContentUrlExc); when(primaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any(), any())).thenThrow(unsupportedContentUrlExc);
when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(primaryStoreMock.requestContentDirectUrl(eq("urlPriSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any())).thenReturn(new DirectAccessUrl()); when(secondaryStoreMock.requestContentDirectUrl(eq("urlSecSupported"), any(), any(), any(), any())).thenReturn(new DirectAccessUrl());
directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlPriSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", 30L); directAccessUrl = aggStore.requestContentDirectUrl("urlSecSupported", true, "anyfilename", "anyMimetype", 30L);
assertNotNull(directAccessUrl); assertNotNull(directAccessUrl);
} }
} }