Skip to content
BaseNodeController.java 1.51 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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public abstract class BaseNodeController {

    @Autowired
    private HttpServletRequest servletRequest;
    
    @Value("${vospace-authority}")
    protected String 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 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);
    }