2018-05-17 12:51:56 +02:00
|
|
|
package org.gcube.data.access.storagehub.services;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2018-10-25 16:33:23 +02:00
|
|
|
import java.net.MalformedURLException;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.net.URL;
|
2018-06-29 16:59:24 +02:00
|
|
|
import java.util.ArrayList;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.Arrays;
|
2018-10-25 16:33:23 +02:00
|
|
|
import java.util.Collections;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.Deque;
|
2018-05-28 12:01:01 +02:00
|
|
|
import java.util.LinkedList;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.List;
|
2018-05-28 12:01:01 +02:00
|
|
|
import java.util.Map;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.zip.Deflater;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
import javax.enterprise.context.RequestScoped;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.jcr.Node;
|
2018-06-29 16:59:24 +02:00
|
|
|
import javax.jcr.NodeIterator;
|
2018-10-25 16:33:23 +02:00
|
|
|
import javax.jcr.RepositoryException;
|
2018-05-17 12:51:56 +02:00
|
|
|
import javax.jcr.Session;
|
2018-06-29 16:59:24 +02:00
|
|
|
import javax.jcr.version.Version;
|
2018-05-17 12:51:56 +02:00
|
|
|
import javax.servlet.ServletContext;
|
2018-10-25 16:33:23 +02:00
|
|
|
import javax.ws.rs.Consumes;
|
2018-06-29 16:59:24 +02:00
|
|
|
import javax.ws.rs.DELETE;
|
2018-10-25 16:33:23 +02:00
|
|
|
import javax.ws.rs.FormParam;
|
2018-05-17 12:51:56 +02:00
|
|
|
import javax.ws.rs.GET;
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.StreamingOutput;
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.apache.commons.io.FilenameUtils;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse;
|
|
|
|
import org.gcube.common.storagehub.model.Excludes;
|
2018-06-29 16:59:24 +02:00
|
|
|
import org.gcube.common.storagehub.model.NodeConstants;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.gcube.common.storagehub.model.Paths;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters;
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.InvalidItemException;
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
|
|
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
|
|
|
import org.gcube.common.storagehub.model.items.Item;
|
|
|
|
import org.gcube.common.storagehub.model.items.SharedFolder;
|
2018-06-05 15:33:36 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.VreFolder;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.gcube.common.storagehub.model.service.ItemList;
|
|
|
|
import org.gcube.common.storagehub.model.service.ItemWrapper;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.common.storagehub.model.service.VersionList;
|
2018-06-05 15:33:36 +02:00
|
|
|
import org.gcube.common.storagehub.model.types.ItemAction;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.common.storagehub.model.types.NodeProperty;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.gcube.data.access.storagehub.AuthorizationChecker;
|
|
|
|
import org.gcube.data.access.storagehub.Constants;
|
|
|
|
import org.gcube.data.access.storagehub.Range;
|
|
|
|
import org.gcube.data.access.storagehub.SingleFileStreamingOutput;
|
|
|
|
import org.gcube.data.access.storagehub.Utils;
|
|
|
|
import org.gcube.data.access.storagehub.accounting.AccountingHandler;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.data.access.storagehub.handlers.ClassHandler;
|
2018-07-03 12:15:35 +02:00
|
|
|
import org.gcube.data.access.storagehub.handlers.CredentialHandler;
|
2018-10-25 16:33:23 +02:00
|
|
|
import org.gcube.data.access.storagehub.handlers.Item2NodeConverter;
|
|
|
|
import org.gcube.data.access.storagehub.handlers.Node2ItemConverter;
|
|
|
|
import org.gcube.data.access.storagehub.handlers.TrashHandler;
|
2018-06-29 16:59:24 +02:00
|
|
|
import org.gcube.data.access.storagehub.handlers.VersionHandler;
|
2018-07-05 16:26:08 +02:00
|
|
|
import org.gcube.smartgears.utils.InnerMethodName;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
@Path("items")
|
2018-05-17 12:51:56 +02:00
|
|
|
public class ItemsManager {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ItemsManager.class);
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
RepositoryInitializer repository;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
AccountingHandler accountingHandler;
|
|
|
|
|
|
|
|
@RequestScoped
|
|
|
|
@PathParam("id")
|
|
|
|
String id;
|
|
|
|
|
|
|
|
@Context
|
|
|
|
ServletContext context;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
AuthorizationChecker authChecker;
|
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
@Inject
|
|
|
|
VersionHandler versionHandler;
|
2018-10-25 16:33:23 +02:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
TrashHandler trashHandler;
|
|
|
|
|
|
|
|
@Inject Node2ItemConverter node2Item;
|
|
|
|
@Inject Item2NodeConverter item2Node;
|
|
|
|
|
2018-06-05 15:33:36 +02:00
|
|
|
@GET
|
2018-05-17 12:51:56 +02:00
|
|
|
@Path("{id}")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public ItemWrapper<Item> getById(@QueryParam("exclude") List<String> excludes){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("getById");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
|
|
|
Item toReturn = null;
|
|
|
|
try{
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
toReturn = node2Item.getItem(ses.getNodeByIdentifier(id), excludes);
|
|
|
|
}catch(RepositoryException re){
|
|
|
|
log.error("jcr error getting item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemWrapper<Item>(toReturn);
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
@GET
|
|
|
|
@Path("{id}/items/{name}")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public ItemList findChildrenByNamePattern(@QueryParam("exclude") List<String> excludes, @PathParam("name") String name){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("findChildrenByNamePattern");
|
2018-06-29 16:59:24 +02:00
|
|
|
Session ses = null;
|
|
|
|
List<Item> toReturn = new ArrayList<>();
|
|
|
|
try{
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-06-29 16:59:24 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
|
|
|
NodeIterator it = ses.getNodeByIdentifier(id).getNodes(name);
|
|
|
|
while (it.hasNext())
|
2018-10-25 16:33:23 +02:00
|
|
|
toReturn.add(node2Item.getItem(it.nextNode(), excludes));
|
|
|
|
}catch(RepositoryException re){
|
|
|
|
log.error("jcr error searching item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error searching item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-06-29 16:59:24 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemList(toReturn);
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("{id}/children/count")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-10-25 16:33:23 +02:00
|
|
|
public Long countById(@QueryParam("showHidden") Boolean showHidden, @QueryParam("exclude") List<String> excludes, @QueryParam("onlyType") String nodeType){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("countById");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
|
|
|
Long toReturn = null;
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
try{
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
toReturn = Utils.getItemCount(ses.getNodeByIdentifier(id), showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
|
|
|
|
}catch(RuntimeException | RepositoryException re){
|
|
|
|
log.error("jcr error counting item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error counting item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
return toReturn ;
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("{id}/children")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-10-25 16:33:23 +02:00
|
|
|
public ItemList listById(@QueryParam("showHidden") Boolean showHidden, @QueryParam("exclude") List<String> excludes, @QueryParam("onlyType") String nodeType){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("listById");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
|
|
|
List<? extends Item> toReturn = null;
|
|
|
|
try{
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, null, showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
|
|
|
|
}catch(RepositoryException re){
|
|
|
|
log.error("jcr error getting children", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting children", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemList(toReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("{id}/children/paged")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-10-25 16:33:23 +02:00
|
|
|
public ItemList listByIdPaged(@QueryParam("showHidden") Boolean showHidden, @QueryParam("start") Integer start, @QueryParam("limit") Integer limit, @QueryParam("exclude") List<String> excludes, @QueryParam("onlyType") String nodeType){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("listByIdPaged");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
|
|
|
List<? extends Item> toReturn = null;
|
|
|
|
try{
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, new Range(start, limit),showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
|
|
|
|
}catch(RepositoryException re){
|
|
|
|
log.error("jcr error getting paged children", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting paged children", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemList(toReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
2018-10-25 16:33:23 +02:00
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-17 12:51:56 +02:00
|
|
|
@Path("{id}/publiclink")
|
2018-10-25 16:33:23 +02:00
|
|
|
public URL getPublicLink(@QueryParam("version") String version) {
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("getPubliclink");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
2018-10-25 16:33:23 +02:00
|
|
|
URL toReturn = null;
|
2018-05-17 12:51:56 +02:00
|
|
|
try{
|
|
|
|
String login = AuthorizationProvider.instance.get().getClient().getId();
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
Node selectedNode = ses.getNodeByIdentifier(id);
|
|
|
|
|
|
|
|
Item item = node2Item.getItem(selectedNode, Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME));
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
if (!(item instanceof AbstractFileItem)) throw new InvalidCallParameters("the choosen item is not a File");
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
|
|
|
|
if (version!=null) {
|
|
|
|
boolean versionFound = false;
|
|
|
|
VersionList versions = getVersions();
|
|
|
|
for (org.gcube.common.storagehub.model.service.Version v: versions.getItemlist() )
|
|
|
|
if (v.getName().equals(version)) {
|
|
|
|
versionFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!versionFound) throw new InvalidCallParameters("the selected file has no version "+version);
|
|
|
|
}
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(selectedNode.getPath(), false, true, 0,login);
|
|
|
|
try {
|
|
|
|
selectedNode.setProperty(NodeProperty.IS_PUBLIC.toString(), true);
|
|
|
|
ses.save();
|
|
|
|
}finally {
|
|
|
|
ses.getWorkspace().getLockManager().unlock(selectedNode.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
String url = createPublicLink(version, id);
|
|
|
|
|
|
|
|
toReturn = new URL(url);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}catch(RepositoryException | MalformedURLException re ){
|
|
|
|
log.error("jcr error getting public link", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting public link", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String createPublicLink(String version, String id) {
|
|
|
|
String basepath = context.getInitParameter("resolver-basepath");
|
|
|
|
String filePublicUrl = String.format("%s/%s",basepath, id);
|
|
|
|
if (version!=null)
|
|
|
|
filePublicUrl = String.format("%s/%s", filePublicUrl, version);
|
|
|
|
return filePublicUrl;
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
@GET
|
|
|
|
@Path("{id}/rootSharedFolder")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public ItemWrapper<Item> getRootSharedFolder(@QueryParam("exclude") List<String> excludes){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("getRootSharedFolder");
|
2018-06-29 16:59:24 +02:00
|
|
|
Session ses = null;
|
2018-10-25 16:33:23 +02:00
|
|
|
Item sharedParent= null;
|
2018-06-29 16:59:24 +02:00
|
|
|
try{
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-06-29 16:59:24 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
Node currentNode =ses.getNodeByIdentifier(id);
|
|
|
|
Item currentItem = node2Item.getItem(currentNode, excludes);
|
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
if (!currentItem.isShared())
|
2018-10-25 16:33:23 +02:00
|
|
|
throw new InvalidItemException("this item is not shared");
|
|
|
|
log.trace("current node is {}",currentNode.getPath());
|
|
|
|
|
|
|
|
while (!node2Item.checkNodeType(currentNode, SharedFolder.class))
|
|
|
|
currentNode = currentNode.getParent();
|
|
|
|
|
|
|
|
sharedParent = node2Item.getItem(currentNode, excludes);
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error getting rootSharedFolder", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting rootSharedFolder", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
return new ItemWrapper<Item>(sharedParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("{id}/versions")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public VersionList getVersions(){
|
|
|
|
InnerMethodName.instance.set("getVersions");
|
|
|
|
Session ses = null;
|
|
|
|
List<org.gcube.common.storagehub.model.service.Version> versions = new ArrayList<>();
|
|
|
|
try{
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
|
|
|
|
|
|
|
Node node = ses.getNodeByIdentifier(id);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
Item currentItem = node2Item.getItem(node, Excludes.GET_ONLY_CONTENT);
|
|
|
|
if (!(currentItem instanceof AbstractFileItem))
|
|
|
|
throw new InvalidItemException("this item is not versioned");
|
|
|
|
|
|
|
|
List<Version> jcrVersions = versionHandler.getContentVersionHistory(node, ses);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
for (Version version: jcrVersions) {
|
|
|
|
boolean currentVersion = ((AbstractFileItem)currentItem).getContent().getStorageId().equals(version.getFrozenNode().getProperty(NodeProperty.STORAGE_ID.toString()).getString());
|
|
|
|
versions.add(new org.gcube.common.storagehub.model.service.Version(version.getIdentifier(), version.getName(), version.getCreated(), currentVersion));
|
|
|
|
}
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error retrieving versions", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error retrieving versions", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-06-29 16:59:24 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
return new VersionList(versions);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("{id}/versions/{version}/download")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public Response downloadVersion(@PathParam("version") String versionName){
|
|
|
|
InnerMethodName.instance.set("downloadSpecificVersion");
|
|
|
|
Session ses = null;
|
|
|
|
try{
|
|
|
|
String login = AuthorizationProvider.instance.get().getClient().getId();
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
|
|
|
Node node = ses.getNodeByIdentifier(id);
|
|
|
|
Item currentItem = node2Item.getItem(node, Excludes.ALL);
|
|
|
|
if (!(currentItem instanceof AbstractFileItem))
|
|
|
|
throw new InvalidItemException("this item is not a file");
|
|
|
|
|
|
|
|
List<Version> jcrVersions = versionHandler.getContentVersionHistory(ses.getNodeByIdentifier(id), ses);
|
|
|
|
|
|
|
|
for (Version version: jcrVersions) {
|
|
|
|
log.debug("retrieved version id {}, name {}", version.getIdentifier(), version.getName());
|
|
|
|
if (version.getName().equals(versionName)) {
|
|
|
|
long size = version.getFrozenNode().getProperty(NodeProperty.SIZE.toString()).getLong();
|
|
|
|
String mimeType = version.getFrozenNode().getProperty(NodeProperty.MIME_TYPE.toString()).getString();
|
|
|
|
String storageId = version.getFrozenNode().getProperty(NodeProperty.STORAGE_ID.toString()).getString();
|
|
|
|
|
|
|
|
final InputStream streamToWrite = Utils.getStorageClient(login).getClient().get().RFileAsInputStream(storageId);
|
|
|
|
|
|
|
|
String oldfilename = FilenameUtils.getBaseName(currentItem.getTitle());
|
|
|
|
String ext = FilenameUtils.getExtension(currentItem.getTitle());
|
|
|
|
|
|
|
|
String fileName = String.format("%s_v%s.%s", oldfilename, version.getName(), ext);
|
|
|
|
|
|
|
|
|
|
|
|
accountingHandler.createReadObj(fileName, ses, node, true);
|
|
|
|
|
|
|
|
StreamingOutput so = new SingleFileStreamingOutput(streamToWrite);
|
|
|
|
|
|
|
|
return Response
|
|
|
|
.ok(so)
|
|
|
|
.header("content-disposition","attachment; filename = \""+fileName+"\"")
|
|
|
|
.header("Content-Length", size)
|
|
|
|
.header("Content-Type", mimeType)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error downloading version", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error downloading version", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
return Response.serverError().build();
|
2018-06-29 16:59:24 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 12:01:01 +02:00
|
|
|
@GET
|
|
|
|
@Path("{id}/anchestors")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public ItemList getAnchestors(@QueryParam("exclude") List<String> excludes){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("getAnchestors");
|
2018-05-28 12:01:01 +02:00
|
|
|
org.gcube.common.storagehub.model.Path absolutePath = Utils.getHomePath();
|
|
|
|
Session ses = null;
|
|
|
|
List<Item> toReturn = new LinkedList<>();
|
|
|
|
try{
|
|
|
|
String login = AuthorizationProvider.instance.get().getClient().getId();
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-28 12:01:01 +02:00
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
Node currentNode = ses.getNodeByIdentifier(id);
|
|
|
|
Item currentItem = node2Item.getItem(currentNode, excludes);
|
|
|
|
log.trace("current node is {}",currentNode.getPath());
|
|
|
|
while (!(currentNode.getPath()+"/").equals(absolutePath.toPath())) {
|
2018-05-28 12:01:01 +02:00
|
|
|
if (currentItem instanceof SharedFolder){
|
|
|
|
Map<String, Object> users = ((SharedFolder) currentItem).getUsers().getValues();
|
|
|
|
String[] user = ((String)users.get(login)).split("/");
|
|
|
|
String parentId = user[0];
|
2018-10-25 16:33:23 +02:00
|
|
|
currentNode = ses.getNodeByIdentifier(parentId);
|
|
|
|
currentItem = node2Item.getItem(currentNode, excludes);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}else {
|
|
|
|
currentNode = currentNode.getParent();
|
|
|
|
currentItem = node2Item.getItem(currentNode, excludes);
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
log.trace("current node is {}",currentNode.getPath());
|
2018-05-28 12:01:01 +02:00
|
|
|
toReturn.add(currentItem);
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error getting anchestors", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error getting anchestors", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-28 12:01:01 +02:00
|
|
|
}finally{
|
|
|
|
if (ses!=null)
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
log.trace("item list to return is empty ? {}",toReturn.isEmpty());
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-05-28 12:01:01 +02:00
|
|
|
return new ItemList(toReturn);
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
@GET
|
|
|
|
@Path("{id}/download")
|
2018-10-25 16:33:23 +02:00
|
|
|
public Response download(@QueryParam("exclude") List<String> excludes){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("downloadById");
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
2018-10-25 16:33:23 +02:00
|
|
|
Response response = null;
|
2018-05-17 12:51:56 +02:00
|
|
|
try{
|
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-05-17 12:51:56 +02:00
|
|
|
final Node node = ses.getNodeByIdentifier(id);
|
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-10-25 16:33:23 +02:00
|
|
|
final Item item = node2Item.getItem(node, null);
|
2018-05-17 12:51:56 +02:00
|
|
|
if (item instanceof AbstractFileItem){
|
|
|
|
AbstractFileItem fileItem =(AbstractFileItem) item;
|
|
|
|
|
|
|
|
final InputStream streamToWrite = Utils.getStorageClient(login).getClient().get().RFileAsInputStream(fileItem.getContent().getStorageId());
|
|
|
|
|
|
|
|
accountingHandler.createReadObj(fileItem.getTitle(), ses, node, true);
|
|
|
|
|
|
|
|
StreamingOutput so = new SingleFileStreamingOutput(streamToWrite);
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
response = Response
|
2018-05-17 12:51:56 +02:00
|
|
|
.ok(so)
|
2018-10-25 16:33:23 +02:00
|
|
|
.header("content-disposition","attachment; filename = \""+fileItem.getName()+"\"")
|
2018-05-17 12:51:56 +02:00
|
|
|
.header("Content-Length", fileItem.getContent().getSize())
|
2018-10-25 16:33:23 +02:00
|
|
|
.header("Content-Type", fileItem.getContent().getMimeType())
|
2018-05-17 12:51:56 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
} else if (item instanceof FolderItem){
|
|
|
|
|
|
|
|
try {
|
2018-10-25 16:33:23 +02:00
|
|
|
final Deque<Item> allNodes = Utils.getAllNodesForZip((FolderItem)item, ses, accountingHandler, excludes);
|
|
|
|
final org.gcube.common.storagehub.model.Path originalPath = Paths.getPath(item.getParentPath());
|
2018-05-17 12:51:56 +02:00
|
|
|
StreamingOutput so = new StreamingOutput() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(OutputStream os) {
|
|
|
|
|
|
|
|
try(ZipOutputStream zos = new ZipOutputStream(os)){
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
log.debug("writing StreamOutput");
|
|
|
|
Utils.zipNode(zos, allNodes, login, originalPath);
|
|
|
|
log.debug("StreamOutput written in {}",(System.currentTimeMillis()-start));
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("error writing stream",e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
response = Response
|
2018-05-17 12:51:56 +02:00
|
|
|
.ok(so)
|
2018-10-25 16:33:23 +02:00
|
|
|
.header("content-disposition","attachment; filename = \""+item.getTitle()+".zip\"")
|
|
|
|
.header("Content-Type", "application/zip")
|
2018-05-17 12:51:56 +02:00
|
|
|
.header("Content-Length", -1l)
|
|
|
|
.build();
|
|
|
|
}finally {
|
|
|
|
if (ses!=null) ses.save();
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
} else throw new InvalidItemException("item type not supported for download: "+item.getClass());
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error download", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error download", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
} finally{
|
|
|
|
if (ses!=null) ses.logout();
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
|
|
|
|
return response;
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
@PUT
|
|
|
|
@Path("{id}/move")
|
2018-10-25 16:33:23 +02:00
|
|
|
public String move(@FormParam("destinationId") String destinationId){
|
2018-07-05 16:26:08 +02:00
|
|
|
InnerMethodName.instance.set("move");
|
2018-06-05 15:33:36 +02:00
|
|
|
//TODO: check if identifier is The Workspace root, or the thras folder or the VREFolder root or if the item is thrashed
|
2018-05-17 12:51:56 +02:00
|
|
|
Session ses = null;
|
|
|
|
try{
|
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-06-20 16:59:41 +02:00
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destinationId, true);
|
2018-10-25 16:33:23 +02:00
|
|
|
authChecker.checkWriteAuthorizationControl(ses, id, false);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
final Node nodeToMove = ses.getNodeByIdentifier(id);
|
2018-05-28 12:01:01 +02:00
|
|
|
final Node destination = ses.getNodeByIdentifier(destinationId);
|
2018-10-25 16:33:23 +02:00
|
|
|
Node originalParent = nodeToMove.getParent();
|
|
|
|
|
|
|
|
Item destinationItem = node2Item.getItem(destination,null);
|
|
|
|
final Item item = node2Item.getItem(nodeToMove, null);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
if (item instanceof SharedFolder)
|
|
|
|
throw new InvalidItemException("shared folder cannot be moved");
|
2018-06-05 15:33:36 +02:00
|
|
|
|
|
|
|
if (Constants.FOLDERS_TO_EXLUDE.contains(item.getTitle()) || Constants.FOLDERS_TO_EXLUDE.contains(destinationItem.getTitle()))
|
2018-10-25 16:33:23 +02:00
|
|
|
throw new InvalidItemException("protected folder cannot be moved");
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
if (!(destinationItem instanceof FolderItem))
|
|
|
|
throw new InvalidItemException("destination item is not a folder");
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
2018-06-05 15:33:36 +02:00
|
|
|
ses.getWorkspace().getLockManager().lock(nodeToMove.getPath(), true, true, 0,login);
|
2018-10-25 16:33:23 +02:00
|
|
|
try {
|
|
|
|
String uniqueName =(Utils.checkExistanceAndGetUniqueName(ses, destination, nodeToMove.getName()));
|
|
|
|
String newPath = String.format("%s/%s",destination.getPath(), uniqueName);
|
|
|
|
if (item instanceof FolderItem && Utils.hasSharedChildren(nodeToMove))
|
|
|
|
throw new InvalidItemException("folder item with shared children cannot be moved");
|
|
|
|
|
|
|
|
ses.getWorkspace().move(nodeToMove.getPath(), newPath);
|
|
|
|
Utils.setPropertyOnChangeNode(ses.getNode(newPath), login, ItemAction.MOVED);
|
|
|
|
|
|
|
|
String mimeTypeForAccounting = (item instanceof AbstractFileItem)? ((AbstractFileItem) item).getContent().getMimeType(): null;
|
|
|
|
|
|
|
|
accountingHandler.createFolderAddObj(uniqueName, item.getClass().getSimpleName(), mimeTypeForAccounting , ses, destination, false);
|
|
|
|
accountingHandler.createFolderRemoveObj(item.getTitle(), item.getClass().getSimpleName(), mimeTypeForAccounting, ses, originalParent, false);
|
|
|
|
ses.save();
|
|
|
|
}finally {
|
|
|
|
ses.getWorkspace().getLockManager().unlock(nodeToMove.getPath());
|
|
|
|
ses.getWorkspace().getLockManager().unlock(destination.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error moving item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error moving item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
|
|
|
} finally{
|
|
|
|
if (ses!=null) {
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
@PUT
|
|
|
|
@Path("{id}/copy")
|
|
|
|
public String copy(@FormParam("destinationId") String destinationId, @FormParam("fileName") String newFileName){
|
|
|
|
InnerMethodName.instance.set("copy");
|
|
|
|
//TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root or if the item is thrashed
|
|
|
|
Session ses = null;
|
|
|
|
String newFileIdentifier = null;
|
|
|
|
try{
|
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
|
|
|
//ses = RepositoryInitializer.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
|
|
|
//TODO check if it is possible to change all the ACL on a workspace
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destinationId, true);
|
|
|
|
authChecker.checkReadAuthorizationControl(ses, id);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
final Node nodeToCopy = ses.getNodeByIdentifier(id);
|
|
|
|
final Node destination = ses.getNodeByIdentifier(destinationId);
|
|
|
|
//Item destinationItem = node2Item.getItem(destination,null);
|
|
|
|
|
|
|
|
final Item item = node2Item.getItem(nodeToCopy, Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME));
|
|
|
|
|
|
|
|
if (item instanceof FolderItem)
|
|
|
|
throw new InvalidItemException("folder cannot be copied");
|
|
|
|
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
ses.getWorkspace().getLockManager().lock(nodeToCopy.getPath(), true, true, 0,login);
|
|
|
|
try {
|
|
|
|
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destination, newFileName);
|
|
|
|
String newPath= String.format("%s/%s", destination.getPath(), uniqueName);
|
|
|
|
ses.getWorkspace().copy(nodeToCopy.getPath(), newPath);
|
|
|
|
Node newNode = ses.getNode(newPath);
|
|
|
|
newFileIdentifier = newNode.getIdentifier();
|
|
|
|
//TODO: copy on storage and modify content
|
|
|
|
if (item instanceof AbstractFileItem) {
|
|
|
|
String newStorageID = Utils.getStorageClient(login).getClient().copyFile().from(((AbstractFileItem)item).getContent().getStorageId()).to(newPath);
|
|
|
|
((AbstractFileItem) item).getContent().setStorageId(newStorageID);
|
|
|
|
item2Node.replaceContent(ses, newNode, (AbstractFileItem) item, ItemAction.CLONED);
|
|
|
|
} else
|
|
|
|
Utils.setPropertyOnChangeNode(newNode, login, ItemAction.CLONED);
|
|
|
|
|
|
|
|
String mimeTypeForAccounting = (item instanceof AbstractFileItem)? ((AbstractFileItem) item).getContent().getMimeType(): null;
|
|
|
|
accountingHandler.createFolderAddObj(uniqueName, item.getClass().getSimpleName(), mimeTypeForAccounting, ses, destination, false);
|
|
|
|
|
|
|
|
ses.save();
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}finally {
|
|
|
|
ses.getWorkspace().getLockManager().unlock(nodeToCopy.getPath());
|
|
|
|
ses.getWorkspace().getLockManager().unlock(destination.getPath());
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error moving item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error moving item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-05-17 12:51:56 +02:00
|
|
|
} finally{
|
2018-06-05 15:33:36 +02:00
|
|
|
if (ses!=null) {
|
|
|
|
ses.logout();
|
|
|
|
}
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
return newFileIdentifier;
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
@PUT
|
|
|
|
@Path("{id}/rename")
|
|
|
|
public Response rename(@FormParam("newName") String newName){
|
|
|
|
InnerMethodName.instance.set("rename");
|
|
|
|
//TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root or if the item is thrashed
|
2018-06-05 15:33:36 +02:00
|
|
|
Session ses = null;
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-06-05 15:33:36 +02:00
|
|
|
try{
|
2018-10-25 16:33:23 +02:00
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-07-03 12:15:35 +02:00
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
authChecker.checkWriteAuthorizationControl(ses, id, false);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
final Node nodeToMove = ses.getNodeByIdentifier(id);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
final Item item = node2Item.getItem(nodeToMove, null);
|
2018-05-17 12:51:56 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
if (item instanceof SharedFolder)
|
|
|
|
throw new InvalidItemException("shared folder");
|
|
|
|
|
|
|
|
if (Constants.FOLDERS_TO_EXLUDE.contains(item.getTitle()))
|
|
|
|
throw new InvalidItemException("protected folder cannot be renamed");
|
|
|
|
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(nodeToMove.getPath(), true, true, 0,login);
|
|
|
|
ses.getWorkspace().getLockManager().lock(nodeToMove.getParent().getPath(), false, true, 0,login);
|
|
|
|
try {
|
|
|
|
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, nodeToMove.getParent(), newName);
|
|
|
|
|
|
|
|
String newPath = String.format("%s/%s", nodeToMove.getParent().getPath(), uniqueName);
|
|
|
|
nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName);
|
|
|
|
Utils.setPropertyOnChangeNode(nodeToMove, login, ItemAction.RENAMED);
|
|
|
|
ses.move(nodeToMove.getPath(), newPath);
|
|
|
|
accountingHandler.createRename(item.getTitle(), uniqueName, ses.getNode(newPath), ses, false);
|
|
|
|
ses.save();
|
|
|
|
}finally {
|
|
|
|
ses.getWorkspace().getLockManager().unlock(nodeToMove.getPath());
|
|
|
|
ses.getWorkspace().getLockManager().unlock(nodeToMove.getParent().getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error moving item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error moving item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
2018-06-05 15:33:36 +02:00
|
|
|
} finally{
|
|
|
|
if (ses!=null) {
|
|
|
|
ses.logout();
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-06-05 15:33:36 +02:00
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
return Response.ok(id).build();
|
2018-06-05 15:33:36 +02:00
|
|
|
}
|
2018-05-17 12:51:56 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
@PUT
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Path("/{id}/metadata")
|
|
|
|
public Response setProperties(org.gcube.common.storagehub.model.Metadata metadata){
|
|
|
|
InnerMethodName.instance.set("updateMetadata");
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
Session ses = null;
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
try{
|
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
|
|
|
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, id, false);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
final Node nodeToUpdate = ses.getNodeByIdentifier(id);
|
2018-06-29 16:59:24 +02:00
|
|
|
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
ses.getWorkspace().getLockManager().lock(nodeToUpdate.getPath(), false, true, 0,login);
|
|
|
|
try {
|
|
|
|
item2Node.updateMetadataNode(ses, nodeToUpdate, metadata.getValues(), login);
|
|
|
|
ses.save();
|
|
|
|
}finally {
|
|
|
|
ses.getWorkspace().getLockManager().unlock(nodeToUpdate.getPath());
|
|
|
|
}
|
|
|
|
//TODO: UPDATE accounting
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error moving item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error moving item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
|
|
|
} finally{
|
|
|
|
if (ses!=null) {
|
|
|
|
ses.logout();
|
|
|
|
}
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
return Response.ok(id).build();
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
@DELETE
|
|
|
|
@Path("{id}")
|
|
|
|
public Response deleteItem(){
|
|
|
|
InnerMethodName.instance.set("deleteItem");
|
|
|
|
//TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root
|
|
|
|
//TODO: check also that is not already trashed
|
|
|
|
Session ses = null;
|
|
|
|
try{
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
log.info("removing node with id {}", id);
|
|
|
|
|
|
|
|
//TODO check if it is possible to change all the ACL on a workspace
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, id, false);
|
|
|
|
|
|
|
|
final Node nodeToDelete = ses.getNodeByIdentifier(id);
|
|
|
|
|
|
|
|
Item itemToDelete = node2Item.getItem(nodeToDelete, Excludes.GET_ONLY_CONTENT);
|
|
|
|
|
|
|
|
if (itemToDelete instanceof SharedFolder || itemToDelete instanceof VreFolder || (itemToDelete instanceof FolderItem && Utils.hasSharedChildren(nodeToDelete)))
|
|
|
|
throw new InvalidItemException("SharedFolder, VreFolder or folders with shared children cannot be deleted");
|
2018-06-29 16:59:24 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
log.debug("item is trashed? {}", itemToDelete.isTrashed());
|
|
|
|
|
|
|
|
if (!itemToDelete.isTrashed())
|
|
|
|
trashHandler.moveToTrash(ses, nodeToDelete, itemToDelete);
|
|
|
|
else
|
|
|
|
trashHandler.removeNodes(ses, Collections.singletonList(itemToDelete));
|
|
|
|
|
|
|
|
}catch(RepositoryException re ){
|
|
|
|
log.error("jcr error moving item", re);
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
log.error("error moving item", she);
|
|
|
|
GXOutboundErrorResponse.throwException(she);
|
|
|
|
} finally{
|
|
|
|
if (ses!=null) {
|
|
|
|
ses.logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Response.ok().build();
|
2018-06-29 16:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|