Java tutorial
/* * 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 hr.foi.sis.controllers; import hr.foi.sis.model.Person; import hr.foi.sis.model.TestData; import hr.foi.sis.repositories.TestDataRepository; import java.security.Principal; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * * @author paz */ @RestController @RequestMapping(value = "/testData") public class TestDataController { TestDataRepository testDataRepository; @Autowired public TestDataController(TestDataRepository testRepository) { this.testDataRepository = testRepository; } @RequestMapping(value = "/", method = RequestMethod.GET) @PreAuthorize("hasRole('ROLE_ADMIN')") public ResponseEntity<List<TestData>> retrieveAll() { Logger.getLogger("TestDataController.java").log(Level.INFO, "GET on /testData -- retrieving full list of testData"); return new ResponseEntity(this.testDataRepository.findAll(), HttpStatus.OK); } @RequestMapping(value = "/add", method = RequestMethod.POST) @PreAuthorize("isAuthenticated()") public ResponseEntity addData(@RequestBody TestData data) { Logger.getLogger("TestDataController.java").log(Level.INFO, "POST on /testdata/ "); Person creator = (Person) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Logger.getLogger("TestDataController.java").log(Level.INFO, "POST on /testdata/ for user" + creator.getName()); data.setPerson(creator); TestData saved = this.testDataRepository.save(data); if (saved != null) return new ResponseEntity(HttpStatus.OK); else return new ResponseEntity(HttpStatus.BAD_REQUEST); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @PreAuthorize("isAuthenticated() and principal.idPerson == #id") public ResponseEntity<List<TestData>> retrieveByPersonId(@PathVariable("id") long id) { Logger.getLogger("TestDataController.java").log(Level.INFO, "GET on /testdata/" + id); List<TestData> data = this.testDataRepository.findByPerson_IdPerson(id); if (data != null) { Logger.getLogger("TestDataController.java").log(Level.INFO, "Returning data /testdata/" + id); return new ResponseEntity(data, HttpStatus.OK); } else { Logger.getLogger("TestDataController.java").log(Level.INFO, "Data not found"); return new ResponseEntity(HttpStatus.BAD_REQUEST); } } }