Search in sources :

Example 1 with ResponseStatus

use of core.framework.api.web.service.ResponseStatus in project core-ng-project by neowu.

the class WebServiceControllerBuilder method buildMethod.

private String buildMethod(boolean deprecated) {
    var builder = new CodeBuilder();
    builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
    if (deprecated)
        builder.indent(1).append("{}.logDeprecation({});\n", type(WebServiceController.class), variable(Methods.path(method)));
    Annotation[][] annotations = method.getParameterAnnotations();
    List<String> params = new ArrayList<>(annotations.length);
    Class<?>[] paramClasses = method.getParameterTypes();
    for (int i = 0; i < annotations.length; i++) {
        Class<?> paramClass = paramClasses[i];
        String paramClassLiteral = type(paramClass);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            params.add("$" + pathParam.value());
            if (String.class.equals(paramClass)) {
                builder.indent(1).append("String ${} = request.pathParam(\"{}\");\n", pathParam.value(), pathParam.value());
            } else if (Integer.class.equals(paramClass)) {
                builder.indent(1).append("Integer ${} = {}.toInt(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
            } else if (Long.class.equals(paramClass)) {
                builder.indent(1).append("Long ${} = {}.toLong(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
            } else if (paramClass.isEnum()) {
                builder.indent(1).append("{} ${} = ({}){}.toEnum(request.pathParam(\"{}\"), {});\n", paramClassLiteral, pathParam.value(), paramClassLiteral, type(PathParamHelper.class), pathParam.value(), variable(paramClass));
            } else {
                throw new Error("not supported path param type, type=" + paramClass.getCanonicalName());
            }
        } else {
            params.add("bean");
            builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramClassLiteral, paramClassLiteral, variable(paramClass));
        }
    }
    if (void.class == method.getReturnType()) {
        builder.indent(1).append("delegate.{}(", method.getName());
    } else {
        builder.indent(1).append("{} response = delegate.{}(", type(method.getReturnType()), method.getName());
    }
    builder.appendCommaSeparatedValues(params).append(");\n");
    if (void.class == method.getReturnType()) {
        builder.indent(1).append("return {}.empty()", type(Response.class));
    } else {
        builder.indent(1).append("return {}.bean(response)", type(Response.class));
    }
    ResponseStatus status = method.getDeclaredAnnotation(ResponseStatus.class);
    if (status == null) {
        builder.append(";\n");
    } else {
        builder.append(".status({});\n", variable(status.value()));
    }
    builder.append("}");
    return builder.build();
}
Also used : Request(core.framework.web.Request) ArrayList(java.util.ArrayList) CodeBuilder(core.framework.internal.asm.CodeBuilder) Response(core.framework.web.Response) ResponseStatus(core.framework.api.web.service.ResponseStatus) PathParam(core.framework.api.web.service.PathParam)

Aggregations

PathParam (core.framework.api.web.service.PathParam)1 ResponseStatus (core.framework.api.web.service.ResponseStatus)1 CodeBuilder (core.framework.internal.asm.CodeBuilder)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 ArrayList (java.util.ArrayList)1