Search in sources :

Example 26 with ResourceSchema

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);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema)

Example 27 with ResourceSchema

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);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ExampleRequestResponse(com.linkedin.restli.docgen.examplegen.ExampleRequestResponse) ResourceLevel(com.linkedin.restli.server.ResourceLevel) ArrayList(java.util.ArrayList) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) IOException(java.io.IOException) ActionSchema(com.linkedin.restli.restspec.ActionSchema) DataMap(com.linkedin.data.DataMap) ExampleRequestResponseGenerator(com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 28 with ResourceSchema

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);
        }
    }
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) DataMap(com.linkedin.data.DataMap)

Example 29 with ResourceSchema

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());
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) OptionsResponse(com.linkedin.restli.common.OptionsResponse) Test(org.testng.annotations.Test)

Example 30 with ResourceSchema

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);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Aggregations

ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)35 Test (org.testng.annotations.Test)13 DataMap (com.linkedin.data.DataMap)9 HashSet (java.util.HashSet)9 CompatibilityInfo (com.linkedin.restli.tools.idlcheck.CompatibilityInfo)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 File (java.io.File)5 StringArray (com.linkedin.data.template.StringArray)4 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CodeUtil (com.linkedin.pegasus.generator.CodeUtil)3 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)3 ResourceModelEncoder (com.linkedin.restli.internal.server.model.ResourceModelEncoder)3 FileInputStream (java.io.FileInputStream)3 DataSchema (com.linkedin.data.schema.DataSchema)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2