[Trunk | Admin Tools Library]:

1. GenericPortalController.java: Added Generic portal controller for handling all portals without type limitation.
2. SimpleErrorController.java: Added /error request mapping, to format all error that are not explicitly handled.
3. AdminToolsLibraryExceptionsHandler.java: Added methods 
	"notFoundException()" (ChangeSetPersister.NotFoundException), 
	"forbiddenException()" (ForbiddenException), 
	"duplicateKeyException()" (DuplicateKeyException).
4. ForbiddenException.java: [NEW] Added class ForbiddenException extends RuntimeException.
5. ExceptionResponse.java: Added field HttpStatus status.
6. SingleValueWrapperResponse.java: [Moved from Admin Tools Service to Admin Tools Library] 
	Generic class SingleValueWrapperResponse created, with field "value" of type defined when instance is created (used for returning single value from API methods).
This commit is contained in:
Konstantina Galouni 2020-12-09 13:46:32 +00:00
parent 613af8fc70
commit e98cf88a13
6 changed files with 222 additions and 4 deletions

View File

@ -0,0 +1,90 @@
package eu.dnetlib.uoaadmintoolslibrary.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse;
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
// not used by portals
@RestController
@RequestMapping("/portal")
@CrossOrigin(origins = "*")
public class GenericPortalController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private PortalService portalService;
@RequestMapping(value = "/{pid}/type", method = RequestMethod.GET)
public String getPortalType(@PathVariable String pid) {
Portal portal = portalService.getPortal(pid);
if (portal == null) {
return null;
} else {
return portal.getType();
}
}
@RequestMapping(value = {""}, method = RequestMethod.GET)
public List<Portal> getAllPortals() {
List<Portal> portals = portalService.getAllPortals();
return portals;
}
@RequestMapping(value = {"/full"}, method = RequestMethod.GET)
public List<PortalResponse> getAllPortalsFull() {
return portalService.getAllPortalsFull();
}
// @PreAuthorize("isAuthenticated()")
// @RequestMapping(value = {"/my-portals"}, method = RequestMethod.GET)
// public List<Portal> getMyPortals(
// //@RequestHeader("X-XSRF-TOKEN") String token
//
// ) {
//// log.debug(token);
//// UserInfo userInfo = utils.getUserInfo(token);
//// log.debug(userInfo);
//// if(userInfo != null) {
//// List<String> roles = userInfo.getRoles();
//// for (String role : roles) {
//// log.debug(role);
//// }
//// }
//
// List<GrantedAuthority> authorities = null;
// Authentication authentication = (Authentication) SecurityContextHolder.getContext().getAuthentication();
// if(authentication != null) {
// authorities = (List<GrantedAuthority>) authentication.getAuthorities();
// }
// log.debug(authorities);
//
//// for(GrantedAuthority authority : authorities) {
//// authorizationService.
//// }
// List<Portal> portals = portalService.getAllPortals();
// for(Portal portal : portals) {
// if(authorizationService..manager(portal.getType(), portal.getPid())) {
//
// }
// }
//
// List<Portal> myPortals = new ArrayList<>();
// // Get roles and for every role, find portalType and add portals
// List<String> portalTypes = new ArrayList<>();
// portalTypes.add("connect");
// portalTypes.add("community");
// portalTypes.add("explore");
// for(String portalType : portalTypes) {
// myPortals.addAll(portalService.getAllPortalsByType(portalType));
// log.debug("Num of portals now: "+myPortals.size());
// }
//
// return myPortals;
// }
}

View File

@ -0,0 +1,60 @@
package eu.dnetlib.uoaadmintoolslibrary.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by argirok on 8/3/2018.
*/
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/error")
public class SimpleErrorController implements ErrorController {
private final ErrorAttributes errorAttributes;
@Autowired
public SimpleErrorController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
@Override
public String getErrorPath() {
return "/error";
}
@RequestMapping
public Map<String, Object> error(HttpServletRequest aRequest){
Map<String, Object> body = getErrorAttributes(aRequest,getTraceParameter(aRequest));
String trace = (String) body.get("trace");
if(trace != null){
String[] lines = trace.split("\n\t");
body.put("trace", lines);
}
return body;
}
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest);
return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
}
}

View File

@ -2,6 +2,8 @@ package eu.dnetlib.uoaadmintoolslibrary.handlers;
import eu.dnetlib.uoaadmintoolslibrary.responses.ExceptionResponse;
import org.apache.log4j.Logger;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
@ -62,4 +64,38 @@ public class AdminToolsLibraryExceptionsHandler {
log.debug("invalidReCaptchaException exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Not found Exception");
response.setErrorMessage("Not found Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.error("notFoundException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<ExceptionResponse> forbiddenException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Forbidden Exception");
response.setErrorMessage("Forbidden Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.FORBIDDEN);
log.error("forbiddenException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
}
@ExceptionHandler(DuplicateKeyException.class)
public ResponseEntity<ExceptionResponse> duplicateKeyException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("DuplicateKey Exception");
response.setErrorMessage("DuplicateKey Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
log.error("duplicateKeyException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -0,0 +1,11 @@
package eu.dnetlib.uoaadmintoolslibrary.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.FORBIDDEN)
public class ForbiddenException extends RuntimeException {
public ForbiddenException(String message){
super(message);
}
}

View File

@ -1,15 +1,18 @@
package eu.dnetlib.uoaadmintoolslibrary.responses;
import java.util.List;
import org.springframework.http.HttpStatus;
public class ExceptionResponse {
private HttpStatus status;
private String errorCode;
private String errorMessage;
private String errors;
public ExceptionResponse() {
}
public ExceptionResponse() {}
public HttpStatus getStatus() { return status; }
public void setStatus(HttpStatus status) { this.status = status; }
public String getErrorCode() {
return errorCode;

View File

@ -0,0 +1,18 @@
package eu.dnetlib.uoaadmintoolslibrary.responses;
public class SingleValueWrapperResponse<T> {
private T value = null;
public SingleValueWrapperResponse() { }
public SingleValueWrapperResponse(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}