Skip to content
MoveService.java 6.08 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;
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;
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 {

    @Autowired
    private NodeDAO nodeDao;

    @Value("${vospace-authority}")
    private String authority;
    @Autowired
    private HttpServletRequest servletRequest;
    
Sonia Zorba's avatar
Sonia Zorba committed
    @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)) {
        // 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");
        }        
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 new PermissionDeniedException(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 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();
                
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);
                }
            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);
    }

    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<String> 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));