All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ftl.rest.client.$$RestJsonModelReaderTemplateftl Maven / Gradle / Ivy

There is a newer version: 0.11
Show newest version
<#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 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}();
        
        <#-- ------------------------------------------------------------------------------------------------ -->
        <#list JAVA_MODEL_CLASS.internals as INTERNAL>
        <@setInternal INTERNAL=INTERNAL/>
        
        <#-- ------------------------------------------------------------------------------------------------ -->
        <#if JAVA_MODEL_CLASS.headerEntries?has_content>
        final HttpHeaders httpHeaders = response.headers();
        <#list JAVA_MODEL_CLASS.headerEntries as ENTRY>
        <@setHeaderFieldValue entry=ENTRY/>
        
        
        <#-- ------------------------------------------------------------------------------------------------ -->
        return model;
<#-- -------------------------------------------------------------------------------------------------------- -->
        <#else>
        return ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName}.fromJsonObject(response.body());
        
    }
    
    <#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());
    }
    
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}"));
        
    <#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}"));
        
    
<#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}"));
    
<#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}"));
    


<#-- -------------------------------------------------------------------------------------------------------- -->
<#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()" />
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#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});


<#-- -------------------------------------------------------------------------------------------------------- -->




© 2015 - 2024 Weber Informatics LLC | Privacy Policy