Merged HEAD (5.2) to 5.2.N (5.2.1)

126556 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      124269 jvonka: RA-834: Optionally request generation of a rendition on content creation (eg. file upload)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126902 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 12:08:24 +00:00
parent 061e6a1662
commit e212513df3
5 changed files with 244 additions and 29 deletions

View File

@@ -428,6 +428,31 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi
protected static final long PAUSE_TIME = 5000; //millisecond
protected static final int MAX_RETRY = 10;
protected Rendition waitAndGetRendition(String userId, String sourceNodeId, String renditionId) throws Exception
{
int retryCount = 0;
while (retryCount < MAX_RETRY)
{
try
{
HttpResponse response = getSingle(getNodeRenditionsUrl(sourceNodeId), userId, renditionId, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
return rendition;
}
catch (AssertionError ex)
{
// If the asynchronous create rendition action is not finished yet,
// wait for 'PAUSE_TIME' and try again.
retryCount++;
Thread.sleep(PAUSE_TIME);
}
}
return null;
}
protected Rendition createAndGetRendition(String userId, String sourceNodeId, String renditionId) throws Exception
{
Rendition renditionRequest = new Rendition();
@@ -451,27 +476,7 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi
}
}
retryCount = 0;
while (retryCount < MAX_RETRY)
{
try
{
HttpResponse response = getSingle(getNodeRenditionsUrl(sourceNodeId), userId, renditionId, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
return rendition;
}
catch (AssertionError ex)
{
// If the asynchronous create rendition action is not finished yet,
// wait for 'PAUSE_TIME' and try again.
retryCount++;
Thread.sleep(PAUSE_TIME);
}
}
return null;
return waitAndGetRendition(userId, sourceNodeId, renditionId);
}
protected String getNodeRenditionsUrl(String nodeId)

View File

@@ -20,6 +20,7 @@
package org.alfresco.rest.api.tests;
import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString;
import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsStringNonNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -58,6 +59,7 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -448,6 +450,97 @@ public class RenditionsTest extends AbstractBaseApiTest
}
}
/**
* Tests create rendition when on upload/create of a file
*
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testCreateRenditionOnUpload() throws Exception
{
String userId = userOneN1.getId();
// Create a folder within the site document's library
String folderName = "folder" + System.currentTimeMillis();
String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userId);
// Create multipart request
String renditionName = "doclib";
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create()
.setFileData(new FileData(fileName, file, MimetypeMap.MIMETYPE_PDF))
.setRenditions(Collections.singletonList(renditionName));
MultiPartRequest reqBody = multiPartBuilder.build();
// Upload quick.pdf file into 'folder' - including request to create 'doclib' thumbnail
HttpResponse response = post(getNodeChildrenUrl(folder_Id), userId, reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// wait and check that rendition is created ...
Rendition rendition = waitAndGetRendition(userId, contentNodeId, renditionName);
assertNotNull(rendition);
assertEquals(RenditionStatus.CREATED, rendition.getStatus());
// also accepted for JSON when creating empty file (albeit with no content)
Document d1 = new Document();
d1.setName("d1.txt");
d1.setNodeType("cm:content");
ContentInfo ci = new ContentInfo();
ci.setMimeType("text/plain");
d1.setContent(ci);
// create empty file including request to generate imgpreview thumbnail
renditionName = "imgpreview";
response = post(getNodeChildrenUrl(folder_Id), userId, toJsonAsStringNonNull(d1), "?renditions="+renditionName, 201);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String d1Id = documentResp.getId();
// wait and check that rendition is created ...
rendition = waitAndGetRendition(userId, d1Id, renditionName);
assertNotNull(rendition);
assertEquals(RenditionStatus.CREATED, rendition.getStatus());
// -ve - currently we do not support multiple rendition requests on create
reqBody = MultiPartBuilder.create()
.setFileData(new FileData(fileName, file, MimetypeMap.MIMETYPE_PDF))
.setRenditions(Arrays.asList(new String[]{"doclib,imgpreview"}))
.build();
post(getNodeChildrenUrl(folder_Id), userId, reqBody.getBody(), null, reqBody.getContentType(), 400);
// -ve
reqBody = MultiPartBuilder.create()
.setFileData(new FileData(fileName, file, MimetypeMap.MIMETYPE_PDF))
.setRenditions(Arrays.asList(new String[]{"unknown"}))
.build();
post(getNodeChildrenUrl(folder_Id), userId, reqBody.getBody(), null, reqBody.getContentType(), 404);
// -ve
ThumbnailService thumbnailService = applicationContext.getBean("thumbnailService", ThumbnailService.class);
thumbnailService.setThumbnailsEnabled(false);
try
{
// Create multipart request
String txtFileName = "quick-1.txt";
File txtFile = getResourceFile(fileName);
reqBody = MultiPartBuilder.create()
.setFileData(new FileData(txtFileName, txtFile, MimetypeMap.MIMETYPE_TEXT_PLAIN))
.setRenditions(Arrays.asList(new String[]{"doclib"}))
.build();
post(getNodeChildrenUrl(folder_Id), userId, reqBody.getBody(), null, reqBody.getContentType(), 501);
}
finally
{
thumbnailService.setThumbnailsEnabled(true);
}
}
/**
* Tests download rendition.
* <p>GET:</p>

View File

@@ -60,6 +60,7 @@ public class MultiPartBuilder
private Boolean overwrite;
private Boolean autoRename;
private String nodeType;
private List<String> renditionIds = Collections.emptyList(); // initially single rendition name/id (in the future we may support multiple)
private Map<String, String> properties = Collections.emptyMap();
private MultiPartBuilder()
@@ -78,6 +79,7 @@ public class MultiPartBuilder
this.overwrite = that.overwrite;
this.autoRename = that.autoRename;
this.nodeType = that.nodeType;
this.renditionIds = that.renditionIds;
this.properties = new HashMap<>(that.properties);
}
@@ -157,12 +159,18 @@ public class MultiPartBuilder
return this;
}
private String getAspects(List<String> aspects)
public MultiPartBuilder setRenditions(List<String> renditionIds)
{
if (!aspects.isEmpty())
this.renditionIds = renditionIds;
return this;
}
private String getCommaSeparated(List<String> names)
{
if (! names.isEmpty())
{
StringBuilder sb = new StringBuilder(aspects.size() * 2);
for (String str : aspects)
StringBuilder sb = new StringBuilder(names.size() * 2);
for (String str : names)
{
sb.append(str).append(',');
}
@@ -262,11 +270,12 @@ public class MultiPartBuilder
addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
addPartIfNotNull(parts, "description", description);
addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
addPartIfNotNull(parts, "aspects", getAspects(aspects));
addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
addPartIfNotNull(parts, "majorversion", majorVersion);
addPartIfNotNull(parts, "overwrite", overwrite);
addPartIfNotNull(parts, "autorename", autoRename);
addPartIfNotNull(parts, "nodetype", nodeType);
addPartIfNotNull(parts, "renditions", getCommaSeparated(renditionIds));
if (!properties.isEmpty())
{