use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestLiHTMLDocumentationRenderer method addRelated.
private void addRelated(Object parent, Map<String, Object> pageModel) {
final Node<?> node = _relationships.getRelationships(parent);
Map<String, ResourceSchema> relatedResources;
Map<String, NamedDataSchema> relatedSchemas;
synchronized (this) {
relatedResources = _relatedResourceCache.get(parent);
if (relatedResources == null) {
relatedResources = new HashMap<String, ResourceSchema>();
final Iterator<Node<ResourceSchema>> resourcesItr = node.getAdjacency(ResourceSchema.class).iterator();
while (resourcesItr.hasNext()) {
final ResourceSchema currResource = (ResourceSchema) resourcesItr.next().getObject();
relatedResources.put(currResource.getName(), currResource);
}
_relatedResourceCache.put(parent, relatedResources);
}
relatedSchemas = _relatedSchemaCache.get(parent);
if (relatedSchemas == null) {
relatedSchemas = new HashMap<String, NamedDataSchema>();
final Iterator<Node<NamedDataSchema>> schemaItr = node.getAdjacency(NamedDataSchema.class).iterator();
while (schemaItr.hasNext()) {
final NamedDataSchema currResource = (NamedDataSchema) schemaItr.next().getObject();
relatedSchemas.put(currResource.getFullName(), currResource);
}
_relatedSchemaCache.put(parent, relatedSchemas);
}
}
pageModel.put("relatedResources", relatedResources);
pageModel.put("relatedSchemas", relatedSchemas);
}
use of com.linkedin.restli.restspec.ResourceSchema 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);
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestLiJSONDocumentationRenderer method renderResource.
private void renderResource(ResourceSchema resourceSchema, DataMap outputMap) throws IOException {
final DataMap resources = outputMap.getDataMap("resources");
final DataMap models = outputMap.getDataMap("models");
resources.put(ResourceSchemaUtil.getFullName(resourceSchema), resourceSchema.data());
addRelatedModels(resourceSchema, models);
final List<ResourceSchema> subresources = _relationships.getResourceSchemaCollection().getAllSubResources(resourceSchema);
if (subresources != null) {
for (ResourceSchema subresource : subresources) {
resources.put(ResourceSchemaUtil.getFullName(subresource), subresource.data());
addRelatedModels(subresource, models);
}
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class TestGreetingsClient method testOptions.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderWithResourceNameDataProvider")
public void testOptions(RootBuilderWrapper<Long, Greeting> builders, String resourceName, ProtocolVersion protocolVersion) throws RemoteInvocationException, URISyntaxException, IOException {
Request<OptionsResponse> optionsRequest = builders.options().build();
OptionsResponse optionsResponse = getClient().sendRequest(optionsRequest).getResponse().getEntity();
Map<String, ResourceSchema> resources = optionsResponse.getResourceSchemas();
Assert.assertEquals(resources.size(), 1);
ResourceSchema resourceSchema = resources.get("com.linkedin.restli.examples.greetings.client." + resourceName);
// sanity check the resource schema
Assert.assertEquals(resourceSchema.getName(), resourceName);
Assert.assertTrue(resourceSchema.hasCollection());
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestLiJSONDocumentationRenderer method renderResource.
@Override
public void renderResource(String resourceName, OutputStream out) {
final ResourceSchema resourceSchema = _relationships.getResourceSchemaCollection().getResource(resourceName);
if (resourceSchema == null) {
throw new RoutingException(String.format("Resource named '%s' does not exist", resourceName), 404);
}
final DataMap outputMap = createEmptyOutput();
try {
renderResource(resourceSchema, outputMap);
_codec.writeMap(outputMap, out);
} catch (IOException e) {
throw new RestLiInternalException(e);
}
}
Aggregations