mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged 5.2.N (5.2.2) to HEAD (5.2)
134671 cpopa: Merged WEBAPP-API (5.2.1) to 5.2.N (5.2.1) 134630 cpopa: APPSREPO-105 : Add an API to download multiple file/folders as a zip - Added an API for creating a download, retrieving download info and canceling a download git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@137348 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
59
source/java/org/alfresco/rest/api/Downloads.java
Normal file
59
source/java/org/alfresco/rest/api/Downloads.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.rest.api;
|
||||
|
||||
import org.alfresco.rest.api.model.Download;
|
||||
|
||||
/**
|
||||
* downloads API
|
||||
*
|
||||
* @author cpopa
|
||||
*
|
||||
*/
|
||||
public interface Downloads
|
||||
{
|
||||
/**
|
||||
* Creates a download:download node.
|
||||
*
|
||||
* @param download
|
||||
* @return information about the newly created download:download node
|
||||
*/
|
||||
Download createDownloadNode(Download download);
|
||||
|
||||
/**
|
||||
* Get status info about a download node.
|
||||
*
|
||||
* @param downloadNodeId
|
||||
* @return status info about a download:download node
|
||||
*/
|
||||
Download getDownloadStatus(String downloadNodeId);
|
||||
|
||||
/**
|
||||
* Stop the zip creation if still in progress
|
||||
* @param downloadNodeId
|
||||
*/
|
||||
void cancel(String downloadNodeId);
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.rest.api.downloads;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.rest.api.Downloads;
|
||||
import org.alfresco.rest.api.model.Download;
|
||||
import org.alfresco.rest.framework.WebApiDescription;
|
||||
import org.alfresco.rest.framework.WebApiParam;
|
||||
import org.alfresco.rest.framework.core.ResourceParameter;
|
||||
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
|
||||
import org.alfresco.rest.framework.resource.EntityResource;
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
|
||||
import org.alfresco.rest.framework.resource.parameters.Parameters;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author cpopa
|
||||
*
|
||||
*/
|
||||
@EntityResource(name = "downloads", title = "Downloads")
|
||||
public class DownloadsEntityResource implements EntityResourceAction.Create<Download>, EntityResourceAction.ReadById<Download>, EntityResourceAction.Delete, InitializingBean
|
||||
{
|
||||
private Downloads downloads;
|
||||
|
||||
public void setDownloads(Downloads downloads)
|
||||
{
|
||||
this.downloads = downloads;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet()
|
||||
{
|
||||
ParameterCheck.mandatory("downloads", this.downloads);
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title = "Create download", description = "Create a download node whose content will be a zip which is being created asynchronously.", successStatus = HttpServletResponse.SC_ACCEPTED)
|
||||
@WebApiParam(name = "entity", title = "Download request", description = "Download request which contains the node ids for the zip elements.",
|
||||
kind = ResourceParameter.KIND.HTTP_BODY_OBJECT, allowMultiple = false)
|
||||
public List<Download> create(List<Download> entity, Parameters parameters)
|
||||
{
|
||||
Download downloadNode = downloads.createDownloadNode(entity.get(0));
|
||||
return Collections.singletonList(downloadNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title = "Get download information", description = "Get information about the progress of the zip creation.")
|
||||
@WebApiParam(name = "nodeId", title = "Download nodeId")
|
||||
public Download readById(String nodeId, Parameters parameters) throws EntityNotFoundException
|
||||
{
|
||||
return downloads.getDownloadStatus(nodeId);
|
||||
}
|
||||
|
||||
@WebApiDescription(title = "Cancel download", description = "Stop the zip creation if still in progress.", successStatus = HttpServletResponse.SC_ACCEPTED)
|
||||
@Override
|
||||
public void delete(String nodeId, Parameters parameters)
|
||||
{
|
||||
downloads.cancel(nodeId);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
@WebApi(name="alfresco", scope=Api.SCOPE.PUBLIC, version=1)
|
||||
package org.alfresco.rest.api.downloads;
|
||||
import org.alfresco.rest.framework.Api;
|
||||
import org.alfresco.rest.framework.WebApi;
|
175
source/java/org/alfresco/rest/api/impl/DownloadsImpl.java
Normal file
175
source/java/org/alfresco/rest/api/impl/DownloadsImpl.java
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.rest.api.impl;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.download.DownloadModel;
|
||||
import org.alfresco.rest.api.Downloads;
|
||||
import org.alfresco.rest.api.Nodes;
|
||||
import org.alfresco.rest.api.model.Download;
|
||||
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
|
||||
import org.alfresco.service.cmr.download.DownloadService;
|
||||
import org.alfresco.service.cmr.download.DownloadStatus;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.AccessStatus;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author cpopa
|
||||
*
|
||||
*/
|
||||
public class DownloadsImpl implements Downloads
|
||||
{
|
||||
private DownloadService downloadService;
|
||||
private NodeService nodeService;
|
||||
private Nodes nodes;
|
||||
private PermissionService permissionService;
|
||||
public static final String DEFAULT_ARCHIVE_NAME = "archive.zip";
|
||||
public static final String DEFAULT_ARCHIVE_EXTENSION = ".zip";
|
||||
|
||||
public void setDownloadService(DownloadService downloadService)
|
||||
{
|
||||
this.downloadService = downloadService;
|
||||
}
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
public void setNodes(Nodes nodes)
|
||||
{
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public void setPermissionService(PermissionService permissionService)
|
||||
{
|
||||
this.permissionService = permissionService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Download createDownloadNode(Download download)
|
||||
{
|
||||
checkEmptyNodeIds(download);
|
||||
|
||||
checkDuplicateNodeId(download);
|
||||
|
||||
NodeRef[] zipContentNodeRefs = validateAndGetNodeRefs(download);
|
||||
|
||||
checkNodeIdsReadPermission(zipContentNodeRefs);
|
||||
|
||||
NodeRef zipNodeRef = downloadService.createDownload(zipContentNodeRefs, true);
|
||||
|
||||
String archiveName = zipContentNodeRefs.length > 1 ?
|
||||
DEFAULT_ARCHIVE_NAME :
|
||||
nodeService.getProperty(zipContentNodeRefs[0], ContentModel.PROP_NAME) + DEFAULT_ARCHIVE_EXTENSION;
|
||||
|
||||
nodeService.setProperty(zipNodeRef, ContentModel.PROP_NAME, archiveName);
|
||||
Download downloadInfo = getStatus(zipNodeRef);
|
||||
return downloadInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Download getDownloadStatus(String downloadNodeId)
|
||||
{
|
||||
NodeRef downloadNodeRef = nodes.validateNode(downloadNodeId);
|
||||
|
||||
checkIsDownloadNodeType(downloadNodeRef);
|
||||
|
||||
Download downloadInfo = getStatus(downloadNodeRef);
|
||||
return downloadInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel(String downloadNodeId)
|
||||
{
|
||||
NodeRef downloadNodeRef = nodes.validateNode(downloadNodeId);
|
||||
checkIsDownloadNodeType(downloadNodeRef);
|
||||
|
||||
downloadService.cancelDownload(downloadNodeRef);
|
||||
}
|
||||
|
||||
protected NodeRef[] validateAndGetNodeRefs(Download download)
|
||||
{
|
||||
return download.getNodeIds().stream()
|
||||
.map(nodeRef -> nodes.validateNode(nodeRef))
|
||||
.toArray(NodeRef[]::new);
|
||||
}
|
||||
|
||||
protected void checkNodeIdsReadPermission(NodeRef[] zipContentNodeRefs)
|
||||
{
|
||||
for (NodeRef nodeRef : zipContentNodeRefs)
|
||||
{
|
||||
if (permissionService.hasReadPermission(nodeRef).equals(AccessStatus.DENIED)){
|
||||
throw new PermissionDeniedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkDuplicateNodeId(Download download)
|
||||
{
|
||||
if(download.getNodeIds().size() != new HashSet<String>(download.getNodeIds()).size()){
|
||||
throw new InvalidArgumentException("Cannot specify the same nodeId twice");
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkEmptyNodeIds(Download download)
|
||||
{
|
||||
if (download.getNodeIds().size() == 0)
|
||||
{
|
||||
throw new InvalidArgumentException("Cannot create an archive with 0 entries.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkIsDownloadNodeType(NodeRef downloadNodeRef)
|
||||
{
|
||||
QName nodeIdType = this.nodeService.getType(downloadNodeRef);
|
||||
|
||||
if(!nodeIdType.equals(DownloadModel.TYPE_DOWNLOAD)){
|
||||
throw new InvalidArgumentException("Please specify the nodeId of a download node.");
|
||||
}
|
||||
}
|
||||
|
||||
private Download getStatus(NodeRef downloadNodeRef)
|
||||
{
|
||||
DownloadStatus status = downloadService.getDownloadStatus(downloadNodeRef);
|
||||
Download downloadInfo = new Download();
|
||||
downloadInfo.setDownloadId(downloadNodeRef.getId());
|
||||
downloadInfo.setDone(status.getDone());
|
||||
downloadInfo.setFilesAdded(status.getFilesAdded());
|
||||
downloadInfo.setStatus(status.getStatus());
|
||||
downloadInfo.setTotalFiles(status.getTotalFiles());
|
||||
downloadInfo.setTotal(status.getTotal());
|
||||
return downloadInfo;
|
||||
}
|
||||
|
||||
}
|
131
source/java/org/alfresco/rest/api/model/Download.java
Normal file
131
source/java/org/alfresco/rest/api/model/Download.java
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
package org.alfresco.rest.api.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.cmr.download.DownloadStatus;
|
||||
|
||||
/**
|
||||
* Represents a download entity
|
||||
*
|
||||
*/
|
||||
public class Download
|
||||
{
|
||||
private String downloadId;
|
||||
private List<String> nodeIds;
|
||||
private DownloadStatus.Status status;
|
||||
private long done;
|
||||
private long total;
|
||||
private long filesAdded;
|
||||
private long totalFiles;
|
||||
|
||||
public String getDownloadId()
|
||||
{
|
||||
return downloadId;
|
||||
}
|
||||
|
||||
public void setDownloadId(String downloadId)
|
||||
{
|
||||
this.downloadId = downloadId;
|
||||
}
|
||||
|
||||
public List<String> getNodeIds()
|
||||
{
|
||||
return nodeIds;
|
||||
}
|
||||
|
||||
public void setNodeIds(List<String> nodeIds)
|
||||
{
|
||||
this.nodeIds = nodeIds;
|
||||
}
|
||||
|
||||
public DownloadStatus.Status getStatus()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(DownloadStatus.Status status)
|
||||
{
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public long getDone()
|
||||
{
|
||||
return done;
|
||||
}
|
||||
|
||||
public void setDone(long done)
|
||||
{
|
||||
this.done = done;
|
||||
}
|
||||
|
||||
public long getTotal()
|
||||
{
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(long total)
|
||||
{
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public long getFilesAdded()
|
||||
{
|
||||
return filesAdded;
|
||||
}
|
||||
|
||||
public void setFilesAdded(long filesAdded)
|
||||
{
|
||||
this.filesAdded = filesAdded;
|
||||
}
|
||||
|
||||
public long getTotalFiles()
|
||||
{
|
||||
return totalFiles;
|
||||
}
|
||||
|
||||
public void setTotalFiles(long totalFiles)
|
||||
{
|
||||
this.totalFiles = totalFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder builder = new StringBuilder(150);
|
||||
builder.append("Download [downloadId=").append(downloadId)
|
||||
.append(", nodeIds=").append(nodeIds)
|
||||
.append(", status=").append(status)
|
||||
.append(", done=").append(done)
|
||||
.append(", total=").append(total)
|
||||
.append(", filesAdded=").append(filesAdded)
|
||||
.append(", totalFiles=").append(totalFiles)
|
||||
.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user