use of com.linkedin.data.template.TemplateRuntimeException 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.TemplateRuntimeException in project rest.li by linkedin.
the class RestLiRouter method parseSimpleKey.
private static void parseSimpleKey(final ResourceModel resource, final ServerResourceContext context, final String pathSegment) {
Object parsedKey;
try {
parsedKey = ArgumentUtils.parseSimplePathKey(pathSegment, resource, context.getRestliProtocolVersion());
} catch (NumberFormatException e) {
// thrown from Integer.valueOf or Long.valueOf
throw new RoutingException(String.format("Key value '%s' must be of type '%s'", pathSegment, resource.getKeyClass().getName()), HttpStatus.S_400_BAD_REQUEST.getCode(), e);
} catch (IllegalArgumentException e) {
// thrown from Enum.valueOf
throw new RoutingException(String.format("Key parameter value '%s' is invalid", pathSegment), HttpStatus.S_400_BAD_REQUEST.getCode(), e);
} catch (TemplateRuntimeException e) {
// thrown from DateTemplateUtil.coerceOutput
throw new RoutingException(String.format("Key parameter value '%s' is invalid", pathSegment), HttpStatus.S_400_BAD_REQUEST.getCode(), e);
}
context.getPathKeys().append(resource.getKeyName(), parsedKey);
}
Aggregations