Search in sources :

Example 11 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method buildActionParam.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Parameter buildActionParam(final Method method, final AnnotationSet annotations, final Class<?> paramType) {
    ActionParam actionParam = annotations.get(ActionParam.class);
    Optional optional = annotations.get(Optional.class);
    String paramName = actionParam.value();
    Class<? extends TyperefInfo> typerefInfoClass = actionParam.typeref();
    try {
        Parameter param = new Parameter(paramName, paramType, getDataSchema(paramType, getSchemaFromTyperefInfo(typerefInfoClass)), optional != null, getDefaultValueData(optional), Parameter.ParamType.POST, true, annotations);
        return param;
    } catch (TemplateRuntimeException e) {
        throw new ResourceConfigException("DataSchema for parameter '" + paramName + "' of type " + paramType.getSimpleName() + " on " + buildMethodMessage(method) + "cannot be found; type is invalid or requires typeref", e);
    } catch (Exception e) {
        throw new ResourceConfigException("Typeref for parameter '" + paramName + "' on " + buildMethodMessage(method) + " cannot be instantiated, " + e.getMessage(), e);
    }
}
Also used : ActionParam(com.linkedin.restli.server.annotations.ActionParam) Optional(com.linkedin.restli.server.annotations.Optional) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 12 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method buildAssocKeyParam.

