Search in sources :

Example 1 with UncaughtExceptionHandler

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;
}
Also used : CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass) ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass)

Example 2 with UncaughtExceptionHandler

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);
}
Also used : ExceptionHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.ExceptionHandlersImplementer) UncaughtExceptionHandler(com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler)

Example 3 with UncaughtExceptionHandler

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));
}
Also used : ExceptionHandlerManager(com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager) Test(org.testng.annotations.Test)

Example 4 with UncaughtExceptionHandler

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;
}
Also used : HashMap(java.util.HashMap) ExceptionHandlerProxy(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerProxy) UncaughtExceptionHandler(com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler) ExceptionHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod)

Example 5 with UncaughtExceptionHandler

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncaughtExceptionHandler(com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler) ServletException(javax.servlet.ServletException) DeserializeValueException(com.tvd12.ezyhttp.core.exception.DeserializeValueException) IOException(java.io.IOException) HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException)

Aggregations

UncaughtExceptionHandler (com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler)3 EzyClass (com.tvd12.ezyfox.reflect.EzyClass)1 DeserializeValueException (com.tvd12.ezyhttp.core.exception.DeserializeValueException)1 HttpRequestException (com.tvd12.ezyhttp.core.exception.HttpRequestException)1 ExceptionHandlersImplementer (com.tvd12.ezyhttp.server.core.asm.ExceptionHandlersImplementer)1 ExceptionHandlerManager (com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager)1 ExceptionHandlerMethod (com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod)1 ExceptionHandlerProxy (com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerProxy)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.testng.annotations.Test)1