Search in sources :

Example 16 with ResourceConfigException

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

the class RestLiAnnotationReader method getActionTyperefDataSchema.

private static TyperefDataSchema getActionTyperefDataSchema(ResourceModel model, Action actionAnno, String actionName) {
    TyperefDataSchema returnTyperefSchema = null;
    Class<? extends TyperefInfo> typerefInfoClass = actionAnno.returnTyperef();
    try {
        returnTyperefSchema = getSchemaFromTyperefInfo(typerefInfoClass);
    } catch (Exception e) {
        throw new ResourceConfigException("Typeref @Action method named '" + actionName + "' on class '" + model.getResourceClass().getName() + "' cannot be instantiated, " + e.getMessage());
    }
    return returnTyperefSchema;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) 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 17 with ResourceConfigException

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

the class RestLiAnnotationReader method buildQueryParam.

private static Parameter<?> buildQueryParam(final Method method, final AnnotationSet annotations, final Class<?> paramType) {
    QueryParam queryParam = annotations.get(QueryParam.class);
    Optional optional = annotations.get(Optional.class);
    String paramName = queryParam.value();
    if (INVALID_CHAR_PATTERN.matcher(paramName).find()) {
        throw new ResourceConfigException("Unsupported character in the parameter name :" + paramName);
    }
    Class<? extends TyperefInfo> typerefInfoClass = queryParam.typeref();
    try {
        @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter(queryParam.value(), paramType, getDataSchema(paramType, getSchemaFromTyperefInfo(typerefInfoClass)), optional != null, getDefaultValueData(optional), Parameter.ParamType.QUERY, 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 : Optional(com.linkedin.restli.server.annotations.Optional) QueryParam(com.linkedin.restli.server.annotations.QueryParam) 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 18 with ResourceConfigException

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

the class RestLiAnnotationReader method validateFinderMethod.

private static void validateFinderMethod(final ResourceMethodDescriptor finderMethodDescriptor, final ResourceModel resourceModel) {
    Method method = finderMethodDescriptor.getMethod();
    Class<?> valueClass = resourceModel.getValueClass();
    Class<?> returnType, elementType;
    try {
        returnType = getLogicalReturnClass(method);
        final List<Class<?>> typeArguments;
        if (List.class.isAssignableFrom(returnType)) {
            typeArguments = ReflectionUtils.getTypeArguments(List.class, returnType.asSubclass(List.class));
        } else if (CollectionResult.class.isAssignableFrom(returnType)) {
            typeArguments = ReflectionUtils.getTypeArguments(CollectionResult.class, returnType.asSubclass(CollectionResult.class));
        } else {
            throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an unsupported return type");
        }
        if (typeArguments == null || typeArguments.get(0) == null) {
            // the return type may leave value type as parameterized and specify in runtime
            final ParameterizedType collectionType = (ParameterizedType) getLogicalReturnType(method);
            elementType = (Class<?>) collectionType.getActualTypeArguments()[0];
        } else {
            elementType = typeArguments.get(0);
        }
    } catch (ClassCastException e) {
        throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return or a data template type", e);
    }
    if (!List.class.isAssignableFrom(returnType) && !CollectionResult.class.isAssignableFrom(returnType)) {
        throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return type '" + returnType.getName() + "'. Expected " + "List<" + valueClass.getName() + "> or CollectionResult<" + valueClass.getName() + ">");
    }
    String collectionClassName = returnType.getSimpleName();
    if (!RecordTemplate.class.isAssignableFrom(elementType) || !resourceModel.getValueClass().equals(elementType)) {
        throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return type. Expected " + collectionClassName + "<" + valueClass.getName() + ">, but found " + collectionClassName + "<" + elementType + '>');
    }
    ResourceMethodDescriptor existingFinder = resourceModel.findNamedMethod(finderMethodDescriptor.getFinderName());
    if (existingFinder != null) {
        throw new ResourceConfigException("Found duplicate @Finder method named '" + finderMethodDescriptor.getFinderName() + "' on class '" + resourceModel.getResourceClass().getName() + '\'');
    }
// query parameters are checked in getQueryParameters method
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionResult(com.linkedin.restli.server.CollectionResult) ArrayList(java.util.ArrayList) List(java.util.List) RestMethod(com.linkedin.restli.server.annotations.RestMethod) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 19 with ResourceConfigException

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

the class RestLiAnnotationReader method buildAlternativeKey.

/**
   * Create an {@link com.linkedin.restli.server.AlternativeKey} object from an {@link com.linkedin.restli.server.annotations.AlternativeKey} annotation.
   *
   * @param resourceName Name of the resource.
   * @param altKeyAnnotation The {@link com.linkedin.restli.server.annotations.AlternativeKey} annotation.
   * @return {@link com.linkedin.restli.server.AlternativeKey} object.
   */
private static com.linkedin.restli.server.AlternativeKey<?, ?> buildAlternativeKey(String resourceName, AlternativeKey altKeyAnnotation) {
    String keyName = altKeyAnnotation.name();
    Class<?> keyType = altKeyAnnotation.keyType();
    Class<? extends TyperefInfo> altKeyTyperef = altKeyAnnotation.keyTyperefClass();
    KeyCoercer<?, ?> keyCoercer;
    try {
        keyCoercer = altKeyAnnotation.keyCoercer().newInstance();
    } catch (InstantiationException e) {
        throw new ResourceConfigException(String.format("KeyCoercer for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    } catch (IllegalAccessException e) {
        throw new ResourceConfigException(String.format("KeyCoercer for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    }
    try {
        @SuppressWarnings("unchecked") com.linkedin.restli.server.AlternativeKey<?, ?> altKey = new com.linkedin.restli.server.AlternativeKey(keyCoercer, keyType, getDataSchema(keyType, getSchemaFromTyperefInfo(altKeyTyperef)));
        return altKey;
    } catch (TemplateRuntimeException e) {
        throw new ResourceConfigException(String.format("DataSchema for alternative key '%s' of type %s on resource %s cannot be found; type is invalid or requires typeref.", keyName, keyType, resourceName), e);
    } catch (Exception e) {
        throw new ResourceConfigException(String.format("Typeref for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    }
}
Also used : TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) AlternativeKey(com.linkedin.restli.server.annotations.AlternativeKey)

Example 20 with ResourceConfigException

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

the class RestLiAnnotationReader method buildHeaderParam.

private static Parameter<?> buildHeaderParam(final AnnotationSet annotations, final Class<?> paramType) {
    if (!paramType.equals(String.class)) {
        throw new ResourceConfigException("Incorrect data type for param: @" + HeaderParam.class.getSimpleName() + " parameter annotation must be of type String");
    }
    Optional optional = annotations.get(Optional.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter("", paramType, null, optional != null, "", Parameter.ParamType.HEADER, false, annotations);
    return param;
}
Also used : HeaderParam(com.linkedin.restli.server.annotations.HeaderParam) Optional(com.linkedin.restli.server.annotations.Optional) 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