-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added service and controller to expose API for CRUD operations around custom attribute definitions
- Loading branch information
Showing
4 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...in/java/edu/internet2/tier/shibboleth/admin/ui/controller/CustomAttributesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
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.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.CustomAttributeDefinition; | ||
import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor; | ||
import edu.internet2.tier.shibboleth.admin.ui.service.CustomAttributesService; | ||
|
||
@Controller | ||
@RequestMapping(value = "/api/custom") | ||
public class CustomAttributesController { | ||
@Autowired | ||
private CustomAttributesService caService; | ||
|
||
@PostMapping("/attribute") | ||
@Transactional | ||
public ResponseEntity<?> create(@RequestBody CustomAttributeDefinition definition) { | ||
// If already defined, we can't create a new one, nor will this call update the definition | ||
CustomAttributeDefinition cad = caService.find(definition.getName()); | ||
|
||
if (cad != null) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation(ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/attribute").build().toUri()); | ||
|
||
return ResponseEntity.status(HttpStatus.CONFLICT).headers(headers) | ||
.body(new ErrorResponse(String.valueOf(HttpStatus.CONFLICT.value()), | ||
String.format("The custom attribute definition with name: [%s] already exists.", definition.getName()))); | ||
} | ||
|
||
CustomAttributeDefinition result = caService.createOrUpdateDefinition(definition); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(result); | ||
} | ||
|
||
@PutMapping("/attribute") | ||
@Transactional | ||
public ResponseEntity<?> update(@RequestBody CustomAttributeDefinition definition) { | ||
CustomAttributeDefinition cad = caService.find(definition.getName()); | ||
if (cad == null) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation(ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/attribute").build().toUri()); | ||
|
||
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers) | ||
.body(new ErrorResponse(String.valueOf(HttpStatus.NOT_FOUND.value()), | ||
String.format("The custom attribute definition with name: [%s] does not already exist.", definition.getName()))); | ||
} | ||
|
||
CustomAttributeDefinition result = caService.createOrUpdateDefinition(definition); | ||
return ResponseEntity.ok(result); | ||
} | ||
|
||
@GetMapping("/attributes") | ||
@Transactional(readOnly = true) | ||
public ResponseEntity<?> getAll() { | ||
return ResponseEntity.ok(caService.getAllDefinitions()); | ||
} | ||
|
||
@GetMapping("/attribute/{name}") | ||
@Transactional(readOnly = true) | ||
public ResponseEntity<?> getOne(@PathVariable String name) { | ||
CustomAttributeDefinition cad = caService.find(name); | ||
if (cad == null) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation( | ||
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/attribute/" + name).build().toUri()); | ||
|
||
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers) | ||
.body(new ErrorResponse(String.valueOf(HttpStatus.NOT_FOUND.value()), | ||
String.format("The custom attribute definition with name: [%s] does not already exist.", name))); | ||
} | ||
return ResponseEntity.ok(cad); | ||
} | ||
|
||
@DeleteMapping("/attribute/{name}") | ||
@Transactional | ||
public ResponseEntity<?> delete(@PathVariable String name) { | ||
CustomAttributeDefinition cad = caService.find(name); | ||
if (cad == null) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation( | ||
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/attribute/" + name).build().toUri()); | ||
|
||
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers) | ||
.body(new ErrorResponse(String.valueOf(HttpStatus.NOT_FOUND.value()), | ||
String.format("The custom attribute definition with name: [%s] does not already exist.", name))); | ||
} | ||
caService.deleteDefinition(cad); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
...ain/java/edu/internet2/tier/shibboleth/admin/ui/repository/CustomAttributeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/CustomAttributesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import java.util.List; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.CustomAttributeDefinition; | ||
|
||
public interface CustomAttributesService { | ||
|
||
CustomAttributeDefinition createOrUpdateDefinition(CustomAttributeDefinition definition); | ||
|
||
void deleteDefinition(CustomAttributeDefinition definition); | ||
|
||
CustomAttributeDefinition find(String name); | ||
|
||
List<CustomAttributeDefinition> getAllDefinitions(); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...main/java/edu/internet2/tier/shibboleth/admin/ui/service/CustomAttributesServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.CustomAttributeDefinition; | ||
import edu.internet2.tier.shibboleth.admin.ui.repository.CustomAttributeRepository; | ||
|
||
@Service | ||
public class CustomAttributesServiceImpl implements CustomAttributesService { | ||
@Autowired | ||
private CustomAttributeRepository repository; | ||
|
||
@Override | ||
public CustomAttributeDefinition createOrUpdateDefinition(CustomAttributeDefinition definition) { | ||
return repository.save(definition); | ||
} | ||
|
||
@Override | ||
public void deleteDefinition(CustomAttributeDefinition definition) { | ||
repository.delete(definition); | ||
} | ||
|
||
@Override | ||
public CustomAttributeDefinition find(String name) { | ||
return repository.findByName(name); | ||
} | ||
|
||
@Override | ||
public List<CustomAttributeDefinition> getAllDefinitions() { | ||
return repository.findAll(); | ||
} | ||
|
||
} |