use of com.linkedin.data.template.DataTemplate in project rest.li by linkedin.
the class TestUnionWithAliases method buildUnionData.
private <T> DataMap buildUnionData(String memberAlias, Object memberValue) {
String key = memberAlias;
Object value = null;
if (memberValue instanceof DataTemplate) {
DataTemplate<?> dataTemplate = (DataTemplate<?>) memberValue;
value = dataTemplate.data();
} else if (memberValue instanceof Enum) {
value = memberValue.toString();
} else {
value = memberValue;
}
DataMap dataMap = new DataMap();
dataMap.put(key, value);
return dataMap;
}
use of com.linkedin.data.template.DataTemplate in project rest.li by linkedin.
the class ArgumentBuilder method buildArrayArgument.
/**
* Build a method argument from a request parameter that is an array
*
* @param context {@link ResourceContext}
* @param param {@link Parameter}
* @return argument value in the correct type
*/
private static Object buildArrayArgument(final ResourceContext context, final Parameter<?> param, boolean validateParam) {
final Object convertedValue;
if (DataTemplate.class.isAssignableFrom(param.getItemType())) {
final DataList itemsList = (DataList) context.getStructuredParameter(param.getName());
convertedValue = Array.newInstance(param.getItemType(), itemsList.size());
int j = 0;
for (Object paramData : itemsList) {
final DataTemplate<?> itemsElem = DataTemplateUtil.wrap(paramData, param.getItemType().asSubclass(DataTemplate.class));
ArgumentUtils.validateDataAgainstSchema(itemsElem.data(), itemsElem.schema(), validateParam);
Array.set(convertedValue, j++, itemsElem);
}
} else {
final List<String> itemStringValues = context.getParameterValues(param.getName());
ArrayDataSchema parameterSchema;
if (param.getDataSchema() instanceof ArrayDataSchema) {
parameterSchema = (ArrayDataSchema) param.getDataSchema();
} else {
throw new RoutingException("An array schema is expected.", HttpStatus.S_400_BAD_REQUEST.getCode());
}
convertedValue = Array.newInstance(param.getItemType(), itemStringValues.size());
int j = 0;
for (String itemStringValue : itemStringValues) {
if (itemStringValue == null) {
throw new RoutingException("Parameter '" + param.getName() + "' cannot contain null values", HttpStatus.S_400_BAD_REQUEST.getCode());
}
try {
Array.set(convertedValue, j++, ArgumentUtils.convertSimpleValue(itemStringValue, parameterSchema.getItems(), param.getItemType(), false));
} catch (NumberFormatException e) {
Class<?> targetClass = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(parameterSchema.getItems().getDereferencedType());
// thrown from Integer.valueOf or Long.valueOf
throw new RoutingException(String.format("Array parameter '%s' value '%s' must be of type '%s'", param.getName(), itemStringValue, targetClass.getName()), HttpStatus.S_400_BAD_REQUEST.getCode());
} catch (IllegalArgumentException e) {
// thrown from Enum.valueOf
throw new RoutingException(String.format("Array parameter '%s' value '%s' is invalid", param.getName(), itemStringValue), HttpStatus.S_400_BAD_REQUEST.getCode());
} catch (TemplateRuntimeException e) {
// thrown from DataTemplateUtil.coerceOutput
throw new RoutingException(String.format("Array parameter '%s' value '%s' is invalid. Reason: %s", param.getName(), itemStringValue, e.getMessage()), HttpStatus.S_400_BAD_REQUEST.getCode());
}
}
}
return convertedValue;
}
use of com.linkedin.data.template.DataTemplate in project rest.li by linkedin.
the class Parameter method getDefaultValue.
public Object getDefaultValue() {
if (_defaultValueData == null) {
return null;
}
final Object result;
if (_defaultValueData instanceof String) {
final String defaultValueString = (String) _defaultValueData;
try {
if (getType().isArray()) {
DataList valueAsDataList = _codec.stringToList(defaultValueString);
DataSchema itemSchema = ((ArrayDataSchema) getDataSchema()).getItems();
// Handle custom type arrays. Only single level arrays are supported.
if (CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(itemSchema) != null) {
// First coerce the default value to the de-referenced type.
valueAsDataList = new DataList(valueAsDataList.stream().map(val -> DataTemplateUtil.coerceOutput(val, getDataClassFromSchema(itemSchema))).collect(Collectors.toList()));
}
result = DataTemplateUtil.convertDataListToArray(valueAsDataList, getItemType());
} else if (DataTemplate.class.isAssignableFrom(getType())) {
final Object input;
if (AbstractArrayTemplate.class.isAssignableFrom(getType())) {
input = _codec.stringToList(defaultValueString);
} else if (AbstractMapTemplate.class.isAssignableFrom(getType()) || UnionTemplate.class.isAssignableFrom(getType()) || RecordTemplate.class.isAssignableFrom(getType())) {
input = _codec.stringToMap(defaultValueString);
} else {
input = defaultValueString;
}
result = DataTemplateUtil.wrap(input, getType().asSubclass(DataTemplate.class));
validate((DataTemplate<?>) result, getType());
} else if (CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(getDataSchema()) != null) {
// First convert the default value from string to the de-referenced type.
Object deReferencedResult = ValueConverter.coerceString(defaultValueString, getDataClass());
// Use the coercer to get the custom type.
result = DataTemplateUtil.coerceOutput(deReferencedResult, getType());
} else {
result = ValueConverter.coerceString(defaultValueString, getType());
}
} catch (TemplateOutputCastException e) {
throw new ResourceConfigException(e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new ResourceConfigException("Default value for parameter of type \"" + getType().getName() + "\" is not supported: " + e.getMessage(), e);
} catch (IOException e) {
throw new ResourceConfigException("Default value for parameter of type \"" + getType().getName() + "\" is not supported: " + e.getMessage(), e);
}
} else {
result = _defaultValueData;
}
return result;
}
use of com.linkedin.data.template.DataTemplate in project rest.li by linkedin.
the class ArgumentBuilder method buildArrayArgument.
/**
* Build a method argument from a request parameter that is an array
*
* @param context {@link ResourceContext}
* @param param {@link Parameter}
* @return argument value in the correct type
*/
private static Object buildArrayArgument(final ResourceContext context, final Parameter<?> param) {
final Object convertedValue;
if (DataTemplate.class.isAssignableFrom(param.getItemType())) {
final DataList itemsList = (DataList) context.getStructuredParameter(param.getName());
convertedValue = Array.newInstance(param.getItemType(), itemsList.size());
int j = 0;
for (Object paramData : itemsList) {
final DataTemplate<?> itemsElem = DataTemplateUtil.wrap(paramData, param.getItemType().asSubclass(DataTemplate.class));
ValidateDataAgainstSchema.validate(itemsElem.data(), itemsElem.schema(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
Array.set(convertedValue, j++, itemsElem);
}
} else {
final List<String> itemStringValues = context.getParameterValues(param.getName());
ArrayDataSchema parameterSchema = null;
if (param.getDataSchema() instanceof ArrayDataSchema) {
parameterSchema = (ArrayDataSchema) param.getDataSchema();
} else {
throw new RoutingException("An array schema is expected.", HttpStatus.S_400_BAD_REQUEST.getCode());
}
convertedValue = Array.newInstance(param.getItemType(), itemStringValues.size());
int j = 0;
for (String itemStringValue : itemStringValues) {
if (itemStringValue == null) {
throw new RoutingException("Parameter '" + param.getName() + "' cannot contain null values", HttpStatus.S_400_BAD_REQUEST.getCode());
}
try {
Array.set(convertedValue, j++, ArgumentUtils.convertSimpleValue(itemStringValue, parameterSchema.getItems(), param.getItemType()));
} catch (NumberFormatException e) {
Class<?> targetClass = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(parameterSchema.getItems().getDereferencedType());
// thrown from Integer.valueOf or Long.valueOf
throw new RoutingException(String.format("Array parameter '%s' value '%s' must be of type '%s'", param.getName(), itemStringValue, targetClass.getName()), HttpStatus.S_400_BAD_REQUEST.getCode());
} catch (IllegalArgumentException e) {
// thrown from Enum.valueOf
throw new RoutingException(String.format("Array parameter '%s' value '%s' is invalid", param.getName(), itemStringValue), HttpStatus.S_400_BAD_REQUEST.getCode());
} catch (TemplateRuntimeException e) {
// thrown from DataTemplateUtil.coerceOutput
throw new RoutingException(String.format("Array parameter '%s' value '%s' is invalid. Reason: %s", param.getName(), itemStringValue, e.getMessage()), HttpStatus.S_400_BAD_REQUEST.getCode());
}
}
}
return convertedValue;
}
use of com.linkedin.data.template.DataTemplate in project rest.li by linkedin.
the class ClassNameDataSchemaResolver method locateDataSchema.
@Override
protected NamedDataSchema locateDataSchema(String className, StringBuilder errorMessageBuilder) {
final DataSchemaLocation location = new ClassNameDataSchemaLocation(className);
if (isBadLocation(location)) {
return null;
}
final Class<?> clazz;
try {
clazz = _classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" not found", className));
return null;
}
final DataSchema schema = DataTemplateUtil.getSchema(clazz);
if (schema instanceof NamedDataSchema) {
return (NamedDataSchema) schema;
}
if (DataTemplate.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") final Class<? extends DataTemplate<?>> clazzWithTyperef = (Class<? extends DataTemplate<?>>) clazz;
final TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(clazzWithTyperef);
if (typerefInfo != null) {
return typerefInfo.getSchema();
}
}
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" is not a NamedDataSchema", className));
return null;
}
Aggregations