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

ftl.rest.server.$$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 (HTTP_READER_TYPE.name() == "HTTP_BODY" || HTTP_READER_TYPE.name() == "QUERY_OR_HTTP_BODY") && PARAMS_PRESENT>
    private final ExchangeDataFormatConverter exchangeDataFormatConverter =
            new JsonExchangeDataFormatConverter(false);

    private final ${JAVA_MODEL_CLASS.modelFromJsonConverterImplSimpleClassName} ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName} =
            new ${JAVA_MODEL_CLASS.modelFromJsonConverterImplSimpleClassName}();

    
    @Override
    public ${JAVA_MODEL_CLASS.javaSimpleClassName       } read(final PathVariableMapping pathVariableMapping,
           ${space(JAVA_MODEL_CLASS.javaSimpleClassName)}      final HttpRequest request,
           ${space(JAVA_MODEL_CLASS.javaSimpleClassName)}      final boolean readParametersFromBody) {
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#if HTTP_READER_TYPE.name() == "INTERNAL_VARIABLE_ONLY" || HTTP_READER_TYPE.name() == "PATH_VARIABLES_ONLY">
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        <@setSimpleProperties shift=""/>
        return model;
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#elseif HTTP_READER_TYPE.name() == "QUERY_STRING">
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        <@setSimpleProperties shift=""/>
        <@setQueryParams shift=""/>
        return model;
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#elseif HTTP_READER_TYPE.name() == "HTTP_BODY">
        <@handleHttpBody shift=""/>
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#elseif HTTP_READER_TYPE.name() == "QUERY_OR_HTTP_BODY">
        <#if PARAMS_PRESENT>
        if (readParametersFromBody) {
            <@handleHttpBody shift="    "/>
        } else {
            final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
            <@setSimpleProperties shift="    "/>
            <@setQueryParams shift="    "/>
            return model;
        }
        <#else>
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        <@setSimpleProperties shift=""/>
        <@setQueryParams shift=""/>
        return model;
        
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#else>
        return new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        
    }
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro handleHttpBody shift>
    <#if PARAMS_PRESENT>
        ${shift}final Object body = exchangeDataFormatConverter.fromBytes(request.getContent());
        ${shift}final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = ${JAVA_MODEL_CLASS.fromJsonConverterInstanceName}.fromJsonObject(body);
    <#else>
        ${shift}final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
    
        <@setSimpleProperties shift=shift/>
        ${shift}return model;

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setSimpleProperties shift>
    <#if JAVA_MODEL_CLASS.internals?has_content>
    <#list JAVA_MODEL_CLASS.internals as INTERNAL>
        <@setInternal INTERNAL=INTERNAL shift=shift/>
    
    
    <#-- ---------------------------------------------------------------------------------------------------- -->
    <#if JAVA_MODEL_CLASS.pathVariableEntries?has_content>
        <#list JAVA_MODEL_CLASS.pathVariableEntries as ENTRY>
            <@setPathVariableFieldValue entry=ENTRY shift=shift/>
        
    
    <#-- ---------------------------------------------------------------------------------------------------- -->
    <#if JAVA_MODEL_CLASS.headerEntries?has_content>
        ${shift}final HttpHeaders httpHeaders = request.getHeaders();
        <#list JAVA_MODEL_CLASS.headerEntries as ENTRY>
            <@setHeaderFieldValue entry=ENTRY shift=shift/>
        
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setPathVariableFieldValue entry shift>
<#assign pathVariable = entry.key >
<#if entry.value.primitive>
    <#assign M = entry.value.primitiveType.convertMethod >
    <#if pathVariable.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${pathVariable.fieldName}", ${M}(pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}"));
    <#elseif pathVariable.modelReadAccessorType.name() == "DIRECT">
        ${shift}model.${pathVariable.fieldName} = ${M}(pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}");
    <#elseif pathVariable.modelReadAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${pathVariable.setter}(${M}(pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}"));
    
<#elseif entry.value.enum>
    <#if pathVariable.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${pathVariable.fieldName}", toEnum(${entry.value.javaSimpleClassName}.class, pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}"));
    <#elseif pathVariable.modelReadAccessorType.name() == "DIRECT">
        ${shift}model.${pathVariable.fieldName} = toEnum(${entry.value.javaSimpleClassName}.class, pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}");
    <#elseif pathVariable.modelReadAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${pathVariable.setter}(toEnum(${entry.value.javaSimpleClassName}.class, pathVariableMapping.getValue("${pathVariable.modelName}"), HttpModelType.${pathVariable.httpModelType.name()}, "${pathVariable.modelName}"));
    


