Commit 9b7177fc authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Configured TokenFilter. Retrieved user groups in CreateNodeController. Changes on unit tests

parent 6ef44fe2
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseNodeController {

    @Autowired
    private HttpServletRequest servletRequest;

    /**
     * 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.
     */
    protected String getPath() {
        String requestURL = servletRequest.getRequestURL().toString();
        String[] split = requestURL.split("/nodes/");

        String path = "/";
        if (split.length == 2) {
            path += split[1];
        }
        return path;
    }
}
+15 −27
Original line number Diff line number Diff line
package it.inaf.oats.vospace;

import it.inaf.ia2.aa.data.User;
import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

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

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

import java.util.List;
import org.springframework.web.bind.annotation.PutMapping;

@RestController
public class CreateNodeController {
public class CreateNodeController extends BaseNodeController {

    @Autowired
    NodeDAO node_dao;
    private NodeDAO nodeDao;

    @PostMapping(value = "/{path}",
    @PutMapping(value = {"/nodes", "/nodes/**"},
            consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public Node createNode(@PathVariable("path") String path, @RequestBody Node node) {
        
        System.out.println("In createNodeController");
        node_dao.createNode(node);
        return node;
    }
    
    private class RequestWrapper {

    List<Property> nodeProperty;
    String nodeId;
    String nodeType;
    public Node createNode(@RequestBody Node node, User principal) {

        String path = getPath();

        List<String> userGroups = principal.getGroups();

        nodeDao.createNode(node);
        return node;
    }
    
}
+2 −19
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;

@RestController
public class ListNodeController {
public class ListNodeController extends BaseNodeController {

    @Autowired
    private NodeDAO nodeDAO;
@@ -20,24 +20,7 @@ public class ListNodeController {
    @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);
        String path = getPath();
        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;
    }
}
+0 −23
Original line number Diff line number Diff line
package it.inaf.oats.vospace;

import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import it.inaf.ia2.aa.data.User;

@RestController
public class PrivateController {

    @GetMapping(value = "/private",
             produces = {MediaType.APPLICATION_JSON_VALUE})
    public User getUser(HttpServletRequest request) {
        User user = (User)request.getUserPrincipal();
        return user;
    }

}
+4 −4
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import it.inaf.ia2.aa.LoginFilter;
import it.inaf.ia2.aa.TokenFilter;

@SpringBootApplication
public class VospaceApplication {
@@ -14,10 +14,10 @@ public class VospaceApplication {
    }

    @Bean
    public FilterRegistrationBean loginFilterRegistration() {
    public FilterRegistrationBean tokenFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/private/*");
        registration.setFilter(new TokenFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }
}
Loading