Skip to content
MoveService.java 5.17 KiB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
/*
 * 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.ShortNodeDescriptor;
import java.util.Optional;
import net.ivoa.xml.vospace.v2.Transfer;
Sonia Zorba's avatar
Sonia Zorba committed
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
Sonia Zorba's avatar
Sonia Zorba committed
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@EnableTransactionManagement
public class MoveService extends AbstractNodeService {
Sonia Zorba's avatar
Sonia Zorba committed
    /**
     * Perform modeNode operation. User is passed as parameter because this method
     * is run in a separate thread and original HttpServletRequest is not available
     * anymore ("No thread-bound request found" would happen).
     */
Sonia Zorba's avatar
Sonia Zorba committed
    @Transactional(rollbackFor = { Exception.class }, isolation = Isolation.REPEATABLE_READ)
Sonia Zorba's avatar
Sonia Zorba committed
    public void processMoveJob(Transfer transfer, User user) {
        // Get Source Vos Path
        String sourcePath = URIUtils.returnVosPathFromNodeURI(transfer.getTarget(), authority);
        // Get Destination Vos Path (it's in transfer direction)
        String destinationPath = URIUtils.returnVosPathFromNodeURI(transfer.getDirection(), authority);
        // Generic common validation for move process job paths
        this.validatePath(sourcePath);
        this.validatePath(destinationPath);

        if (sourcePath.equals(destinationPath)) {
            throw new IllegalArgumentException("Cannot move node to itself");
        // 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");
        }

        // Check if destination equals parent path of source
        if(NodeUtils.getParentPath(sourcePath).equals(destinationPath)){
            return;
Sonia Zorba's avatar
Sonia Zorba committed
        try {
            // Get source node
            Optional<Long> sourceIdOpt = nodeDao.getNodeId(sourcePath);
            long sourceId = sourceIdOpt.orElseThrow(() -> new NodeNotFoundException(sourcePath));
Sonia Zorba's avatar
Sonia Zorba committed
            if (nodeDao.isBranchBusy(sourceId)) {
                throw new NodeBusyException(sourcePath);
            }
Sonia Zorba's avatar
Sonia Zorba committed
            if (!nodeDao.isBranchWritable(sourceId, user.getName(), user.getGroups())) {
                throw PermissionDeniedException.forPath(sourcePath);
            Optional<ShortNodeDescriptor> destShortNodeDescriptor 
                    = nodeDao.getShortNodeDescriptor(destinationPath, user.getName(), user.getGroups());
            String  destinationNodeLtreePath = null;
            if (destShortNodeDescriptor.isPresent()) {
Sonia Zorba's avatar
Sonia Zorba committed
                // 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 PermissionDeniedException.forPath(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();
                
Sonia Zorba's avatar
Sonia Zorba committed
                // 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");
Sonia Zorba's avatar
Sonia Zorba committed
                    // TODO (if we want this): modify the return type of createDestination to obtain an ltree path
                    // parentDestLtree = this.createDestination(NodeUtils.getParentPath(destinationPath), user);
Sonia Zorba's avatar
Sonia Zorba committed
                }
            if (destinationNodeLtreePath != null) {                
                nodeDao.moveNodeBranch(sourceId, destinationNodeLtreePath);
Sonia Zorba's avatar
Sonia Zorba committed
            }
Sonia Zorba's avatar
Sonia Zorba committed
        } catch (CannotSerializeTransactionException ex) {
            // Concurrent transactions attempted to modify this set of nodes            
            throw new NodeBusyException(sourcePath);