<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setHeaderFieldValue entry shift>
<#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">
        ${shift}setFieldValue(model, "${header.fieldName}", ${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
        <#elseif header.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${header.fieldName} = ${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}");
        <#elseif header.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${header.setter}(${M}Array(httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
        
    <#elseif entry.value.enumList>
        <#if header.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${header.fieldName}", toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
        <#elseif header.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${header.fieldName} = toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}");
        <#elseif header.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${header.setter}(toEnumArray(${element.javaSimpleClassName}.class, httpHeaders.getValues("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
        
    
<#elseif entry.value.primitive>
    <#assign M = entry.value.primitiveType.convertMethod >
    <#if header.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${header.fieldName}", ${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
    <#elseif header.modelReadAccessorType.name() == "DIRECT">
        ${shift}model.${header.fieldName} = ${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}");
    <#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${header.setter}(${M}(httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
    
<#elseif entry.value.enum>
    <#if header.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${header.fieldName}", toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
    <#elseif header.modelReadAccessorType.name() == "DIRECT">
        ${shift}model.${header.fieldName} = toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}");
    <#elseif header.modelReadAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${header.setter}(toEnum(${entry.value.javaSimpleClassName}.class, httpHeaders.getValue("${header.modelName}"), HttpModelType.${header.httpModelType.name()}, "${header.modelName}"));
    


<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternal INTERNAL shift>
    <#if INTERNAL.remoteAddress>
        <#if INTERNAL.remoteAddressStringType>
            <@setInternalValue item=INTERNAL value="String.valueOf(request.getRemoteAddress())" shift=shift/>
        <#else>
            <@setInternalValue item=INTERNAL value="request.getRemoteAddress()" shift=shift/>
        
    <#elseif INTERNAL.requestUrl>
        <@setInternalValue item=INTERNAL value="request.getUri()" shift=shift/>
    <#elseif INTERNAL.requestMethod>
        <@setInternalValue item=INTERNAL value="request.getMethod()" shift=shift/>
    <#elseif INTERNAL.httpHeaders>
        <@setInternalValue item=INTERNAL value="request.getHeaders()" shift=shift/>
    <#elseif INTERNAL.httpVersion>
        <@setInternalValue item=INTERNAL value="request.getVersion()" shift=shift/>
    <#elseif INTERNAL.httpRequest>
        <@setInternalValue item=INTERNAL value="request" shift=shift/>
    <#elseif INTERNAL.requestBody>
        <@setInternalValue item=INTERNAL value="request.getContent()" shift=shift/>
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setInternalValue item value shift>
    <#if item.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${item.fieldName}", ${value});
    <#elseif item.modelReadAccessorType.name() == "DIRECT">
        ${shift}model.${item.fieldName} = ${value};
    <#elseif item.modelReadAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${item.setter}(${value});
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setQueryParams shift>
    <#if JAVA_MODEL_CLASS.paramEntries?has_content>
        ${shift}final QueryParams params = extractParams(request.getQueryString());
    <#list JAVA_MODEL_CLASS.paramEntries as fieldEntry>
        <#if fieldEntry.value.object>
        throwNotImplYet("Nested model lists does not supported yet. " +
                "Use HTTP method with body instead HTTP method, which provide query string " +
                "(For example POST instead of GET) or remove list parameter from request model.");
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#elseif fieldEntry.value.list>
        <#if fieldEntry.value.primitiveList>
        <@setParamFieldValue entry=fieldEntry shift=shift/>
        <#elseif fieldEntry.value.enumList>
        <@setParamFieldValue entry=fieldEntry shift=shift/>
        <#else>
        throwNotImplYet("Nested model lists does not supported yet. " +
                "Use HTTP method with body instead HTTP method, which provide query string " +
                "(For example POST instead of GET) or remove list parameter from request model.");
        
        <#-- -------------------------------------------------------------------------------------------------------- -->
        <#else>
        <@setParamFieldValue entry=fieldEntry shift=shift/>
        
    
    

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setParamFieldValue entry shift>
<#assign param = entry.key >
<#if entry.value.list>
    <#assign element = entry.value.elementModelClass>
    <#if entry.value.primitiveList>
        <#assign M = element.primitiveType.convertMethod >
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${param.fieldName}", ${M}Array(params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${param.fieldName} = ${M}Array(params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${param.setter}(${M}Array(params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
        
    <#elseif entry.value.enumList>
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${param.fieldName}", toEnumArray(${element.javaSimpleClassName}.class, params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${param.fieldName} = toEnumArray(${element.javaSimpleClassName}.class, params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${param.setter}(toEnumArray(${element.javaSimpleClassName}.class, params.getValues("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
        
    
<#elseif entry.value.primitive>
    <#assign M = entry.value.primitiveType.convertMethod >
    <#if param.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${param.fieldName}", ${M}(params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
    <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${param.fieldName} = ${M}(params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}");
    <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${param.setter}(${M}(params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
    
<#elseif entry.value.enum>
    <#if param.modelWriteAccessorType.name() == "REFLECTION">
        ${shift}setFieldValue(model, "${param.fieldName}", toEnum(${entry.value.javaSimpleClassName}.class, params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
    <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        ${shift}model.${param.fieldName} = toEnum(${entry.value.javaSimpleClassName}.class, params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}");
    <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        ${shift}model.${param.setter}(toEnum(${entry.value.javaSimpleClassName}.class, params.getValue("${param.modelName}"), HttpModelType.${param.httpModelType.name()}, "${param.modelName}"));
    


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