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);
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestLiResourceRelationship method findDataModels.
private void findDataModels() {
final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {
@Override
public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
final String schema = resourceSchema.getSchema();
// ActionSet resources do not have a schema
if (schema != null) {
final NamedDataSchema schemaSchema = extractSchema(schema);
if (schemaSchema != null) {
connectSchemaToResource(visitContext, schemaSchema);
}
}
}
@Override
public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
final IdentifierSchema id = collectionSchema.getIdentifier();
final NamedDataSchema typeSchema = extractSchema(id.getType());
if (typeSchema != null) {
connectSchemaToResource(visitContext, typeSchema);
}
final String params = id.getParams();
if (params != null) {
final NamedDataSchema paramsSchema = extractSchema(params);
if (paramsSchema != null) {
connectSchemaToResource(visitContext, paramsSchema);
}
}
}
@Override
public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
for (AssocKeySchema key : associationSchema.getAssocKeys()) {
final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
if (keyTypeSchema != null) {
connectSchemaToResource(visitContext, keyTypeSchema);
}
}
}
@Override
public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
String parameterTypeString = parameterSchema.getType();
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(parameterTypeString)) {
visitInlineSchema(visitContext, parameterTypeString);
} else {
final NamedDataSchema schema;
// grab the schema name from it
if (parameterSchema.hasItems()) {
schema = extractSchema(parameterSchema.getItems());
} else // the only remaining possibility is that the type field contains the name of a data schema
{
schema = extractSchema(parameterTypeString);
}
if (schema != null) {
connectSchemaToResource(visitContext, schema);
}
}
}
@Override
public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
final MetadataSchema metadata = finderSchema.getMetadata();
if (metadata != null) {
final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
if (metadataTypeSchema != null) {
connectSchemaToResource(visitContext, metadataTypeSchema);
}
}
}
@Override
public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
final String returns = actionSchema.getReturns();
if (returns != null) {
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(returns)) {
visitInlineSchema(visitContext, returns);
} else // otherwise the type field contains the name of a data schema
{
final NamedDataSchema returnsSchema = extractSchema(returns);
if (returnsSchema != null) {
connectSchemaToResource(visitContext, returnsSchema);
}
}
}
final StringArray throwsArray = actionSchema.getThrows();
if (throwsArray != null) {
for (String errorName : throwsArray) {
final NamedDataSchema errorSchema = extractSchema(errorName);
if (errorSchema != null) {
connectSchemaToResource(visitContext, errorSchema);
}
}
}
}
private boolean isInlineSchema(String schemaString) {
return schemaString.startsWith("{");
}
private void visitInlineSchema(VisitContext visitContext, String schemaString) {
DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver);
if (schema instanceof ArrayDataSchema) {
DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
if (itemSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
}
}
if (schema instanceof MapDataSchema) {
DataSchema valueSchema = ((MapDataSchema) schema).getValues();
if (valueSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
}
}
}
private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
_dataModels.put(schema.getFullName(), schema);
final DataSchemaTraverse traveler = new DataSchemaTraverse();
traveler.traverse(schema, new DataSchemaTraverse.Callback() {
@Override
public void callback(List<String> path, DataSchema nestedSchema) {
if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
_dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
schemaNode.addAdjacentNode(node);
}
}
});
final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
resourceNode.addAdjacentNode(schemaNode);
schemaNode.addAdjacentNode(resourceNode);
}
};
ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Aggregations