B Asap I Controller
B Asap I Controller
impl;
import static
com.telefonica.de.gps.api.v2.impl.converter.ErrorCodeConverter.convert;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.List;
import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import com.telefonica.de.gps.api.v2.model.ErrorResponse;
import com.telefonica.de.gps.api.v2.model.ErrorResponse.ErrorCodeEnum;
import com.telefonica.de.gps.config.FeatureConfig;
import com.telefonica.de.gps.connector.drc.model.DrcErrorCodeEnum;
import com.telefonica.de.gps.exception.ConflictResourceExcpetion;
import com.telefonica.de.gps.exception.DrcCommunicationException;
import com.telefonica.de.gps.exception.DrcCommunicationTimeoutException;
import com.telefonica.de.gps.exception.DrcNotAvailableException;
import com.telefonica.de.gps.exception.DrcPayoutCommunicationException;
import com.telefonica.de.gps.exception.DrcPayoutCommunicationTimeoutException;
import com.telefonica.de.gps.exception.DrcPayoutNotAvailableException;
import com.telefonica.de.gps.exception.DrcPayoutUnexpectedResponseException;
import com.telefonica.de.gps.exception.DrcUnexpectedResponseException;
import com.telefonica.de.gps.exception.ExternalSystemNotAvailableException;
import com.telefonica.de.gps.exception.ExternalSystemUnexpectedResponseException;
import com.telefonica.de.gps.exception.FunctionalException;
import com.telefonica.de.gps.exception.InvalidStatusUpdateException;
import com.telefonica.de.gps.exception.InvalidUpdateRequestException;
import com.telefonica.de.gps.exception.NoRecordAvailableException;
import com.telefonica.de.gps.exception.PspCommunicationTimeoutException;
import com.telefonica.de.gps.exception.PspNotAvailableException;
import com.telefonica.de.gps.exception.PspUnexpectedResponseException;
import com.telefonica.de.gps.exception.ValidationException;
@CrossOrigin
public class BaseApiController
{
/**
* Missing resources exception handling
*/
@ExceptionHandler(NoResultException.class)
public ResponseEntity<ErrorResponse> handleNoResultException(final Exception
exception,
final HttpServletRequest httpRequest)
{
log.error("> handle " + exception.getClass());
log.error("- Exception: ", exception);
log.error("< handle " + exception.getClass());
return ResponseEntity.notFound().build();
}
/**
* Validation exception handling
*/
@ExceptionHandler({ValidationException.class,
MethodArgumentTypeMismatchException.class,
HttpMessageNotReadableException.class })
public ResponseEntity<ErrorResponse> handleValidationException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_REQUEST,
new
ErrorResponse().errorCode(ErrorCodeEnum.VALIDATION_FAILED).errorMessage(exception.g
etMessage())
.externalErrorCode(formatNullDrcCode(((ValidationException)
exception).getExternalErrorCode()))
.externalErrorMessage(((ValidationException)
exception).getExternalErrorMessage())
.path(httpRequest.getServletPath()));
}
@ExceptionHandler(ConflictResourceExcpetion.class) public
ResponseEntity<ErrorResponse> handleConflictException(Exception ex,final
HttpServletRequest httpRequest) {
ErrorResponse().errorCode(ErrorCodeEnum.RESOURCE_ALREADY_EXISTS).errorMessage(ex.
getMessage()).path(httpRequest.getServletPath()).timestamp(OffsetDateTime.now()));
}
@ExceptionHandler({ MethodArgumentNotValidException.class })
public ResponseEntity<ErrorResponse>
handleMethodArgumentNotValidException(
final MethodArgumentNotValidException exception, final
HttpServletRequest httpRequest) {
if ("Pattern".equals(error.getCode())) {
String pattern = error.getArguments()[2].toString();
sb.append("has an invalid format:
'").append(error.getRejectedValue()).append("'. ")
.append("The pattern must be
'").append(pattern).append("'");
} else {
sb.append("Error Message ::" + error.getCodes()[0]);
}
sb.append(" , ");
}
@ExceptionHandler({ DateTimeParseException.class, })
public ResponseEntity<ErrorResponse> handleDateTimeParseException(final
Exception exception,
final HttpServletRequest httpRequest) {
String correctFormat = "yyyy-MM-dd'T'HH:mm:ssXXX";
String customErrorMessage = "The provided date-time is not in the
correct format. " +
"Please use the format: " + correctFormat;
@ExceptionHandler({ ConstraintViolationException.class,org.springframework.web.bin
d.MissingServletRequestParameterException.class })
public ResponseEntity<ErrorResponse> handleConstraintViolationException(final
Exception exception,
final HttpServletRequest httpRequest) {
return handleException(exception, HttpStatus.BAD_REQUEST,
new
ErrorResponse().errorCode(ErrorCodeEnum.VALIDATION_FAILED).errorMessage(exception.g
etMessage())
.errorMessage(exception.getMessage())
.path(httpRequest.getServletPath()));
}
@ExceptionHandler(NoRecordAvailableException.class)
public ResponseEntity<ErrorResponse> handleNoRecordAvailableException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.NOT_FOUND,
new
ErrorResponse().errorCode(ErrorCodeEnum.NO_RECORDS_AVBL).errorMessage(exception.get
Message())
.path(httpRequest.getServletPath()));
}
@ExceptionHandler(InvalidStatusUpdateException.class)
public ResponseEntity<ErrorResponse> handleInvalidStatusUpdateException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_REQUEST,
new
ErrorResponse().errorCode(ErrorCodeEnum.INVALID_STATUS_UPDATE).errorMessage(excepti
on.getMessage())
.externalErrorCode(formatNullDrcCode(((InvalidStatusUpdateE
xception) exception).getExternalErrorCode()))
.externalErrorMessage(((InvalidStatusUpdateException)
exception).getExternalErrorMessage())
.path(httpRequest.getServletPath()));
}
@ExceptionHandler(InvalidUpdateRequestException.class)
public ResponseEntity<ErrorResponse>
handleInvalidUpdateRequestException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_REQUEST,
new
ErrorResponse().errorCode(ErrorCodeEnum.INVALID_UPDATE_REQUEST).errorMessage(except
ion.getMessage())
.path(httpRequest.getServletPath()));
}
/**
* Functional exception handling
*/
@ExceptionHandler(FunctionalException.class)
public ResponseEntity<ErrorResponse> handleFunctionalException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.UNPROCESSABLE_ENTITY,
new
ErrorResponse().errorCode(convert(((FunctionalException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.externalErrorCode(formatNullDrcCode(((Function
alException) exception).getExternalErrorCode()))
.externalErrorMessage(((FunctionalException)
exception).getExternalErrorMessage())
.pspErrorCode(((FunctionalException)
exception).getPspCode())
.pspErrorMessage(((FunctionalException)
exception).getPspResponse()));
/**
* PSP unexpected exception handling
*/
@ExceptionHandler(PspUnexpectedResponseException.class)
public ResponseEntity<ErrorResponse>
handlePspUnexpectedResponseException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new
ErrorResponse().errorCode(convert(((PspUnexpectedResponseException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.pspErrorCode(((PspUnexpectedResponseException)
exception).getPspCode()));
}
/**
* External System unexpected exception handling
*/
@ExceptionHandler(ExternalSystemUnexpectedResponseException.class)
public ResponseEntity<ErrorResponse>
handleExternalSystemUnexpectedResponseException(final
ExternalSystemUnexpectedResponseException exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new ErrorResponse()
.errorCode(ErrorCodeEnum.PSP_ERROR)
.errorMessage(exception.getMessage())
.path(httpRequest.getServletPath())
.pspErrorMessage(exception.getPspResponse())
.pspErrorCode(exception.getPspCode())
);
}
/**
* PSP service not available error handling
*/
@ExceptionHandler({ PspNotAvailableException.class,
PspCommunicationTimeoutException.class })
public ResponseEntity<ErrorResponse> handlePspServiceException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.SERVICE_UNAVAILABLE,
new
ErrorResponse().errorCode(ErrorCodeEnum.PSP_ERROR).errorMessage(exception.getMessag
e())
.path(httpRequest.getServletPath()));
}
/**
* External System not available error handling
*/
@ExceptionHandler(ExternalSystemNotAvailableException.class)
public ResponseEntity<ErrorResponse> handlePspServiceException(final
ExternalSystemNotAvailableException exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.SERVICE_UNAVAILABLE,
new ErrorResponse()
.errorCode(ErrorCodeEnum.PSP_ERROR)
.errorMessage(exception.getMessage())
.path(httpRequest.getServletPath())
);
}
/**
* Unexpected exception handling
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleUnexpectedException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.INTERNAL_SERVER_ERROR,
new
ErrorResponse().errorCode(ErrorCodeEnum.GPS_INTERNAL_ERROR).errorMessage(exception.
getMessage())
.path(httpRequest.getServletPath()));
}
/**
* Drc not available handling
*/
@ExceptionHandler({DrcNotAvailableException.class,
DrcCommunicationTimeoutException.class})
public ResponseEntity<ErrorResponse> handleDrcNotAvailableException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.SERVICE_UNAVAILABLE,
new
ErrorResponse().errorCode(ErrorCodeEnum.DRC_INTERNAL_ERROR).errorMessage(exception.
getMessage())
.path(httpRequest.getServletPath()));
}
/**
* PSP unexpected exception handling
*/
@ExceptionHandler(DrcUnexpectedResponseException.class)
public ResponseEntity<ErrorResponse>
handleDrcUnexpectedResponseException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new
ErrorResponse().errorCode(convert(((DrcUnexpectedResponseException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.externalErrorCode(formatNullDrcCode(((DrcUnexp
ectedResponseException) exception).getExternalErrorCode()))
.externalErrorMessage(((DrcUnexpectedResponseEx
ception) exception).getExternalErrorMessage())
.path(httpRequest.getServletPath()));
}
/**
* PSP unexpected exception handling
*/
@ExceptionHandler(DrcCommunicationException.class)
public ResponseEntity<ErrorResponse> handleDrcCommunicationException(final
Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new
ErrorResponse().errorCode(convert(((DrcCommunicationException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.path(httpRequest.getServletPath()));
}
/**
* DRC PAYOUT unexpected exception handling
*/
@ExceptionHandler(DrcPayoutUnexpectedResponseException.class)
public ResponseEntity<ErrorResponse>
handleDrcPayoutUnexpectedResponseException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new
ErrorResponse().errorCode(convert(((DrcPayoutUnexpectedResponseException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.externalErrorCode(formatNullDrcCode(((DrcPayou
tUnexpectedResponseException) exception).getExternalErrorCode()))
.externalErrorMessage(((DrcPayoutUnexpectedResp
onseException) exception).getExternalErrorMessage())
.path(httpRequest.getServletPath()));
}
/**
* DRC Payout Communication exception handling
*/
@ExceptionHandler(DrcPayoutCommunicationException.class)
public ResponseEntity<ErrorResponse>
handleDrcPayoutCommunicationException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.BAD_GATEWAY,
new
ErrorResponse().errorCode(convert(((DrcPayoutCommunicationException)
exception).getErrorCode()))
.errorMessage(exception.getMessage()).path(http
Request.getServletPath())
.path(httpRequest.getServletPath()));
}
/**
* Drc Payout not available handling
*/
@ExceptionHandler({DrcPayoutNotAvailableException.class,
DrcPayoutCommunicationTimeoutException.class})
public ResponseEntity<ErrorResponse>
handleDrcPayoutNotAvailableException(final Exception exception,
final HttpServletRequest httpRequest)
{
return handleException(exception, HttpStatus.SERVICE_UNAVAILABLE,
new
ErrorResponse().errorCode(ErrorCodeEnum.DRC_INTERNAL_ERROR).errorMessage(exception.
getMessage())
.path(httpRequest.getServletPath()));
}