Search in sources :

Example 1 with PathParam

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

the class WebServiceClientBuilder method buildMethod.

private String buildMethod(Method method) {
    CodeBuilder builder = new CodeBuilder();
    Type returnType = method.getGenericReturnType();
    Map<String, Integer> pathParamIndexes = Maps.newHashMap();
    Type requestBeanType = null;
    Integer requestBeanIndex = null;
    builder.append("public {} {}(", type(returnType), method.getName());
    Annotation[][] annotations = method.getParameterAnnotations();
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        Class<?> paramClass = parameterTypes[i];
        if (i > 0)
            builder.append(", ");
        builder.append("{} param{}", type(paramClass), i);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            pathParamIndexes.put(pathParam.value(), i);
        } else {
            requestBeanIndex = i;
            requestBeanType = method.getGenericParameterTypes()[i];
        }
    }
    builder.append(") {\n");
    builder.indent(1).append("java.lang.reflect.Type requestType = {};\n", requestBeanType == null ? "null" : variable(requestBeanType));
    builder.indent(1).append("Object requestBean = {};\n", requestBeanIndex == null ? "null" : "param" + requestBeanIndex);
    builder.indent(1).append("java.util.Map pathParams = new java.util.HashMap();\n");
    pathParamIndexes.forEach((name, index) -> builder.indent(1).append("pathParams.put({}, param{});\n", variable(name), index));
    String returnTypeLiteral = returnType == void.class ? type(Void.class) : type(returnType);
    String path = method.getDeclaredAnnotation(Path.class).value();
    // to pass path as string literal, the escaped char will not be transferred, like \\, currently not convert is because only type regex may contain special char
    builder.indent(1).append("String serviceURL = client.serviceURL({}, pathParams);\n", variable(path));
    HTTPMethod httpMethod = HTTPMethods.httpMethod(method);
    builder.indent(1).append("{} response = ({}) client.execute({}, serviceURL, requestType, requestBean, {});\n", returnTypeLiteral, returnTypeLiteral, variable(httpMethod), variable(returnType));
    if (returnType != void.class)
        builder.indent(1).append("return response;\n");
    builder.append("}");
    return builder.build();
}
Also used : Path(core.framework.api.web.service.Path) CodeBuilder(core.framework.impl.asm.CodeBuilder) Type(java.lang.reflect.Type) HTTPMethod(core.framework.http.HTTPMethod) PathParam(core.framework.api.web.service.PathParam)

Example 2 with PathParam

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

the class WebServiceControllerBuilder method buildMethod.

private String buildMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
    List<String> params = Lists.newArrayList();
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    for (int i = 0; i < annotations.length; i++) {
        Type paramType = paramTypes[i];
        String paramTypeLiteral = type(paramType);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            params.add(pathParam.value());
            builder.indent(1).append("{} {} = ({}) request.pathParam(\"{}\", {});\n", paramTypeLiteral, pathParam.value(), paramTypeLiteral, pathParam.value(), variable(paramType));
        } else {
            params.add("bean");
            builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramTypeLiteral, paramTypeLiteral, variable(paramType));
        }
    }
    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.equals(method.getReturnType())) {
        builder.indent(1).append("return {}.empty().status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    } else {
        builder.indent(1).append("return {}.bean(response).status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    }
    builder.append("}");
    return builder.build();
}
Also used : Response(core.framework.web.Response) Type(java.lang.reflect.Type) Request(core.framework.web.Request) PathParam(core.framework.api.web.service.PathParam) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 3 with PathParam

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

the class WebServiceInterfaceValidator method validate.

private void validate(Method method) {
    validateHTTPMethod(method);
    HTTPMethod httpMethod = HTTPMethods.httpMethod(method);
    Path path = method.getDeclaredAnnotation(Path.class);
    if (path == null)
        throw Exceptions.error("method must have @Path, method={}", method);
    new PathPatternValidator(path.value()).validate();
    validateResponseBeanType(method.getGenericReturnType());
    Set<String> pathVariables = pathVariables(path.value());
    Type requestBeanType = null;
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    Set<String> pathParams = Sets.newHashSet();
    for (int i = 0; i < paramTypes.length; i++) {
        Type paramType = paramTypes[i];
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            validatePathParamType(paramType);
            pathParams.add(pathParam.value());
        } else {
            if (requestBeanType != null)
                throw Exceptions.error("service method must not have more than one bean param, previous={}, current={}", requestBeanType.getTypeName(), paramType.getTypeName());
            requestBeanType = paramType;
            if (httpMethod == HTTPMethod.GET || httpMethod == HTTPMethod.DELETE) {
                requestBeanMapper.registerQueryParamBean(requestBeanType);
            } else {
                requestBeanMapper.registerRequestBean(requestBeanType);
            }
        }
    }
    if (pathVariables.size() != pathParams.size() || !pathVariables.containsAll(pathParams))
        throw Exceptions.error("service method @PathParam params must match variable in path pattern, path={}, method={}", path.value(), method);
}
Also used : Path(core.framework.api.web.service.Path) Type(java.lang.reflect.Type) HTTPMethod(core.framework.http.HTTPMethod) PathPatternValidator(core.framework.impl.web.route.PathPatternValidator) PathParam(core.framework.api.web.service.PathParam)

Example 4 with PathParam

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

the class TypescriptDefinitionBuilder method parseParams.

private void parseParams(ServiceDefinition.ServiceMethodDefinition methodDefinition, Method method) {
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    for (int i = 0; i < paramTypes.length; i++) {
        String type = parseType(paramTypes[i]);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            methodDefinition.params.put(pathParam.value(), type);
        } else {
            methodDefinition.params.put("request", type);
        }
    }
}
Also used : Type(java.lang.reflect.Type) PathParam(core.framework.api.web.service.PathParam)

Aggregations

PathParam (core.framework.api.web.service.PathParam)4 Type (java.lang.reflect.Type)4 Path (core.framework.api.web.service.Path)2 HTTPMethod (core.framework.http.HTTPMethod)2 CodeBuilder (core.framework.impl.asm.CodeBuilder)2 PathPatternValidator (core.framework.impl.web.route.PathPatternValidator)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1