private String getFormString(RestMethodInfo methodInfo) { StringBuilder str = new StringBuilder(); boolean first = true; JParameter[] parameters = methodInfo.method.getParameters(); try { for (int i = 0; i < methodInfo.parameterAnnotations.length; i++) { Annotation[] annotations = methodInfo.parameterAnnotations[i]; for (Annotation annotation : annotations) { if (annotation instanceof FormParam) { if (!first) { str.append("&"); } first = false; if (JClassUtils.isSimpleType(parameters[i].getType())) { buildFormStringForSimpleType(str, ((FormParam) annotation).value()); } else { buildFormStringForComplexType( str, parameters[i].getType(), ((FormParam) annotation).value()); } } } } } catch (UnsupportedEncodingException e) { throw new CruxGeneratorException( "Unsupported encoding for parameter name on method [" + methodInfo.method.toString() + "]"); } return str.toString(); }
private void generateMethodParamToHeaderCodeForComplexType( SourcePrinter srcWriter, String builder, String headerName, JType parameterType, String parameterExpression, String parameterCheckExpression) { PropertyInfo[] propertiesInfo = JClassUtils.extractBeanPropertiesInfo(parameterType.isClassOrInterface()); for (PropertyInfo propertyInfo : propertiesInfo) { if (JClassUtils.isSimpleType(propertyInfo.getType())) { generateMethodParamToHeaderCodeForSimpleType( srcWriter, builder, headerName + "." + propertyInfo.getName(), propertyInfo.getType(), parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()", (propertyInfo.getType().isPrimitive() != null ? parameterCheckExpression : parameterCheckExpression + " && " + parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()!=null")); } else { generateMethodParamToHeaderCodeForComplexType( srcWriter, builder, headerName + "." + propertyInfo.getName(), propertyInfo.getType(), parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()", parameterCheckExpression + " && " + parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()!=null"); } } }
private void buildFormStringForComplexType(StringBuilder str, JType parameterType, String value) throws UnsupportedEncodingException { PropertyInfo[] propertiesInfo = JClassUtils.extractBeanPropertiesInfo(parameterType.isClassOrInterface()); boolean first = true; for (PropertyInfo propertyInfo : propertiesInfo) { if (!first) { str.append("&"); } first = false; String parameterName = (StringUtils.isEmpty(value) ? propertyInfo.getName() : value + "." + propertyInfo.getName()); if (JClassUtils.isSimpleType(propertyInfo.getType())) { buildFormStringForSimpleType(str, parameterName); } else { buildFormStringForComplexType(str, propertyInfo.getType(), parameterName); } } }
private boolean generateMethodParamToBodyCodeForAnnotatedParameter( SourcePrinter srcWriter, String builder, JParameter[] parameters, boolean formEncoded, int i, Annotation annotation, JType parameterType, String parameterName) { if (annotation instanceof FormParam) { if (!formEncoded) { srcWriter.println( builder + ".setHeader(\"" + HttpHeaderNames.CONTENT_TYPE + "\", \"application/x-www-form-urlencoded\");"); formEncoded = true; } } if (JClassUtils.isSimpleType(parameterType)) { if (annotation instanceof FormParam) { generateMethodParamToCodeForSimpleType( srcWriter, "requestData", parameterType, ((FormParam) annotation).value(), parameterName, (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"), annotation); } if (annotation instanceof HeaderParam) { generateMethodParamToHeaderCodeForSimpleType( srcWriter, builder, ((HeaderParam) annotation).value(), parameterType, parameterName, (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null")); } if (annotation instanceof CookieParam) { generateMethodParamToCookieCodeForSimpleType( srcWriter, ((CookieParam) annotation).value(), parameterType, parameterName, (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null")); } } else { if (annotation instanceof FormParam) { generateMethodParamToCodeForComplexType( srcWriter, "requestData", parameterType, ((FormParam) annotation).value(), parameterName, parameterName + "!=null", annotation); } if (annotation instanceof HeaderParam) { generateMethodParamToHeaderCodeForComplexType( srcWriter, builder, ((HeaderParam) annotation).value(), parameterType, parameterName, parameterName + "!=null"); } if (annotation instanceof CookieParam) { generateMethodParamToCookieCodeForComplexType( srcWriter, ((CookieParam) annotation).value(), parameterType, parameterName, parameterName + "!=null"); } } return formEncoded; }