use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class Jsr330Adapter method getBean.
public <T> T getBean(final Class<T> beanClass) {
BeanDependencies deps = _fieldDependencyBindings.get(beanClass);
if (deps == null) {
throw new RestLiInternalException("Could not find bean of class '" + beanClass.getName() + "'");
}
try {
Constructor<?> constructor = _constructorParameterDependencies.get(beanClass).getConstructor();
Object[] arguments = _constructorParameterBindings.get(beanClass);
@SuppressWarnings("unchecked") T bean = (T) constructor.newInstance(arguments);
for (Entry<Field, Object> fieldDep : deps.iterator()) {
Field f = fieldDep.getKey();
f.setAccessible(true);
f.set(bean, fieldDep.getValue());
}
return bean;
} catch (Throwable t) {
throw new RestLiInternalException(String.format("Error initializing bean %s", beanClass.getName()), t);
}
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class TestInjectResourceFactory method testHappyPath.
@Test
public void testHappyPath() {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource5.class);
// set up mock ApplicationContext
BeanProvider ctx = createMock(BeanProvider.class);
EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
EasyMock.expect(ctx.getBean(EasyMock.eq("dep3"))).andReturn(new SomeDependency1()).anyTimes();
Map<String, SomeDependency2> map = new HashMap<String, SomeDependency2>();
map.put("someBeanName", new SomeDependency2());
EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map).anyTimes();
EasyMock.replay(ctx);
InjectResourceFactory factory = new InjectResourceFactory(ctx);
factory.setRootResources(pathRootResourceMap);
// #1 happy path
SomeResource1 r1 = factory.create(SomeResource1.class);
assertNotNull(r1);
assertNotNull(r1.getDependency1());
assertNotNull(r1.getDependency2());
assertNull(r1.getNonInjectedDependency());
// #2 No deps
SomeResource2 r2 = factory.create(SomeResource2.class);
assertNotNull(r2);
// #3 bean not registered with ResourceFactory
try {
factory.create(SomeResource3.class);
fail("Expected no such bean exception");
} catch (RestLiInternalException e) {
// expected
}
EasyMock.verify(ctx);
EasyMock.reset(ctx);
// #4 derived resource
SomeResource5 r5 = factory.create(SomeResource5.class);
assertNotNull(r5);
assertNotNull(r5.getDependency1());
assertNotNull(r5.getDerivedDependency1());
assertNotNull(r5.getDependency2());
assertNotNull(r5.getDependency3());
assertNull(r5.getNonInjectedDependency());
}
use of com.linkedin.restli.internal.server.RestLiInternalException 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.internal.server.RestLiInternalException in project rest.li by linkedin.
the class RestLiClasspathScanner method checkForMatchingClass.
public void checkForMatchingClass(final String name) {
if (name.endsWith(CLASS_SUFFIX)) {
for (String packagePath : _packagePaths) {
if (name.contains(packagePath)) {
int start = name.lastIndexOf(packagePath);
int end = name.lastIndexOf(CLASS_SUFFIX);
String clazzPath = name.substring(start, end);
String clazzName = pathToName(clazzPath);
try {
Class<?> clazz = classForName(clazzName);
for (Annotation a : clazz.getAnnotations()) {
if (_annotations.contains(a.annotationType())) {
_matchedClasses.add(clazz);
break;
}
}
} catch (ClassNotFoundException e) {
throw new RestLiInternalException("Failed to load class while scanning packages", e);
}
}
}
}
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
final DataSchema schemaToEncode;
if (dataSchema instanceof TyperefDataSchema) {
return ((TyperefDataSchema) dataSchema).getFullName();
} else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
return dataSchema.getUnionMemberKey();
} else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
schemaToEncode = unionRef.getSchema();
} else {
schemaToEncode = dataSchema;
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schemaToEncode);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
Aggregations