Skip to content

Commit

Permalink
SHIBUI-1734
Browse files Browse the repository at this point in the history
Added service and controller to expose API for CRUD operations around
custom attribute definitions
  • Loading branch information
chasegawa committed May 6, 2021
1 parent 1112662 commit d6625a1
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package edu.internet2.tier.shibboleth.admin.ui.repository;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import edu.internet2.tier.shibboleth.admin.ui.domain.CustomAttributeDefinition;

Expand Down
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();

}
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();
}

}

0 comments on commit d6625a1

Please sign in to comment.