Skip to content
BaseNodeController.java 2.12 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
 */
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.InvalidArgumentException;
import it.inaf.oats.vospace.exception.InvalidURIException;
import net.ivoa.xml.vospace.v2.LinkNode;
import net.ivoa.xml.vospace.v2.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import static org.springframework.web.servlet.function.RequestPredicates.path;

public abstract class BaseNodeController {

    @Autowired
    private HttpServletRequest servletRequest;
    
    @Value("${vospace-authority}")
    protected String getPath() {       
        String requestURL = servletRequest.getRequestURL().toString();
        try {
            return NodeUtils.getPathFromRequestURLString(requestURL);
        } catch (IllegalArgumentException ex) {
            throw new InvalidURIException(ex);
        }
    }

    protected String getParentPath(String path) {
        return NodeUtils.getParentPath(path);
    protected void validateAndCheckPayloadURIConsistence(Node node) {
        // Get Node path (and validates it too)
        String decodedURIPathFromNode = URIUtils.returnVosPathFromNodeURI(node.getUri(), this.authority);

        // Check if payload URI is consistent with http request
        String requestPath = this.getPath();
        if (!decodedURIPathFromNode.equals(this.getPath())) {
            throw new InvalidURIException(decodedURIPathFromNode, requestPath);
        }
        
    }
    
    protected void validateInternalLinkNode(LinkNode linkNode) {
        String target = linkNode.getTarget();
        // I validate it here to add context easily
        if (target == null) {
            throw new InvalidArgumentException("LinkNode in payload has no target element specified");
        }

        URIUtils.returnVosPathFromNodeURI(linkNode.getTarget(), authority);
    }