Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Allow adding custom aspects to multipart upload #232

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/main/java/org/alfresco/rest/api/impl/NodesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2017 Alfresco Software Limited
* Copyright (C) 2005 - 2019 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
Expand Down Expand Up @@ -72,9 +72,6 @@
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.thumbnail.ThumbnailDefinition;
import org.alfresco.repo.thumbnail.ThumbnailHelper;
import org.alfresco.repo.thumbnail.ThumbnailRegistry;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.version.VersionModel;
Expand Down Expand Up @@ -103,7 +100,6 @@
import org.alfresco.rest.framework.core.exceptions.NotFoundException;
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
import org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException;
import org.alfresco.rest.framework.core.exceptions.StaleEntityException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedMediaTypeException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.content.BinaryResource;
Expand Down Expand Up @@ -1878,6 +1874,7 @@ public Node createNode(String parentFolderNodeId, Node nodeInfo, Parameters para
return newNode;
}

@Override
public void addCustomAspects(NodeRef nodeRef, List<String> aspectNames, List<QName> excludedAspects)
{
if (aspectNames == null)
Expand Down Expand Up @@ -2866,6 +2863,7 @@ public Node upload(String parentFolderNodeId, FormData formData, Parameters para
String versionComment = null;
String relativePath = null;
String renditionNames = null;
String aspectNames = null;

Map<String, Object> qnameStrProps = new HashMap<>();
Map<QName, Serializable> properties = null;
Expand Down Expand Up @@ -2923,6 +2921,10 @@ public Node upload(String parentFolderNodeId, FormData formData, Parameters para
renditionNames = getStringOrNull(field.getValue());
break;

case "aspectnames":
aspectNames = getStringOrNull(field.getValue());
break;

default:
{
final String propName = field.getName();
Expand Down Expand Up @@ -2957,7 +2959,8 @@ public Node upload(String parentFolderNodeId, FormData formData, Parameters para
// if requested, make (get or create) path
parentNodeRef = getOrCreatePath(parentNodeRef, relativePath);
final QName assocTypeQName = ContentModel.ASSOC_CONTAINS;
final Set<String> renditions = getRequestedRenditions(renditionNames);
final Set<String> renditions = splitCommaSeparatedString(renditionNames);
final Set<String> aspects = splitCommaSeparatedString(aspectNames);

try
{
Expand Down Expand Up @@ -3009,7 +3012,15 @@ else if (overwrite && nodeService.hasAspect(existingFile, ContentModel.ASPECT_VE
checkRenditionNames(renditions);
requestRenditions(renditions, fileNode);

return fileNode;
if(aspects != null)
{
List<String> aspectsList = new ArrayList<>(aspects);
addCustomAspects(nodeRef, aspectsList, EXCLUDED_ASPECTS);
}

Node newNode = getFolderOrDocument(nodeRef.getId(), parameters);

return newNode;

// Do not clean formData temp files to allow for retries.
// Temp files will be deleted later when GC call DiskFileItem#finalize() method or by temp file cleaner.
Expand Down Expand Up @@ -3096,25 +3107,25 @@ private void checkRenditionNames(Set<String> renditionNames)
}
}

static Set<String> getRequestedRenditions(String renditionsParam)
static Set<String> splitCommaSeparatedString(String listString)
{
if (renditionsParam == null)
if (listString == null)
{
return null;
}

String[] renditionNames = renditionsParam.split(",");
String[] subValues = listString.split(",");

Set<String> renditions = new LinkedHashSet<>(renditionNames.length);
for (String name : renditionNames)
Set<String> subValuesSet = new LinkedHashSet<>(subValues.length);
for (String name : subValues)
{
name = name.trim();
if (!name.isEmpty())
{
renditions.add(name.trim());
subValuesSet.add(name.trim());
}
}
return renditions;
return subValuesSet;
}

private void requestRenditions(Set<String> renditionNames, Node fileNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public void createRenditions(NodeRef nodeRef, List<Rendition> renditions, Parame
for (Rendition rendition : renditions)
{
String name = getName(rendition);
Set<String> requestedRenditions = NodesImpl.getRequestedRenditions(name);
Set<String> requestedRenditions = NodesImpl.splitCommaSeparatedString(name);
if (requestedRenditions == null)
{
renditionNames.add(null);
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/alfresco/rest/api/tests/NodeApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2638,6 +2638,28 @@ public void testMultipartFormDataUpload() throws Exception

// Upload with multipart with missing boundary e.g. multipart/form-data; boundary=7cgH0q1hSgrlmU7tUe8kGSWT4Un6aRH
post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, "multipart/form-data", 415);

// set aspect which doesn't exist
List<String> aspects = new ArrayList<>();
aspects.add("hackathon:2019");
aspects.add("papi:dessertable");
multiPartBuilder.setAspects(aspects);
reqBody = multiPartBuilder.build();
post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 400);

// set aspect which exist
aspects = new ArrayList<>();
aspects.add("papi:dessertable");
multiPartBuilder.setAspects(aspects);
reqBody = multiPartBuilder.build();
HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);

Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);

// Check the upload response
assertEquals("quick-1.txt", document.getName());
assertNotNull(document.getAspectNames());
assertTrue("Doesn't contains papi:dessertable in aspects", document.getAspectNames().contains("papi:dessertable"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public MultiPartRequest build() throws IOException
addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
addPartIfNotNull(parts, "description", description);
addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
addPartIfNotNull(parts, "aspectnames", getCommaSeparated(aspects));
addPartIfNotNull(parts, "majorversion", majorVersion);
addPartIfNotNull(parts, "overwrite", overwrite);
addPartIfNotNull(parts, "autorename", autoRename);
Expand Down