use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class ResourceModelAnnotation method getAnnotationData.
private static AnnotationEntry getAnnotationData(Annotation a, boolean isTopLevel) {
final DataMap data = new DataMap();
final Class<? extends Annotation> annotationClass = a.annotationType();
AnnotationTrait trait = _traits.get(annotationClass);
if (trait == null) {
trait = getTraits(annotationClass, isTopLevel);
_traits.put(annotationClass, trait);
}
for (Method m : annotationClass.getDeclaredMethods()) {
final MetaTrait methodTrait = trait.memberTraits.get(m);
if (methodTrait == null || (isTopLevel && !methodTrait.isRestSpecAnnotated)) {
continue;
}
try {
final Object memberValue = m.invoke(a);
if (methodTrait.skipDefault) {
final Object annotationDefault = m.getDefaultValue();
if (annotationDefault != null && annotationDefault.equals(memberValue)) {
continue;
}
}
final Object valueData = annotationMemberToData(memberValue);
if (valueData != null) {
data.put(methodTrait.isRestSpecAnnotated ? methodTrait.name : m.getName(), valueData);
}
} catch (IllegalAccessException e) {
throw new RestLiInternalException(e);
} catch (IllegalArgumentException e) {
throw new RestLiInternalException(e);
} catch (InvocationTargetException e) {
throw new RestLiInternalException(e);
}
}
if (data.isEmpty()) {
if (trait.masterTrait.isRestSpecAnnotated) {
return new AnnotationEntry(trait.masterTrait.name, new DataMap());
} else {
return null;
}
} else {
return new AnnotationEntry(trait.masterTrait.name, data);
}
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
/*package*/
static String buildDataSchemaType(DataSchema schema) {
if (schema instanceof PrimitiveDataSchema || schema instanceof NamedDataSchema) {
return schema.getUnionMemberKey();
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schema);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + schema.toString() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class TestInjectResourceFactory method testMissingNamedDependency.
@Test
public void testMissingNamedDependency() {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class);
BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(null).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);
try {
factory.setRootResources(pathRootResourceMap);
fail("Expected unresolvable bean exception");
} catch (RestLiInternalException e) {
assertTrue(e.getMessage().startsWith("Expected to find"));
}
EasyMock.verify(ctx);
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class TestInjectResourceFactory method testAmbiguousBeanResolution.
@Test
public void testAmbiguousBeanResolution() throws Exception {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource4.class);
// set up mock ApplicationContext
BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
Map<String, SomeDependency2> map2 = new HashMap<String, SomeDependency2>();
map2.put("someBeanName", new SomeDependency2());
EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map2).anyTimes();
Map<String, SomeDependency1> map1 = new HashMap<String, SomeDependency1>();
map1.put("someDep1", new SomeDependency1());
map1.put("anotherDep1", new SomeDependency1());
EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency1.class))).andReturn(map1).anyTimes();
EasyMock.replay(ctx);
InjectResourceFactory factory = new InjectResourceFactory(ctx);
// #4 ambiguous dep
try {
factory.setRootResources(pathRootResourceMap);
fail("Expected unresolvable bean exception");
} catch (RestLiInternalException e) {
assertTrue(e.getMessage().startsWith("Expected to find"));
}
EasyMock.verify(ctx);
EasyMock.reset(ctx);
}
use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.
the class RestLiHTMLDocumentationRenderer method renderResource.
@Override
public void renderResource(String resourceName, OutputStream out) {
final ResourceSchema resourceSchema = _resourceSchemas.getResource(resourceName);
final List<ResourceSchema> parentResources = _resourceSchemas.getParentResources(resourceSchema);
ExampleRequestResponseGenerator generator = new ExampleRequestResponseGenerator(parentResources, resourceSchema, _schemaResolver);
if (resourceSchema == null) {
throw new RoutingException(String.format("Resource \"%s\" does not exist", resourceName), HttpStatus.S_404_NOT_FOUND.getCode());
}
final Map<String, Object> pageModel = createPageModel();
pageModel.put("resource", resourceSchema);
pageModel.put("resourceName", resourceName);
pageModel.put("resourceFullName", ResourceSchemaUtil.getFullName(resourceSchema));
pageModel.put("resourceType", getResourceType(resourceSchema));
pageModel.put("subResources", _resourceSchemas.getSubResources(resourceSchema));
final List<ResourceMethodDocView> restMethods = new ArrayList<ResourceMethodDocView>();
final List<ResourceMethodDocView> finders = new ArrayList<ResourceMethodDocView>();
final List<ResourceMethodDocView> actions = new ArrayList<ResourceMethodDocView>();
final MethodGatheringResourceSchemaVisitor visitor = new MethodGatheringResourceSchemaVisitor(resourceName);
ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
for (RecordTemplate methodSchema : visitor.getAllMethods()) {
final ExampleRequestResponse capture;
if (methodSchema instanceof RestMethodSchema) {
RestMethodSchema restMethodSchema = (RestMethodSchema) methodSchema;
capture = generator.method(ResourceMethod.valueOf(restMethodSchema.getMethod().toUpperCase()));
} else if (methodSchema instanceof FinderSchema) {
FinderSchema finderMethodSchema = (FinderSchema) methodSchema;
capture = generator.finder(finderMethodSchema.getName());
} else if (methodSchema instanceof ActionSchema) {
ActionSchema actionMethodSchema = (ActionSchema) methodSchema;
final ResourceLevel resourceLevel = (visitor.getCollectionActions().contains(methodSchema) ? ResourceLevel.COLLECTION : ResourceLevel.ENTITY);
capture = generator.action(actionMethodSchema.getName(), resourceLevel);
} else {
capture = null;
}
String requestEntity = null;
String responseEntity = null;
if (capture != null) {
try {
DataMap entityMap;
if (capture.getRequest().getEntity().length() > 0) {
entityMap = DataMapUtils.readMap(capture.getRequest());
requestEntity = new String(_codec.mapToBytes(entityMap));
}
if (capture.getResponse() != null && capture.getResponse().getEntity() != null && capture.getResponse().getEntity().length() > 0) {
entityMap = DataMapUtils.readMap(capture.getResponse());
responseEntity = new String(_codec.mapToBytes(entityMap));
}
} catch (IOException e) {
throw new RestLiInternalException(e);
}
}
final ResourceMethodDocView docView = new ResourceMethodDocView(methodSchema, capture, getDoc(methodSchema, resourceSchema.hasSimple()), requestEntity, responseEntity);
if (methodSchema instanceof RestMethodSchema) {
restMethods.add(docView);
} else if (methodSchema instanceof FinderSchema) {
finders.add(docView);
} else if (methodSchema instanceof ActionSchema) {
actions.add(docView);
}
}
pageModel.put("restMethods", restMethods);
pageModel.put("finders", finders);
pageModel.put("actions", actions);
addRelated(resourceSchema, pageModel);
_templatingEngine.render("resource.vm", pageModel, out);
}
Aggregations