use of com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler in project ezyhttp by youngmonkeys.
the class ExceptionHandlerImplementer method doImplement.
@SuppressWarnings("rawtypes")
protected UncaughtExceptionHandler doImplement() throws Exception {
ClassPool pool = ClassPool.getDefault();
String implClassName = getImplClassName();
CtClass implClass = pool.makeClass(implClassName);
EzyClass superClass = new EzyClass(getSuperClass());
String exceptionHandlerFieldContent = makeExceptionHandlerFieldContent();
String setExceptionHandlerMethodContent = makeSetExceptionHandlerMethodContent();
String handleExceptionMethodContent = makeHandleExceptionMethodContent();
String getResponseContentTypeMethodContent = makeGetResponseContentTypeMethodContent();
printComponentContent(implClassName);
printComponentContent(exceptionHandlerFieldContent);
printComponentContent(setExceptionHandlerMethodContent);
printComponentContent(handleExceptionMethodContent);
printComponentContent(getResponseContentTypeMethodContent);
implClass.setSuperclass(pool.get(superClass.getName()));
implClass.addField(CtField.make(exceptionHandlerFieldContent, implClass));
implClass.addMethod(CtNewMethod.make(setExceptionHandlerMethodContent, implClass));
implClass.addMethod(CtNewMethod.make(handleExceptionMethodContent, implClass));
implClass.addMethod(CtNewMethod.make(getResponseContentTypeMethodContent, implClass));
Class answerClass = implClass.toClass();
implClass.detach();
AsmUncaughtExceptionHandler handler = (AsmUncaughtExceptionHandler) answerClass.newInstance();
setRepoComponent(handler);
return handler;
}
use of com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler in project ezyhttp by youngmonkeys.
the class ApplicationContextBuilder method addExceptionHandlers.
protected void addExceptionHandlers() {
List<Object> exceptionHandlerList = exceptionHandlerManager.getExceptionHandlerList();
ExceptionHandlersImplementer implementer = newExceptionHandlersImplementer();
Map<Class<?>, UncaughtExceptionHandler> exceptionHandlers = implementer.implement(exceptionHandlerList);
exceptionHandlerManager.addUncaughtExceptionHandlers(exceptionHandlers);
}
use of com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler in project ezyhttp by youngmonkeys.
the class ExceptionHandlerManagerTest method test.
@Test
public void test() {
// given
ExceptionHandlerManager sut = new ExceptionHandlerManager();
ExExceptionHandler exceptionHandler = new ExExceptionHandler();
sut.addExceptionHandler(exceptionHandler);
ExUncaughtExceptionHandler uncaughtExceptionHandler = new ExUncaughtExceptionHandler();
sut.addUncaughtExceptionHandler(Exception.class, uncaughtExceptionHandler);
// when
// then
Asserts.assertEquals(1, sut.getExceptionHandlerList().size());
Asserts.assertEquals(uncaughtExceptionHandler, sut.getUncaughtExceptionHandler(Exception.class));
}
use of com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler in project ezyhttp by youngmonkeys.
the class ExceptionHandlersImplementer method implement.
public Map<Class<?>, UncaughtExceptionHandler> implement(Object exceptionHandler) {
Map<Class<?>, UncaughtExceptionHandler> handlers = new HashMap<>();
ExceptionHandlerProxy proxy = new ExceptionHandlerProxy(exceptionHandler);
for (ExceptionHandlerMethod method : proxy.getExceptionHandlerMethods()) {
ExceptionHandlerImplementer implementer = newImplementer(proxy, method);
UncaughtExceptionHandler handler = implementer.implement();
for (Class<?> exceptionClass : method.getExceptionClasses()) {
handlers.put(exceptionClass, handler);
}
}
return handlers;
}
use of com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler in project ezyhttp by youngmonkeys.
the class BlockingServlet method handleException.
protected void handleException(HttpMethod method, RequestArguments arguments, Exception e) {
UncaughtExceptionHandler handler = getUncaughtExceptionHandler(e.getClass());
HttpServletRequest request = arguments.getRequest();
HttpServletResponse response = arguments.getResponse();
Exception exception = e;
if (handler != null) {
try {
Object result = handler.handleException(arguments, e);
if (result != null) {
String responseContentType = handler.getResponseContentType();
if (responseContentType != null) {
response.setContentType(responseContentType);
}
handleResponseData(request, response, result);
} else {
handleError(method, request, response, HttpServletResponse.SC_BAD_REQUEST);
}
exception = null;
} catch (Exception ex) {
exception = ex;
}
}
if (exception != null) {
handleError(method, request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception);
}
}
Aggregations