/* * This file is part of vospace-rest * Copyright (C) 2021 Istituto Nazionale di Astrofisica * SPDX-License-Identifier: GPL-3.0-or-later */ package it.inaf.oats.vospace; import it.inaf.ia2.aa.data.User; import it.inaf.oats.vospace.datamodel.NodeUtils; import it.inaf.oats.vospace.exception.InternalFaultException; import it.inaf.oats.vospace.exception.NodeBusyException; import it.inaf.oats.vospace.exception.NodeNotFoundException; import it.inaf.oats.vospace.exception.PermissionDeniedException; import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO.ShortNodeDescriptor; import java.util.Optional; import javax.servlet.http.HttpServletRequest; import net.ivoa.xml.vospace.v2.Transfer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.CannotSerializeTransactionException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; @Service @EnableTransactionManagement public class MoveService { @Autowired private NodeDAO nodeDao; @Value("${vospace-authority}") private String authority; @Autowired private HttpServletRequest servletRequest; @Transactional(rollbackFor = { Exception.class }, isolation = Isolation.REPEATABLE_READ) public void processMoveJob(Transfer transfer) { // Get Source Vos Path String sourcePath = transfer.getTarget().substring("vos://".length() + authority.length()); // Get Destination Vos Path (it's in transfer direction) String destinationPath = transfer.getDirection().substring("vos://".length() + authority.length()); // Extract User permissions from servlet request User user = (User) servletRequest.getUserPrincipal(); // Generic common validation for move process job paths this.validatePath(sourcePath); this.validatePath(destinationPath); if (sourcePath.equals(destinationPath)) { return; } // Check if destination is subpath of source // Linux-like: "cannot move to a subdirectory of itself" if(destinationPath.startsWith(sourcePath+"/")) { throw new IllegalArgumentException("Cannot move node to a subdirectory of its own path"); } try { // Get source node Optional sourceIdOpt = nodeDao.getNodeId(sourcePath); long sourceId = sourceIdOpt.orElseThrow(() -> new NodeNotFoundException(sourcePath)); if (nodeDao.isBranchBusy(sourceId)) { throw new NodeBusyException(sourcePath); } if (!nodeDao.isBranchWritable(sourceId, user.getName(), user.getGroups())) { throw new PermissionDeniedException(sourcePath); } Optional destShortNodeDescriptor = nodeDao.getShortNodeDescriptor(destinationPath, user.getName(), user.getGroups()); String destinationNodeLtreePath = null; if (destShortNodeDescriptor.isPresent()) { // When the destination is an existing ContainerNode, the source SHALL be placed under it (i.e., within the container) ShortNodeDescriptor snd = destShortNodeDescriptor.get(); if(snd.isBusy()) throw new NodeBusyException(destinationPath); if(snd.isPermissionDenied()) throw new PermissionDeniedException(destinationPath); if(!snd.isWritable()) throw new InternalFaultException("Destination is not writable: "+ destinationPath); if(!snd.isContainer()) throw new InternalFaultException("Existing destination is not a container: " + destinationPath); destinationNodeLtreePath = snd.getDestinationNodeLtreePath(); } else { // Compare source and destination paths parents and see if it's just a rename if (NodeUtils.getParentPath(sourcePath) .equals(NodeUtils.getParentPath(destinationPath))) { nodeDao.renameNode(sourceId, NodeUtils.getLastPathElement(destinationPath)); } else { throw new UnsupportedOperationException("Creation of destination upon move not supported"); // TODO (if we want this): modify the return type of createDestination to obtain an ltree path //parentDestLtree = this.createDestination(NodeUtils.getParentPath(destinationPath), user); } } if (destinationNodeLtreePath != null) { nodeDao.moveNodeBranch(sourceId, destinationNodeLtreePath); } } catch (CannotSerializeTransactionException ex) { // Concurrent transactions attempted to modify this set of nodes throw new NodeBusyException(sourcePath); } } private void validatePath(String path) { if (path.equals("/")) { throw new IllegalArgumentException("Cannot move root node or to root node"); } } /* // Returns node id of created destination private Long createDestination(String path, User user) { List components = NodeUtils.subPathComponents(path); for (int i = 0; i < components.size(); i++) { if (!this.checkNodeExistence(components.get(i))) { ContainerNode node = new ContainerNode(); node.setUri("vos://" + this.authority + components.get(i)); createNodeController.createNode(node, user); } } return nodeDao.getNodeId(path).orElseThrow(()-> new InternalFaultException("Unable to create destination at path: "+path)); }*/ }