Commit 6b54c130 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Added ErrorController and NodeNotFoundException

parent 9b7177fc
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
package it.inaf.oats.vospace;

import it.inaf.oats.vospace.exception.NodeNotFoundException;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -21,6 +22,7 @@ public class ListNodeController extends BaseNodeController {
            produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE})
    public ResponseEntity<Node> listNode(HttpServletRequest request) {
        String path = getPath();
        return ResponseEntity.ok(nodeDAO.listNode(path));
        return ResponseEntity.ok(nodeDAO.listNode(path)
                .orElseThrow(() -> new NodeNotFoundException(path)));
    }
}
+33 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace.exception;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class ErrorController extends AbstractErrorController {

    @Autowired
    public ErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping(produces = MediaType.TEXT_XML_VALUE)
    public void errorText(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> errors = super.getErrorAttributes(request, true);
        response.setContentType("text/plain;charset=UTF-8");
        response.getOutputStream().print((String) errors.get("message"));
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}
+12 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NodeNotFoundException extends VoSpaceException {

    public NodeNotFoundException(String path) {
        super("NodeNotFound: " + path);
    }
}
+8 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace.exception;

public class VoSpaceException extends RuntimeException {

    public VoSpaceException(String message) {
        super(message);
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
@@ -43,7 +44,7 @@ public class NodeDAO {
        return myNode;
    }

    public Node listNode(String path) {
    public Optional<Node> listNode(String path) {

        String sql = "SELECT os.vos_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_vos_path os ON n.node_id = os.node_id\n"
@@ -59,6 +60,10 @@ public class NodeDAO {
            return getNodeFromResultSet(row);
        });

        if(parentAndChildren.isEmpty()) {
            return Optional.empty();
        }
        
        // Query returns parent as first node
        Node node = parentAndChildren.get(0);
        
@@ -70,7 +75,7 @@ public class NodeDAO {
            }
        }

        return node;
        return Optional.of(node);
    }

    private String getFirstLevelChildrenSelector(String path) {
Loading