Search in sources :

Example 6 with ResourceConfigException

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

the class RestLiAnnotationReader method processResource.

/**
   * Processes an annotated resource class, producing a ResourceModel.
   *
   * @param resourceClass annotated resource class
   * @return {@link ResourceModel} for the provided resource class
   */
public static ResourceModel processResource(final Class<?> resourceClass, ResourceModel parentResourceModel) {
    final ResourceModel model;
    checkAnnotation(resourceClass);
    if ((resourceClass.isAnnotationPresent(RestLiCollection.class) || resourceClass.isAnnotationPresent(RestLiAssociation.class))) {
        // If any of these annotations, a subclass of KeyValueResource is expected
        if (!KeyValueResource.class.isAssignableFrom(resourceClass)) {
            throw new RestLiInternalException("Resource class '" + resourceClass.getName() + "' declares RestLi annotation but does not implement " + KeyValueResource.class.getName() + " interface.");
        }
        @SuppressWarnings("unchecked") Class<? extends KeyValueResource<?, ?>> clazz = (Class<? extends KeyValueResource<?, ?>>) resourceClass;
        model = processCollection(clazz, parentResourceModel);
    } else if (resourceClass.isAnnotationPresent(RestLiActions.class)) {
        model = processActions(resourceClass, parentResourceModel);
    } else if (resourceClass.isAnnotationPresent(RestLiSimpleResource.class)) {
        @SuppressWarnings("unchecked") Class<? extends SingleObjectResource<?>> clazz = (Class<? extends SingleObjectResource<?>>) resourceClass;
        model = processSingleObjectResource(clazz, parentResourceModel);
    } else {
        throw new ResourceConfigException("Class '" + resourceClass.getName() + "' must be annotated with a valid @RestLi... annotation");
    }
    if (parentResourceModel != null) {
        parentResourceModel.addSubResource(model.getName(), model);
    }
    if (!model.isActions()) {
        checkRestLiDataAnnotations(resourceClass, (RecordDataSchema) getDataSchema(model.getValueClass(), null));
    }
    addAlternativeKeys(model, resourceClass);
    DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(resourceClass.getAnnotations());
    addDeprecatedAnnotation(annotationsMap, resourceClass);
    model.setCustomAnnotation(annotationsMap);
    return model;
}
Also used : KeyValueResource(com.linkedin.restli.server.resources.KeyValueResource) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) RestLiActions(com.linkedin.restli.server.annotations.RestLiActions) SingleObjectResource(com.linkedin.restli.server.resources.SingleObjectResource) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) DataMap(com.linkedin.data.DataMap)

Example 7 with ResourceConfigException

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

the class RestLiAnnotationReader method checkParameterHasTyperefSchema.

private static boolean checkParameterHasTyperefSchema(Parameter<?> parameter) {
    boolean result = false;
    DataSchema dataSchema = parameter.getDataSchema();
    Class<?> dataType = parameter.getType();
    if (dataType.isArray()) {
        if (dataSchema instanceof ArrayDataSchema) {
            dataSchema = ((ArrayDataSchema) dataSchema).getItems();
        } else {
            throw new ResourceConfigException("Array typed parameter " + parameter.getName() + " must have an array schema.");
        }
    }
    if (dataSchema instanceof TyperefDataSchema) {
        result = true;
    }
    return result;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 8 with ResourceConfigException

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

the class RestLiAnnotationReader method addFinderResourceMethod.

private static void addFinderResourceMethod(final ResourceModel model, final Method method) {
    Finder finderAnno = method.getAnnotation(Finder.class);
    if (finderAnno == null) {
        return;
    }
    String queryType = finderAnno.value();
    List<Parameter<?>> queryParameters = getParameters(model, method, ResourceMethod.FINDER);
    if (queryType != null) {
        Class<? extends RecordTemplate> metadataType = null;
        final Class<?> returnClass = getLogicalReturnClass(method);
        if (CollectionResult.class.isAssignableFrom(returnClass)) {
            final List<Class<?>> typeArguments = ReflectionUtils.getTypeArguments(CollectionResult.class, returnClass.asSubclass(CollectionResult.class));
            final Class<?> metadataClass;
            if (typeArguments == null || typeArguments.get(1) == null) {
                // the return type may leave metadata type as parameterized and specify in runtime
                metadataClass = ((Class<?>) ((ParameterizedType) getLogicalReturnType(method)).getActualTypeArguments()[1]);
            } else {
                metadataClass = typeArguments.get(1);
            }
            if (!metadataClass.equals(NoMetadata.class)) {
                metadataType = metadataClass.asSubclass(RecordTemplate.class);
            }
        }
        DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
        addDeprecatedAnnotation(annotationsMap, method);
        ResourceMethodDescriptor finderMethodDescriptor = ResourceMethodDescriptor.createForFinder(method, queryParameters, queryType, metadataType, getInterfaceType(method), annotationsMap);
        validateFinderMethod(finderMethodDescriptor, model);
        if (!Modifier.isPublic(method.getModifiers())) {
            throw new ResourceConfigException(String.format("Resource '%s' contains non-public finder method '%s'.", model.getName(), method.getName()));
        }
        model.addResourceMethodDescriptor(finderMethodDescriptor);
    }
}
Also used : NoMetadata(com.linkedin.restli.server.NoMetadata) Finder(com.linkedin.restli.server.annotations.Finder) DataMap(com.linkedin.data.DataMap) ParameterizedType(java.lang.reflect.ParameterizedType) CollectionResult(com.linkedin.restli.server.CollectionResult) RecordTemplate(com.linkedin.data.template.RecordTemplate) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 9 with ResourceConfigException

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

the class RestLiAnnotationReader method registerCoercer.

private static void registerCoercer(final TyperefDataSchema schema) {
    String coercerClassName = CustomTypeUtil.getJavaCoercerClassFromSchema(schema);
    String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(schema);
    // initialize the custom class
    try {
        Custom.initializeCustomClass(Class.forName(javaClassNameFromSchema, true, Thread.currentThread().getContextClassLoader()));
    } catch (ClassNotFoundException e) {
        throw new ResourceConfigException("Could not find class for type " + javaClassNameFromSchema, e);
    }
    if (coercerClassName != null) {
        try {
            Custom.initializeCoercerClass(Class.forName(coercerClassName, true, Thread.currentThread().getContextClassLoader()));
        } catch (ClassNotFoundException e) {
            throw new ResourceConfigException("Could not find coercer " + coercerClassName + " for type " + javaClassNameFromSchema, e);
        }
    }
}
Also used : ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 10 with ResourceConfigException

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

the class RestLiClasspathScanner method scanPackages.

public void scanPackages() {
    try {
        for (String p : _packagePaths) {
            Enumeration<URL> resources = _classLoader.getResources(toUnixPath(p));
            while (resources.hasMoreElements()) {
                URI u = resources.nextElement().toURI();
                String scheme = u.getScheme().toLowerCase();
                if (scheme.equals(SCHEME_JAR) || scheme.equals(SCHEME_ZIP)) {
                    scanJar(u);
                } else if (scheme.equals(SCHEME_FILE)) {
                    scanDirectory(new File(u.getPath()));
                } else {
                    throw new ResourceConfigException("Unable to scan resource '" + u.toString() + "'. URI scheme not supported by scanner.");
                }
            }
        }
    } catch (IOException e) {
        throw new ResourceConfigException("Unable to scan resources", e);
    } catch (URISyntaxException e) {
        throw new ResourceConfigException("Unable to scan resources", e);
    }
}
Also used : IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) URL(java.net.URL)

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