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;
}
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;
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations