Search in sources :

Example 11 with TemplateRuntimeException

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;
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) DataTemplate(com.linkedin.data.template.DataTemplate) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException)

Example 12 with TemplateRuntimeException

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);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException)

Aggregations

TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)12 RoutingException (com.linkedin.restli.server.RoutingException)6 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)5 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)3 Optional (com.linkedin.restli.server.annotations.Optional)3 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 DataSchema (com.linkedin.data.schema.DataSchema)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 CompoundKey (com.linkedin.restli.common.CompoundKey)2 PathSegmentSyntaxException (com.linkedin.restli.internal.common.PathSegment.PathSegmentSyntaxException)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1 DataSchemaAnnotationValidator (com.linkedin.data.schema.validator.DataSchemaAnnotationValidator)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1