private static Parameter<?> buildAssocKeyParam(final ResourceModel model, final Method method, final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
    Parameter.ParamType parameter = null;
    String assocKeyParamValue = null;
    Class<? extends TyperefInfo> typerefInfoClass = null;
    if (paramAnnotationType.equals(AssocKey.class)) {
        parameter = Parameter.ParamType.KEY;
        assocKeyParamValue = annotations.get(AssocKey.class).value();
        typerefInfoClass = annotations.get(AssocKey.class).typeref();
    } else if (paramAnnotationType.equals(AssocKeyParam.class)) {
        parameter = Parameter.ParamType.ASSOC_KEY_PARAM;
        assocKeyParamValue = annotations.get(AssocKeyParam.class).value();
        typerefInfoClass = annotations.get(AssocKeyParam.class).typeref();
    } else {
        throw new ResourceConfigException("Param Annotation type must be 'AssocKeysParam' or the deprecated 'AssocKey' for AssocKey");
    }
    Optional optional = annotations.get(Optional.class);
    if (!checkAssocKey(model.getKeys(), assocKeyParamValue)) {
        throw new ResourceConfigException("Non-existing assocKey '" + assocKeyParamValue + "' on " + buildMethodMessage(method));
    }
    try {
        @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter(assocKeyParamValue, paramType, getDataSchema(paramType, getSchemaFromTyperefInfo(typerefInfoClass)), optional != null, getDefaultValueData(optional), parameter, true, annotations);
        return param;
    } catch (TemplateRuntimeException e) {
        throw new ResourceConfigException("DataSchema for assocKey '" + assocKeyParamValue + "' of type " + paramType.getSimpleName() + " on " + buildMethodMessage(method) + "cannot be found; type is invalid or requires typeref", e);
    } catch (Exception e) {
        throw new ResourceConfigException("Typeref for assocKey '" + assocKeyParamValue + "' on " + buildMethodMessage(method) + " cannot be instantiated, " + e.getMessage(), e);
    }
}
Also used : Optional(com.linkedin.restli.server.annotations.Optional) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) AssocKeyParam(com.linkedin.restli.server.annotations.AssocKeyParam) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 13 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException 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()) {
                final DataList valueAsDataList = _codec.stringToList(defaultValueString);
                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 {
                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 : DataList(com.linkedin.data.DataList) DataTemplate(com.linkedin.data.template.DataTemplate) RecordTemplate(com.linkedin.data.template.RecordTemplate) AbstractArrayTemplate(com.linkedin.data.template.AbstractArrayTemplate) IOException(java.io.IOException) TemplateOutputCastException(com.linkedin.data.template.TemplateOutputCastException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 14 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method getActionReturnClass.

private static Class<?> getActionReturnClass(ResourceModel model, Method method, Action actionAnno, String actionName) {
    final Type returnType = getLogicalReturnType(method);
    ResourceMethodDescriptor existingMethodDescriptor = model.findActionMethod(actionName, getActionResourceLevel(actionAnno, model));
    if (existingMethodDescriptor != null) {
        throw new ResourceConfigException("Found duplicate @Action method named '" + actionName + "' on class '" + model.getResourceClass().getName() + '\'');
    }
    Class<?> returnClass = getBoxedTypeFromPrimitive(getLogicalReturnClass(method));
    if (ActionResult.class.isAssignableFrom(returnClass)) {
        assert (returnType instanceof ParameterizedType);
        final ParameterizedType paramReturnType = (ParameterizedType) returnType;
        final Type[] actualReturnTypes = paramReturnType.getActualTypeArguments();
        assert (actualReturnTypes.length == 1);
        if (!(actualReturnTypes[0] instanceof Class<?>)) {
            throw new ResourceConfigException("Unsupported type parameter for ActionResult<?>.");
        }
        returnClass = (Class<?>) actualReturnTypes[0];
        if (returnClass == Void.class) {
            returnClass = Void.TYPE;
        }
    }
    return returnClass;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) InterfaceType(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType) Type(java.lang.reflect.Type) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 15 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method buildPagingContextParam.

private static Parameter<?> buildPagingContextParam(final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
    if (!paramType.equals(PagingContext.class)) {
        throw new ResourceConfigException("Incorrect data type for param: @" + PagingContextParam.class.getSimpleName() + " or @" + Context.class.getSimpleName() + " parameter annotation must be of type " + PagingContext.class.getName());
    }
    PagingContext defaultContext = null;
    Parameter.ParamType parameter = null;
    if (paramAnnotationType.equals(PagingContextParam.class)) {
        PagingContextParam pagingContextParam = annotations.get(PagingContextParam.class);
        defaultContext = new PagingContext(pagingContextParam.defaultStart(), pagingContextParam.defaultCount(), false, false);
        parameter = Parameter.ParamType.PAGING_CONTEXT_PARAM;
    } else if (paramAnnotationType.equals(Context.class)) {
        Context contextParam = annotations.get(Context.class);
        defaultContext = new PagingContext(contextParam.defaultStart(), contextParam.defaultCount(), false, false);
        parameter = Parameter.ParamType.CONTEXT;
    } else {
        throw new ResourceConfigException("Param Annotation type must be 'PagingContextParam' or the deprecated 'Context' for PagingContext");
    }
    Optional optional = annotations.get(Optional.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter("", paramType, null, optional != null, defaultContext, parameter, false, annotations);
    return param;
}
Also used : PagingContext(com.linkedin.restli.server.PagingContext) ResourceContext(com.linkedin.restli.server.ResourceContext) ParSeqContext(com.linkedin.restli.server.annotations.ParSeqContext) Context(com.linkedin.restli.server.annotations.Context) PagingContextParam(com.linkedin.restli.server.annotations.PagingContextParam) Optional(com.linkedin.restli.server.annotations.Optional) PagingContext(com.linkedin.restli.server.PagingContext) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)27 DataMap (com.linkedin.data.DataMap)7 Optional (com.linkedin.restli.server.annotations.Optional)7 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)6 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)3 RecordTemplate (com.linkedin.data.template.RecordTemplate)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3 PagingContext (com.linkedin.restli.server.PagingContext)3 AlternativeKey (com.linkedin.restli.server.annotations.AlternativeKey)3 AssocKey (com.linkedin.restli.server.annotations.AssocKey)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)2 InterfaceType (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType)2 CollectionResult (com.linkedin.restli.server.CollectionResult)2 Key (com.linkedin.restli.server.Key)2 PathKeys (com.linkedin.restli.server.PathKeys)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 ActionParam (com.linkedin.restli.server.annotations.ActionParam)2