/**
  * Returned response code should be 409 if data already exists
  *
  * @param ex Caused exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.CONFLICT)
 @ResponseBody
 public ErrorElementType handleException(final AlreadyExistsException ex) {
   LOGGER.info("Advice logging(AlreadyExists): " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.CONFLICT.value()));
   element.setMessage("Data already exists.");
   return element;
 }
 /**
  * If marshall unmarshall fails then return bad request.
  *
  * @param ex Exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.BAD_REQUEST)
 @ResponseBody
 public ErrorElementType handleException(final ConversionException ex) {
   LOGGER.info("Bad request. " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.BAD_REQUEST.value()));
   element.setMessage("Bad request: " + ex.getMessage());
   return element;
 }
 /**
  * Returned response code should be 412 if data pre condition is not meet
  *
  * @param ex Caused exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
 @ResponseBody
 public ErrorElementType handleException(final MissingDataException ex) {
   LOGGER.info("Advice logging(Precondition failed): " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.CONFLICT.value()));
   element.setMessage("Precondition failed");
   return element;
 }
 /**
  * Returned response code should be 404 if data was not found.
  *
  * @param ex Caused exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.NOT_FOUND)
 @ResponseBody
 public ErrorElementType handleException(final NotFoundException ex) {
   LOGGER.info("Advice logging(Not found): " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.NOT_FOUND.value()));
   element.setMessage("No data was found.");
   return element;
 }
 /**
  * If marshall unmarshall fails then return bad request.
  *
  * @param ex Exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.UNAUTHORIZED)
 @ResponseBody
 public ErrorElementType handleException(final InvalidTokenException ex) {
   LOGGER.info("Invalid bearer token. " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.UNAUTHORIZED.value()));
   element.setMessage("User token is either missing or wrong. " + ex.getOAuth2ErrorCode());
   return element;
 }
 /**
  * If application fails or unknown error then return 500.
  *
  * @param ex Caused exception.
  * @return Internal server error.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
 @ResponseBody
 public ErrorElementType handleException(final RuntimeException ex) {
   LOGGER.error("Error occured during request.", ex);
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
   element.setMessage("Application error. Send mail to [email protected].");
   return element;
 }
 /**
  * If marshall unmarshall fails then return bad request.
  *
  * @param ex Caused exception.
  * @return Error message.
  */
 @ExceptionHandler
 @ResponseStatus(HttpStatus.FORBIDDEN)
 @ResponseBody
 public ErrorElementType handleException(final AccessDeniedException ex) {
   LOGGER.info("Access denied. " + ex.getMessage());
   ErrorElementType element = new ErrorElementType();
   element.setErrorcode(BigInteger.valueOf(HttpStatus.FORBIDDEN.value()));
   element.setMessage(
       "User does not have access to the resource. Contact [email protected] for more information.");
   return element;
 }