Newer
Older
/*
* This file is part of vospace-rest
* Copyright (C) 2021 Istituto Nazionale di Astrofisica
* SPDX-License-Identifier: GPL-3.0-or-later
*/
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package it.inaf.oats.vospace;
import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.NodeBusyException;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.uws.v1.JobSummary;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MoveService {
@Autowired
private NodeDAO nodeDao;
@Autowired
private HttpServletRequest servletRequest;
public void processMoveJob(JobSummary job, Transfer transfer) {
// Get Source Path
String sourcePath = transfer.getTarget();
// Get Destination Path (it's in transfer direction)
String destinationPath = transfer.getDirection();
// Extract User permissions from servlet request
User user = (User) servletRequest.getUserPrincipal();
Long sourceId = nodeDao.getNodeId(sourcePath);
List<Node> branchList = nodeDao.listNodesInBranch(sourceId, true);
// Check feasibility of move on source branch
if (!isWritePermissionsValid(branchList, user)) {
throw new PermissionDeniedException(sourcePath);
}
if(sourcePath.equals(destinationPath))
return;
if(!isMoveable(branchList)) {
throw new NodeBusyException(sourcePath);
}
// Set branch at busy
nodeDao.setBranchBusy(sourceId, true);
// Compare source and destination paths and see if it's just a rename
if(NodeUtils.getParentPath(sourcePath).equals(NodeUtils.getParentPath(destinationPath)))
{
nodeDao.renameNode(sourceId, NodeUtils.getLastPathElement(destinationPath));
} else {
this.moveNode(sourceId, sourcePath, destinationPath, user);
}
nodeDao.setBranchBusy(sourceId, false);
}
// All nodes must be writable by the user to have a true
private boolean isWritePermissionsValid(List<Node> list, User user) {
String userName = user.getName();
List<String> userGroups = user.getGroups();
return list.stream().allMatch((n) -> {
return NodeUtils.checkIfWritable(n, userName, userGroups);
});
}
// All nodes must comply to have a true
private boolean isMoveable(List<Node> list) {
return list.stream().allMatch((n) -> {
boolean busy = NodeUtils.getIsBusy(n);
boolean sticky
= Boolean.valueOf(
NodeProperties.getNodePropertyByURI(n,
NodeProperties.STICKY_URN));
return (!busy && !sticky);
});
}
private void moveNode(Long sourceId, String sourcePath, String destPath, User user)
{
}
}