Commit 97be478f authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Implemented basic node listing feature

parent 055784a1
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
target/**
nbactions.xml
nb-configuration.xml
/target/

nb-configuration.xml

deleted100644 → 0
+0 −18
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
    <!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
    <properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
        <!--
Properties that influence various parts of the IDE, especially code formatting and the like. 
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
        <netbeans.hint.jdkPlatform>JDK_15</netbeans.hint.jdkPlatform>
    </properties>
</project-shared-configuration>
+29 −35
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
    <artifactId>vospace-oats</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>vospace-oats</name>
    <description>Demo project for Spring Boot</description>
    <description>VOSpace REST service</description>

    <properties>
        <java.version>11</java.version>
@@ -36,28 +36,12 @@
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- JAXB dependency -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>
        
        <!-- Jackson-JAXB compatibility -->
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
@@ -81,6 +65,14 @@
            <groupId>it.oats.inaf</groupId>
            <artifactId>vospace-datamodel</artifactId>
            <version>1.0-SNAPSHOT</version>
            <exclusions>
                <!-- Transitive dependency excluded to avoid duplicated dependency issues.
                We want to use always the version provided by Spring Boot -->
                <exclusion>
                    <groupId>com.fasterxml.jackson.module</groupId>
                    <artifactId>jackson-module-jaxb-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
              
        <dependency>
@@ -89,16 +81,18 @@
            <version>2.0.0-SNAPSHOT</version>
        </dependency>
        
        <!-- Embedded PostgreSQL: -->
        <dependency>
            <groupId>it.inaf.ia2</groupId>
            <artifactId>rap-client</artifactId>
            <version>1.0-SNAPSHOT</version>
            <groupId>com.opentable.components</groupId>
            <artifactId>otj-pg-embedded</artifactId>
            <version>0.13.3</version>
            <scope>test</scope>
        </dependency>
              
        <dependency>
            <groupId>it.inaf.ia2</groupId>
            <artifactId>gms-client</artifactId>
            <version>1.0-SNAPSHOT</version>
            <groupId>io.zonky.test.postgres</groupId>
            <artifactId>embedded-postgres-binaries-linux-amd64</artifactId>
            <version>12.5.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
+26 −21
Original line number Diff line number Diff line
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.inaf.oats.vospace;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;

import net.ivoa.xml.vospace.v2.Node;

import it.inaf.oats.vospace.persistence.NodeDAO;

import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;

@RestController
public class ListNodeController {
@@ -25,14 +17,27 @@ public class ListNodeController {
    @Autowired
    private NodeDAO nodeDAO;

    @GetMapping(value="/{nodeName}")
    public ResponseEntity<List<Node>>listNodes(@PathVariable("nodeName")String node_name) {
        
        // dal nome del nodo devo ricavarmi l'ivo_id
        String node_ivo_id = "";
        return ResponseEntity.ok(nodeDAO.listNode(node_ivo_id));
        
    @GetMapping(value = {"/nodes", "/nodes/**"},
            produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE})
    public ResponseEntity<Node> listNode(HttpServletRequest request) {
        String path = getPath(request);
        return ResponseEntity.ok(nodeDAO.listNode(path));
    }

    /**
     * Slash is a special character in defining REST endpoints and trying to
     * define a PathVariable containing slashes doesn't work, so the endpoint
     * has been defined using "/nodes/**" instead of "/nodes/{path}" and the
     * path is extracted manually parsing the request URL.
     */
    private String getPath(HttpServletRequest request) {
        String requestURL = request.getRequestURL().toString();
        String[] split = requestURL.split("/nodes/");

        String path = "/";
        if (split.length == 2) {
            path += split[1];
        }
        return path;
    }
}
+64 −27
Original line number Diff line number Diff line
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.inaf.oats.vospace.persistence;

import net.ivoa.xml.vospace.v2.Node;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.http.ResponseEntity;

/**
 *
@@ -32,6 +20,8 @@ import org.springframework.http.ResponseEntity;
@Repository
public class NodeDAO {

    @Value("${vospace-authority}")
    private String authority;

    private final JdbcTemplate jdbcTemplate;

@@ -42,7 +32,6 @@ public class NodeDAO {

    public Node createNode(Node myNode) {

        
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ");
        sb.append("NodeProperty");
@@ -54,22 +43,70 @@ public class NodeDAO {
        return myNode;
    }

    public Node listNode(String path) {

        String sql = "SELECT os.os_path, n.node_id, type, async_trans, owner_id, group_read, group_write, is_public, content_length, created_on, last_modified from node n\n"
                + "JOIN node_os_path os ON n.node_id = os.node_id\n"
                + "WHERE n.path ~ (" + getFirstLevelChildrenSelector(path) + ")::lquery\n"
                + "OR os.os_path = ? ORDER BY os_path";

    public List<Node> listNode(String nodeIvoId) {

        String sql = "SELECT * FROM Node WHERE ivo_id=?";

        return jdbcTemplate.query(conn -> {
        List<Node> parentAndChildren = jdbcTemplate.query(conn -> {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, nodeIvoId);
            ps.setString(1, path);
            ps.setString(2, path);
            return ps;
        }, (row, index) -> {
            Node newNode = new Node();
            newNode.setUri(row.getString("ivo_id"));
            return newNode;
            return getNodeFromResultSet(row);
        });

        // Query returns parent as first node
        Node node = parentAndChildren.get(0);
        
        // Fill children
        if (node instanceof ContainerNode && parentAndChildren.size() > 1) {
            ContainerNode parent = (ContainerNode) node;
            for (int i = 1; i < parentAndChildren.size(); i++) {
                parent.getNodes().add(parentAndChildren.get(i));
            }
        }

        return node;
    }

    private String getFirstLevelChildrenSelector(String path) {
        String select = "(SELECT path FROM node WHERE node_id = (SELECT node_id FROM node_os_path WHERE os_path = ?))::varchar || '";

        if (!"/".equals(path)) {
            select += ".";
        }
        select += "*{1}'";
        return select;
    }

    private Node getNodeFromResultSet(ResultSet rs) throws SQLException {

        Node node = getTypedNode(rs.getString("type"));
        node.setUri(getUri(rs.getString("os_path")));

        return node;
    }

    private Node getTypedNode(String type) {
        Node node;
        switch (type) {
            case "container":
                node = new ContainerNode();
                break;
            case "data":
                node = new DataNode();
                break;
            default:
                throw new UnsupportedOperationException("Node type " + type + " not supported yet");
        }
        return node;
    }

    private String getUri(String path) {
        return "vos://" + authority + path;
    }
}
Loading