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

ftl.rest.server.$$RestJsonModelWriterTemplateftl Maven / Gradle / Ivy

There is a newer version: 0.11
Show newest version
<#include "../../common-lib.javaftl">
<#-- -------------------------------------------------------------------------------------------------------- -->
public final class ${JAVA_MODEL_CLASS.modelWriterImplSimpleClassName} extends ModelWriter<${JAVA_MODEL_CLASS.javaSimpleClassName}> {

<#if JAVA_MODEL_CLASS.paramEntriesPresent>
    private final ${JAVA_MODEL_CLASS.modelToJsonConverterImplSimpleClassName} ${JAVA_MODEL_CLASS.toJsonConverterInstanceName};

    private final ExchangeDataFormatConverter exchangeDataFormatConverter;

    private final String outputMimeType;

    public ${JAVA_MODEL_CLASS.modelWriterImplSimpleClassName}(final boolean humanReadableOutput) {
        exchangeDataFormatConverter = new JsonExchangeDataFormatConverter(humanReadableOutput);
        ${JAVA_MODEL_CLASS.toJsonConverterInstanceName} = new ${JAVA_MODEL_CLASS.modelToJsonConverterImplSimpleClassName}();
        outputMimeType = exchangeDataFormatConverter.getMimeType();
    }

    @Override
    public void write(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model,
                      final HttpResponse response) {
        <#list JAVA_MODEL_CLASS.internals as INTERNAL>
        <@setInternal INTERNAL=INTERNAL/>
        
        <#list JAVA_MODEL_CLASS.headerEntries as ENTRY>
        <@setHeader header=ENTRY.key type=ENTRY.value/>
        
        final Map json = ${JAVA_MODEL_CLASS.toJsonConverterInstanceName}.toJsonObject(model);
        response.setHeader(HttpHeaders.CONTENT_TYPE, outputMimeType);
        response.setContent(exchangeDataFormatConverter.toBytes(json));
    }
<#-- -------------------------------------------------------------------------------------------------------- -->
<#else>
    public ${JAVA_MODEL_CLASS.modelWriterImplSimpleClassName}(final boolean humanReadableOutput) {
        //do nothing
    }

    @Override
    public void write(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model,
                      final HttpResponse response) {
        <#list JAVA_MODEL_CLASS.headerEntries as ENTRY>
        <@setHeader header=ENTRY.key type=ENTRY.value/>
        
        <#list JAVA_MODEL_CLASS.internals as INTERNAL>
        <@setInternal INTERNAL=INTERNAL/>
        
    }

}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternal INTERNAL>
    <#if INTERNAL.httpHeaders>
        <@setInternalValue item=INTERNAL method="setOrAddHeaders"/>
    <#elseif INTERNAL.httpVersion>
        <@setInternalValue item=INTERNAL method="setVersion"/>
    <#elseif INTERNAL.responseStatus>
        <@setInternalValue item=INTERNAL method="setStatus"/>
    <#elseif INTERNAL.responseBody>
        <@setInternalValue item=INTERNAL method="setContent"/>
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setHeader header type>
    <#if header.repeat>
      <#assign list>${getHeaderList(header, type)}
        for (final ${type.elementModelClass.javaSimpleClassName} item : ${list}) {
            response.addHeader("${header.modelName}", item);
        }
    <#else>
      <#if header.modelReadAccessorType.name() == "REFLECTION">
        response.setHeader("${header.modelName}", (${type.javaSimpleClassName}) getFieldValue(model, "${header.fieldName}"));
      <#elseif header.modelReadAccessorType.name() == "DIRECT">
        response.setHeader("${header.modelName}", model.${header.fieldName});
      <#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
        response.setHeader("${header.modelName}", model.${header.getter}());
      
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternalValue item method>
    <#if item.modelReadAccessorType.name() == "REFLECTION">
        response.${method}((${item.fieldSimpleType}) getFieldValue(model, "${item.fieldName}"));
    <#elseif item.modelReadAccessorType.name() == "DIRECT">
        response.${method}(model.${item.fieldName});
    <#elseif item.modelReadAccessorType.name() == "JAVA_BEAN">
        response.${method}(model.${item.getter}());
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#-- -------------------- https://freemarker.apache.org/docs/ref_directive_function.html -------------------- -->
<#-- -------------------------------------------------------------------------------------------------------- -->
<#function getHeaderList header type>
<#assign RESULT>
<#if header.modelReadAccessorType.name() == "REFLECTION">
(${type.javaSimpleClassName}) getFieldValue(model, "${header.fieldName}")
<#elseif header.modelReadAccessorType.name() == "DIRECT">
model.${header.fieldName}
<#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
model.${header.getter}()


<#return RESULT?trim>

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