Search in sources :

Example 1 with TemplateOutputCastException

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);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) TemplateOutputCastException(com.linkedin.data.template.TemplateOutputCastException)

Example 2 with TemplateOutputCastException

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;
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) DataSchemaUtil.getDataClassFromSchema(com.linkedin.data.schema.DataSchemaUtil.getDataClassFromSchema) UnionTemplate(com.linkedin.data.template.UnionTemplate) AbstractMapTemplate(com.linkedin.data.template.AbstractMapTemplate) FieldDef(com.linkedin.data.template.FieldDef) DataTemplate(com.linkedin.data.template.DataTemplate) DataList(com.linkedin.data.DataList) DataSchema(com.linkedin.data.schema.DataSchema) IOException(java.io.IOException) CustomTypeUtil(com.linkedin.util.CustomTypeUtil) Collectors(java.util.stream.Collectors) ActionParam(com.linkedin.restli.server.annotations.ActionParam) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) AbstractArrayTemplate(com.linkedin.data.template.AbstractArrayTemplate) DataTemplateUtil(com.linkedin.data.template.DataTemplateUtil) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) TemplateOutputCastException(com.linkedin.data.template.TemplateOutputCastException) QueryParam(com.linkedin.restli.server.annotations.QueryParam) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValueConverter(com.linkedin.restli.internal.common.ValueConverter) DataTemplate(com.linkedin.data.template.DataTemplate) IOException(java.io.IOException) DataSchema(com.linkedin.data.schema.DataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AbstractArrayTemplate(com.linkedin.data.template.AbstractArrayTemplate) TemplateOutputCastException(com.linkedin.data.template.TemplateOutputCastException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

TemplateOutputCastException (com.linkedin.data.template.TemplateOutputCastException)2 ByteString (com.linkedin.data.ByteString)1 DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 DataSchemaUtil.getDataClassFromSchema (com.linkedin.data.schema.DataSchemaUtil.getDataClassFromSchema)1 ValidateDataAgainstSchema (com.linkedin.data.schema.validation.ValidateDataAgainstSchema)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 AbstractArrayTemplate (com.linkedin.data.template.AbstractArrayTemplate)1 AbstractMapTemplate (com.linkedin.data.template.AbstractMapTemplate)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 DataTemplateUtil (com.linkedin.data.template.DataTemplateUtil)1 FieldDef (com.linkedin.data.template.FieldDef)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 UnionTemplate (com.linkedin.data.template.UnionTemplate)1 ValueConverter (com.linkedin.restli.internal.common.ValueConverter)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1