Skip to content
Snippets Groups Projects

Immutable

Merged Nicola Fulvio Calabria requested to merge immutable into master
4 files
+ 161
235
Compare changes
  • Side-by-side
  • Inline

Files

@@ -6,14 +6,7 @@
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.LinkFoundException;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
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.vospace.v2.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,14 +16,13 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DeleteNodeController extends BaseNodeController {
private static final Logger LOG = LoggerFactory.getLogger(DeleteNodeController.class);
@Autowired
private NodeDAO nodeDAO;
DeleteNodeService deleteNodeService;
private static final Logger LOG = LoggerFactory.getLogger(DeleteNodeController.class);
@DeleteMapping(value = {"/nodes", "/nodes/**"},
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
@@ -39,54 +31,10 @@ public class DeleteNodeController extends BaseNodeController {
String path = getPath();
LOG.debug("deleteNode called for path {}", path);
// Check if the node is present,
// if the node does not exist the service SHALL throw a HTTP 404 status code
// including a NodeNotFound fault in the entity-body
// If note present, got it
Node toBeDeletedNode = nodeDAO.listNode(path)
.orElseThrow(() -> new NodeNotFoundException(path));
// If a parent node in the URI path is a LinkNode, the service SHALL throw
// a HTTP 400 status code including a LinkFound fault in the entity-body.
// For example, given the URI path /a/b/c, the service must throw a HTTP 400
// status code including a LinkFound fault in the entity-body if either /a
// or /a/b are LinkNodes.
List<String> pathComponents = NodeUtils.subPathComponents(path);
if (pathComponents.isEmpty()) {
// Manage root node
throw PermissionDeniedException.forPath("/");
} else {
// Manage all precursors in full path
for (int i = 0; i < pathComponents.size(); i++) {
String tmpPath = pathComponents.get(i);
Node mynode = nodeDAO.listNode(tmpPath)
.orElseThrow(() -> new NodeNotFoundException(tmpPath));
if (mynode.getType().equals("vos:LinkNode") && i < pathComponents.size()-1) // a LinkNode leaf can be deleted
throw new LinkFoundException(tmpPath);
}
}
if (!NodeUtils.checkIfWritable(toBeDeletedNode, principal.getName(), principal.getGroups())) {
throw PermissionDeniedException.forPath(path);
}
try {
nodeDAO.deleteNode(path);
deleteNodeService.doPreliminaryChecks(path);
deleteNodeService.deleteNode(path, principal);
return ResponseEntity.ok("Node deleted");
} catch(Exception ex) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
}
Loading