Search in sources :

Example 31 with RequestHandler

use of com.tvd12.ezyhttp.server.core.handler.RequestHandler in project ezyhttp by youngmonkeys.

the class RequestHandlerImplementer method doImplement.

@SuppressWarnings("rawtypes")
protected RequestHandler doImplement() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String implClassName = getImplClassName();
    CtClass implClass = pool.makeClass(implClassName);
    EzyClass superClass = new EzyClass(getSuperClass());
    String controllerFieldContent = makeControllerFieldContent();
    String setControllerMethodContent = makeSetControllerMethodContent();
    String handleRequestMethodContent = makeHandleRequestMethodContent();
    String handleExceptionMethodContent = makeHandleExceptionMethodContent();
    String getHttpMethodMethodContent = makeGetHttpMethodMethodContent();
    String getRequestURIMethodContent = makeGetRequestURIMethodContent();
    String getResponseContentTypeMethodContent = makeGetResponseContentTypeMethodContent();
    String isAsyncMethodContent = makeIsAsyncMethodContent();
    printComponentContent(controllerFieldContent);
    printComponentContent(setControllerMethodContent);
    printComponentContent(handleRequestMethodContent);
    printComponentContent(handleExceptionMethodContent);
    printComponentContent(getHttpMethodMethodContent);
    printComponentContent(getRequestURIMethodContent);
    printComponentContent(getResponseContentTypeMethodContent);
    printComponentContent(isAsyncMethodContent);
    implClass.setSuperclass(pool.get(superClass.getName()));
    implClass.addField(CtField.make(controllerFieldContent, implClass));
    implClass.addMethod(CtNewMethod.make(setControllerMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(handleRequestMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(handleExceptionMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(getHttpMethodMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(getRequestURIMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(getResponseContentTypeMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(isAsyncMethodContent, implClass));
    Class answerClass = implClass.toClass();
    implClass.detach();
    RequestHandler handler = (RequestHandler) answerClass.newInstance();
    handler.setHandlerMethod(handlerMethod.getMethod().getMethod());
    setRepoComponent(handler);
    return handler;
}
Also used : CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) AbstractRequestHandler(com.tvd12.ezyhttp.server.core.handler.AbstractRequestHandler) ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass)

Example 32 with RequestHandler

use of com.tvd12.ezyhttp.server.core.handler.RequestHandler in project ezyhttp by youngmonkeys.

the class RequestHandlersImplementer method implement.

public Map<RequestURI, List<RequestHandler>> implement(Object controller) {
    Map<RequestURI, List<RequestHandler>> handlers = new HashMap<>();
    ControllerProxy proxy = new ControllerProxy(controller);
    String feature = proxy.getFeature();
    for (RequestHandlerMethod method : proxy.getRequestHandlerMethods()) {
        RequestHandlerImplementer implementer = newImplementer(proxy, method);
        RequestHandler handler = implementer.implement();
        HttpMethod httpMethod = handler.getMethod();
        String requestURI = handler.getRequestURI();
        String methodFeature = method.getFeature();
        RequestURIMeta uriMeta = RequestURIMeta.builder().api(method.isApi() || proxy.isApi()).authenticated(method.isAuthenticated() || proxy.isAuthenticated()).management(method.isManagement() || proxy.isManagement()).payment(method.isPayment() || proxy.isPayment()).feature(methodFeature != null ? methodFeature : feature).build();
        RequestURI uri = new RequestURI(httpMethod, requestURI, uriMeta);
        handlers.computeIfAbsent(uri, k -> new ArrayList<>()).add(handler);
    }
    return handlers;
}
Also used : Setter(lombok.Setter) Collection(java.util.Collection) EzyLoggable(com.tvd12.ezyfox.util.EzyLoggable) HashMap(java.util.HashMap) RequestURIDecorator(com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator) ArrayList(java.util.ArrayList) List(java.util.List) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) Map(java.util.Map) ControllerProxy(com.tvd12.ezyhttp.server.core.reflect.ControllerProxy) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) ControllerProxy(com.tvd12.ezyhttp.server.core.reflect.ControllerProxy) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ArrayList(java.util.ArrayList) List(java.util.List) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod)

Aggregations

RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)30 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)25 Test (org.testng.annotations.Test)25 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)24 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)24 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 ToString (lombok.ToString)24 ServletOutputStream (javax.servlet.ServletOutputStream)22 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)20 Cookie (javax.servlet.http.Cookie)20 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)10 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)7 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)4 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)4 ExceptionHandlerManager (com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager)4 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AsyncContext (javax.servlet.AsyncContext)3