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