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;
}
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;
}
Aggregations