Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
ftl.rest.server.$$RestJsonModelReaderTemplateftl Maven / Gradle / Ivy
Go to download
The RxMicro Annotation Processor internal module that generates REST server components.
<#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}();
#if>
@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;
#if>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#else>
return new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
#if>
}
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}();
#if>
<@setSimpleProperties shift=shift/>
${shift}return model;
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setSimpleProperties shift>
<#if JAVA_MODEL_CLASS.internals?has_content>
<#list JAVA_MODEL_CLASS.internals as INTERNAL>
<@setInternal INTERNAL=INTERNAL shift=shift/>
#list>
#if>
<#-- ---------------------------------------------------------------------------------------------------- -->
<#if JAVA_MODEL_CLASS.pathVariableEntries?has_content>
<#list JAVA_MODEL_CLASS.pathVariableEntries as ENTRY>
<@setPathVariableFieldValue entry=ENTRY shift=shift/>
#list>
#if>
<#-- ---------------------------------------------------------------------------------------------------- -->
<#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/>
#list>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}"));
#if>
<#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}"));
#if>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}"));
#if>
<#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}"));
#if>
#if>
<#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}"));
#if>
<#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}"));
#if>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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/>
#if>
<#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/>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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});
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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.");
#if>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#else>
<@setParamFieldValue entry=fieldEntry shift=shift/>
#if>
#list>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}"));
#if>
<#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}"));
#if>
#if>
<#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}"));
#if>
<#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}"));
#if>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->