Skip to content
CreateNodeController.java 2.16 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
 */
Sara Bertocco's avatar
Sara Bertocco committed
package it.inaf.oats.vospace;

import it.inaf.oats.vospace.exception.InvalidURIException;
import net.ivoa.xml.vospace.v2.LinkNode;
Sara Bertocco's avatar
Sara Bertocco committed
import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Sara Bertocco's avatar
Sara Bertocco committed

@RestController
public class CreateNodeController extends BaseNodeController {
Sara Bertocco's avatar
Sara Bertocco committed

    private static final Logger LOG = LoggerFactory.getLogger(CreateNodeController.class);
    private CreateNodeService createNodeService;
            consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public Node createNode(@RequestBody Node node, User principal) {
        String path = getPath();
        LOG.debug("createNodeController called for node with URI {} and PATH {}", node.getUri(), path);
        // Get Node path (and validates it too)
        String decodedURIPathFromNode = URIUtils.returnVosPathFromNodeURI(node.getUri(), this.authority);

        LOG.debug("createNodeController URI: {} decoded as {}", node.getUri(), decodedURIPathFromNode);

        // Check if payload URI is consistent with http request
        if (!decodedURIPathFromNode.equals(path)) {
            throw new InvalidURIException(decodedURIPathFromNode, path);
        }

        // validate format of input node
        this.validateInputNode(node);

        return createNodeService.createNode(node, path, principal);
    private void validateInputNode(Node node) {

        if (node instanceof LinkNode) {
            this.validateInternalLinkNode((LinkNode) node);
Sara Bertocco's avatar
Sara Bertocco committed
}