use of com.linkedin.data.template.TemplateOutputCastException in project rest.li by linkedin.
the class IPAddressSimpleCoercer method coerceOutput.
@Override
public InetAddress coerceOutput(Object object) throws TemplateOutputCastException {
try {
byte[] addressBytes;
Class<?> objectType = object.getClass();
if (objectType == String.class) {
addressBytes = Data.stringToBytes((String) object, true);
} else if (objectType == ByteString.class) {
addressBytes = ((ByteString) object).copyBytes();
} else {
throw new TemplateOutputCastException("Invalid type");
}
return InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {
throw new TemplateOutputCastException("Invalid host", e);
}
}
use of com.linkedin.data.template.TemplateOutputCastException 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;
}
Aggregations