Moved to Maven layout
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.raychatter.common.annotation;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class AnnotationHandler implements HandlerExceptionResolver {
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(final HttpServletRequest request,
|
||||
final HttpServletResponse response,
|
||||
final Object handler,
|
||||
final Exception thrownException) {
|
||||
|
||||
final ExceptionHandler exceptionHandlerAnnotation = thrownException.getClass().getAnnotation(ExceptionHandler.class);
|
||||
if (exceptionHandlerAnnotation == null) {
|
||||
return new ModelAndView();
|
||||
}
|
||||
|
||||
return doStuffWithAnnotation(exceptionHandlerAnnotation, thrownException, response);
|
||||
}
|
||||
|
||||
private ModelAndView doStuffWithAnnotation(final ExceptionHandler exceptionHandlerAnnotation, final Exception thrownException, final HttpServletResponse response) {
|
||||
|
||||
response.setContentType(exceptionHandlerAnnotation.contentType());
|
||||
response.setStatus(exceptionHandlerAnnotation.httpStatus().value());
|
||||
try {
|
||||
response.getWriter().write(formatMessage(thrownException));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new ModelAndView();
|
||||
}
|
||||
|
||||
// Customize your output message here. If you have multiple dynamic parameters to
|
||||
// put into your template, you can assign them all in this method.
|
||||
private String formatMessage(final Exception thrownException) {
|
||||
return String.format(readTemplate(), thrownException.getMessage());
|
||||
}
|
||||
|
||||
// Reads the template file until the end of the line
|
||||
private String readTemplate() {
|
||||
try {
|
||||
final InputStream templateFile = new ClassPathResource("error.template").getInputStream();
|
||||
return new Scanner(templateFile, "UTF-8").useDelimiter("\\A").next().trim();
|
||||
} catch (IOException exception) {
|
||||
return "Default Error Message: %s";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.raychatter.common.annotation;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({
|
||||
ElementType.ANNOTATION_TYPE,
|
||||
ElementType.CONSTRUCTOR,
|
||||
ElementType.FIELD,
|
||||
ElementType.LOCAL_VARIABLE,
|
||||
ElementType.METHOD,
|
||||
ElementType.PARAMETER,
|
||||
ElementType.TYPE})
|
||||
public @interface ExceptionHandler {
|
||||
HttpStatus httpStatus() default HttpStatus.OK;
|
||||
String contentType();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.raychatter.common.annotation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnnotationHandlerTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void shouldReturnModelAndViewIfAnnotationIsNull() {
|
||||
AnnotationHandler annotationHandler = new AnnotationHandler();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user