uoa-orcid-service/src/main/java/eu/dnetlib/uoaorcidservice/handlers/ExceptionsHandler.java

108 lines
5.1 KiB
Java
Raw Normal View History

package eu.dnetlib.uoaorcidservice.handlers;
import com.google.gson.JsonSyntaxException;
import eu.dnetlib.uoaorcidservice.responses.ExceptionResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.nio.file.AccessDeniedException;
@ControllerAdvice
public class ExceptionsHandler {
private final Logger log = LogManager.getLogger(this.getClass());
// @ExceptionHandler(MissingServletRequestParameterException.class)
// public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
// ExceptionResponse response = new ExceptionResponse();
// response.setErrorCode("Validation Error");
// response.setErrorMessage("Invalid inputs.");
// response.setErrors(ex.getMessage());
// log.debug("invalidInput exception");
//
// return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
// }
//
// @ExceptionHandler(NullPointerException.class)
// public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
// ExceptionResponse response = new ExceptionResponse();
// response.setErrorCode("Null pointer Exception");
// response.setErrorMessage("Null pointer Exception");
// response.setErrors(ex.getMessage());
// log.debug("nullPointerException exception");
// return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
// }
// @ExceptionHandler(AccessDeniedException.class)
// public ResponseEntity<ExceptionResponse> accessDeniedException(Exception ex) {
// ExceptionResponse response = new ExceptionResponse();
// response.setErrorCode("Forbidden Exception");
// response.setErrorMessage("Access Denied Exception");
// response.setErrors(ex.getMessage());
// response.setStatus(HttpStatus.FORBIDDEN);
// log.error("accessDeniedException exception : "+ ex.getMessage());
// return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
// }
@ExceptionHandler(ContentNotFoundException.class)
public ResponseEntity<ExceptionResponse> contentNotFoundException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("No content found");
response.setErrorMessage(ex.getMessage());
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.error("contentNotFound exception: " + response.getErrorCode()+ " - "+response.getErrorMessage());
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(AuthorizationServiceException.class)
public ResponseEntity<ExceptionResponse> authorizationServiceException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("User not registered");
response.setErrorMessage("Authorization Service Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.UNAUTHORIZED);
log.error("authorizationServiceException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(JsonSyntaxException.class)
public ResponseEntity<ExceptionResponse> jsonSyntaxException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Json syntax is not valid");
response.setErrorMessage("Json Syntax Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
log.error("jsonSyntaxException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
// @ExceptionHandler(Exception.class)
// public ResponseEntity<ExceptionResponse> exception(Exception ex) {
// ExceptionResponse response = new ExceptionResponse();
[Trunk | Orcid Service]: 1. orcidservice.properties: Added properties "apiURL", "tokenURL", "clientId", "clientSecret" for ORCID API. 2. log4j.properties: Added configuration for ORCID log file "uoa-orcid-service-orcid.log". 3. UoaOrcidServiceApplication.java: Added "OrcidConfig.class" in "@EnableConfigurationProperties". 4. SimpleErrorController.java: If body.path.contains("/uoa-orcid-service/orcid"), log error in ORCID log file too. 5. UserTokensService.java: Added method "getEncryptedUserTokensByAai()". 6. UserTokensController.java: a. Added ORCID log file for responses by ORCID API. b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties. c. Added method "getUserOrcidId()" (/local/orcidId) d. Added method "getPersonalDetailsFromOrcid()" (/orcid/personal-details) e. Renamed "/orcid/tokens/decrypt" to "/local/tokens/decrypt" & "/orcid/tokens/encrypt" to "/local/tokens/encrypt". 7. Work.java: Added field "updateDate". 8. WorkDAO.java & MongoDBWorkDAO.java: Added methods "List<Work> findByOrcidOrderByCreationDateDesc(String Orcid);" and "Work findByPutCode(String putCode);". 9. WorkService.java: a. Added method "getLocalWorkByPutCode()". b. Method "getLocalWorks()" returns works ordered by most recent creation date. 10. WorkController.java: a. Added ORCID log file for responses by ORCID API. b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties. c. Added "charset=UTF-8" in all Content-Type request headers. d. Renamed "/orcid/put-code" to "/local/put-code". e. Added method "List<List<Work>> getLocalWorks(@RequestBody String[][] pids)" (/local/works). f. Added method "List<Work> getMyLocalWorks()" (/local/works). g. Added method "updateWork()" (/orcid/work/update/{putCode}). h. Added method "getWorksByPutCodes()" (/orcid/works).
2021-02-08 10:54:52 +01:00
// response.setErrorCode("Internal server error");
// response.setErrorMessage("Exception");
// response.setErrors(ex.getMessage());
// response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
// log.error("Exception exception : "+ ex.getMessage());
// return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
// }
}