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