ftl.rest.client.$$RestJsonModelReaderTemplateftl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxmicro-annotation-processor-rest-client Show documentation
Show all versions of rxmicro-annotation-processor-rest-client Show documentation
The RxMicro Annotation Processor internal module that generates REST client components.
<#include "../../common-lib.javaftl">
<#-- -------------------------------------------------------------------------------------------------------- -->
public final class ${JAVA_MODEL_CLASS.modelReaderImplSimpleClassName} extends ModelReader<${JAVA_MODEL_CLASS.javaSimpleClassName}> {
<#if WITH_BODY>
private final ${JAVA_MODEL_CLASS.modelFromJsonConverterImplSimpleClassName} ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName} =
new ${JAVA_MODEL_CLASS.modelFromJsonConverterImplSimpleClassName}();
#if>
<#if READER_TYPE.name() == 'SINGLE' || READER_TYPE.name() == 'BOTH'>
@Override
public ${JAVA_MODEL_CLASS.javaSimpleClassName} readSingle(final ClientHttpResponse response) {
<#if JAVA_MODEL_CLASS.internals?has_content || JAVA_MODEL_CLASS.headerEntries?has_content>
<#if WITH_BODY>
final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName}.fromJsonObject(response.body());
<#else>
final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
#if>
<#-- ------------------------------------------------------------------------------------------------ -->
<#list JAVA_MODEL_CLASS.internals as INTERNAL>
<@setInternal INTERNAL=INTERNAL/>
#list>
<#-- ------------------------------------------------------------------------------------------------ -->
<#if JAVA_MODEL_CLASS.headerEntries?has_content>
final HttpHeaders httpHeaders = response.headers();
<#list JAVA_MODEL_CLASS.headerEntries as ENTRY>
<@setHeaderFieldValue entry=ENTRY/>
#list>
#if>
<#-- ------------------------------------------------------------------------------------------------ -->
return model;
<#-- -------------------------------------------------------------------------------------------------------- -->
<#else>
return ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName}.fromJsonObject(response.body());
#if>
}
#if>
<#if READER_TYPE.name() == 'LIST' || READER_TYPE.name() == 'BOTH'>
@Override
public List<${JAVA_MODEL_CLASS.javaSimpleClassName}> readList(final ClientHttpResponse response) {
return ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName}.fromJsonArray(response.body());
}
#if>
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setHeaderFieldValue entry>
<#assign header = entry.key >
<#if entry.value.list>
<#assign element = entry.value.elementModelClass>
<#if entry.value.primitiveList>
<#assign M = element.primitiveType.convertMethod >
<#if header.modelWriteAccessorType.name() == "REFLECTION">
setFieldValue(model, "${header.fieldName}", ${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
<#elseif header.modelWriteAccessorType.name() == "DIRECT">
model.${header.fieldName} = ${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}");
<#elseif header.modelWriteAccessorType.name() == "JAVA_BEAN">
model.${header.setter}(${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
#if>
<#elseif entry.value.enumList>
<#if header.modelWriteAccessorType.name() == "REFLECTION">
setFieldValue(model, "${header.fieldName}", toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
<#elseif header.modelWriteAccessorType.name() == "DIRECT">
model.${header.fieldName} = toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}");
<#elseif header.modelWriteAccessorType.name() == "JAVA_BEAN">
model.${header.setter}(toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
#if>
#if>
<#elseif entry.value.primitive>
<#assign M = entry.value.primitiveType.convertMethod >
<#if header.modelWriteAccessorType.name() == "REFLECTION">
setFieldValue(model, "${header.fieldName}", ${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
<#elseif header.modelReadAccessorType.name() == "DIRECT">
model.${header.fieldName} = ${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}");
<#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
model.${header.setter}(${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
#if>
<#elseif entry.value.enum>
<#if header.modelWriteAccessorType.name() == "REFLECTION">
setFieldValue(model, "${header.fieldName}", toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
<#elseif header.modelReadAccessorType.name() == "DIRECT">
model.${header.fieldName} = toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}");
<#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
model.${header.setter}(toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType}, "${header.modelName}"));
#if>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternal INTERNAL>
<#if INTERNAL.responseStatus>
<@setInternalValue item=INTERNAL value="response.statusCode()" />
<#elseif INTERNAL.httpVersion>
<@setInternalValue item=INTERNAL value="response.version()" />
<#elseif INTERNAL.httpHeaders>
<@setInternalValue item=INTERNAL value="response.headers()" />
<#elseif INTERNAL.responseBody>
<@setInternalValue item=INTERNAL value="response.bodyAsBytes()" />
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternalValue item value>
<#if item.modelWriteAccessorType.name() == "REFLECTION">
setFieldValue(model, "${item.fieldName}", ${value});
<#elseif item.modelReadAccessorType.name() == "DIRECT">
model.${item.fieldName} = ${value};
<#elseif item.modelReadAccessorType.name() == "JAVA_BEAN">
model.${item.setter}(${value});
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->