com.formkiq.server.api.ErrorHandlingController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
package com.formkiq.server.api;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.formkiq.server.service.AuthenticationFailureException;
import com.formkiq.server.service.FormAccessDeniedException;
import com.formkiq.server.service.FormNotFoundException;
import com.formkiq.server.service.InvalidEmailException;
import com.formkiq.server.service.InvalidJSONException;
import com.formkiq.server.service.InvalidRequestBodyException;
import com.formkiq.server.service.PreconditionFailedException;
import com.formkiq.server.service.SaveActionException;
import javassist.bytecode.stackmap.TypeData.ClassName;
/**
* Error Handling Controller.
*
*/
@ControllerAdvice(annotations = RestController.class)
public class ErrorHandlingController {
/** Logger. */
private static final Logger LOG = Logger.getLogger(ClassName.class
.getName());
/**
* AuthenticationFailureException handler.
* @param ex AuthenticationFailureException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AuthenticationFailureException.class)
@ResponseBody
public static ApiMessageResponse handleAuthenticationFailureException(
final AuthenticationFailureException ex) {
return new ApiMessageResponse("authentication failed");
}
/**
* FormAccessDeniedException handler.
* @return ApiStringResponse
*/
@ExceptionHandler(FormAccessDeniedException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
public static ApiMessageResponse handleFormAccessDeniedException() {
return new ApiMessageResponse("access denied to form");
}
/**
* FormNotFoundException handler.
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(FormNotFoundException.class)
@ResponseBody
public static ApiMessageResponse handleFormNotFoundException() {
return new ApiMessageResponse("form not found");
}
/**
* InvalidEmailException handler.
* @param ex InvalidEmailException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(InvalidEmailException.class)
@ResponseBody
public static ApiMessageResponse handleInvalidEmailException(
final InvalidEmailException ex) {
return new ApiMessageResponse(
"invalid email address '"
+ ex.getInvalidEmail() + "'");
}
/**
* InvalidJSONException handler.
* @param ex InvalidJSONException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidJSONException.class)
@ResponseBody
public static ApiMessageResponse handleInvalidJSONException(
final InvalidJSONException ex) {
return new ApiMessageResponse("invalid json");
}
/**
* AuthenticationFailureException handler.
* @param ex {@link InvalidMediaTypeException}
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidMediaTypeException.class)
@ResponseBody
public static ApiMessageResponse handleInvalidMediaTypeException(
final InvalidMediaTypeException ex) {
return new ApiMessageResponse(ex.getMessage());
}
/**
* InvalidRequestBodyException handler.
* @param ex InvalidRequestBodyException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidRequestBodyException.class)
@ResponseBody
public static ApiMessageResponse handleInvalidRequestBodyException(
final InvalidRequestBodyException ex) {
return new ApiMessageResponse(ex.getMessage());
}
/** MailSender. */
@Autowired
private MailSender mailSender;
/**
* Handler for Access Denied Exception.
* @param ex AccessDeniedException
* @return ApiMessageResponse
*/
@ResponseBody
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AccessDeniedException.class)
public ApiMessageResponse handleAccessDenied(
final AccessDeniedException ex) {
return new ApiMessageResponse(ex.getMessage());
}
/**
* Exception handler.
* @param ex Exception
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
public ApiMessageResponse handleExceptions(final Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
SimpleMailMessage msg = new SimpleMailMessage();
msg.setSubject("Exception in FormKiQ");
msg.setText(sw.toString());
this.mailSender.send(msg);
} catch (Exception e) {
LOG.log(Level.SEVERE, e.getMessage(), e);
}
return new ApiMessageResponse(
"unable to process request");
}
/**
* Handle handleIllegalArgumentException.
* @param ex IllegalArgumentException
* @return ApiMessageResponse
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ApiMessageResponse handleIllegalArgumentException(
final IllegalArgumentException ex) {
String msg = ex.getMessage();
if (!StringUtils.isEmpty(msg) && msg.startsWith("No enum constant ")) {
msg = msg.substring(msg.lastIndexOf(".") + 1);
}
return new ApiMessageResponse(msg);
}
/**
* MissingServletRequestParameterException handler.
*
* @param ex MissingServletRequestParameterException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public ApiMessageResponse handleMissingServletRequestParameterException(
final MissingServletRequestParameterException ex) {
return new ApiMessageResponse("'" + ex.getParameterName()
+ "' parameter is required");
}
/**
* PreconditionFailedException handler.
*
* @param ex PreconditionFailedException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.PRECONDITION_FAILED)
@ExceptionHandler(PreconditionFailedException.class)
@ResponseBody
public ApiMessageResponse handlePreconditionFailedException(
final PreconditionFailedException ex) {
return new ApiMessageResponse(ex.getMessage());
}
/**
* SaveActionException handler.
*
* @param ex SaveActionException
* @return ApiStringResponse
*/
@ResponseStatus(HttpStatus.PRECONDITION_FAILED)
@ExceptionHandler(SaveActionException.class)
@ResponseBody
public ApiMessageResponse handleSaveActionException(
final SaveActionException ex) {
return new ApiMessageResponse(ex.getMessage());
}
}