Search in sources :

Example 16 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class OptionsResponseDecoder method wrapResponse.

@Override
public OptionsResponse wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) throws IOException {
    if (dataMap == null) {
        return null;
    }
    DataMap resources = dataMap.getDataMap(RESOURCES);
    if (resources == null)
        resources = new DataMap();
    HashMap<String, ResourceSchema> resourceMap = new HashMap<String, ResourceSchema>(resources.size());
    for (Map.Entry<String, Object> entry : resources.entrySet()) {
        resourceMap.put(entry.getKey(), new ResourceSchema((DataMap) entry.getValue()));
    }
    DataMap schemas = dataMap.getDataMap(MODELS);
    if (schemas == null)
        schemas = new DataMap();
    HashMap<String, DataSchema> dataSchemaMap = new HashMap<String, DataSchema>(schemas.size());
    for (Map.Entry<String, Object> entry : schemas.entrySet()) {
        String schemaText = CODEC.mapToString((DataMap) entry.getValue());
        dataSchemaMap.put(entry.getKey(), DataTemplateUtil.parseSchema(schemaText));
    }
    return new OptionsResponse(resourceMap, dataSchemaMap);
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) HashMap(java.util.HashMap) OptionsResponse(com.linkedin.restli.common.OptionsResponse) DataMap(com.linkedin.data.DataMap)

Example 17 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class FinderTest method test.

@Test
public void test() throws IOException {
    final String findersFilename = IDLS_DIR + FINDERS_FILE;
    final ResourceSchema findersIdl = _codec.readResourceSchema(new FileInputStream(findersFilename));
    final FinderSchemaArray finders = findersIdl.getCollection().getFinders();
    for (FinderSchema finder : finders) {
        if ("searchWithoutMetadata".equals(finder.getName())) {
            Assert.assertFalse(finder.hasMetadata());
        } else if ("searchWithMetadata".equals(finder.getName())) {
            Assert.assertEquals(finder.getMetadata().getType(), SearchMetadata.class.getName());
        } else if ("basicSearch".equals(finder.getName())) {
            Assert.assertFalse(finder.hasMetadata());
        } else if ("predefinedSearch".equals(finder.getName())) {
            Assert.assertFalse(finder.hasMetadata());
        } else {
            throw new RuntimeException("Unknown finder is added to com.linkedin.restli.examples.greetings.server.FindersResource");
        }
    }
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) FinderSchema(com.linkedin.restli.restspec.FinderSchema) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 18 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class ResourceModelEncoder method loadOrBuildResourceSchema.

/**
   * Checks if a matching .restspec.json file exists in the classpath for the given {@link ResourceModel}.
   * If one is found it is loaded.  If not, one is built from the {@link ResourceModel}.
   *
   * The .restspec.json is preferred because it contains the exact idl that was generated for the resource
   * and also includees includes javadoc from the server class in the restspec.json.
   *
   * @param resourceModel provides the name and namespace of the schema to load or build
   * @return the {@link ResourceSchema} for the given {@link ResourceModel}
   */
public ResourceSchema loadOrBuildResourceSchema(final ResourceModel resourceModel) {
    StringBuilder resourceFilePath = new StringBuilder();
    if (resourceModel.getNamespace() != null) {
        resourceFilePath.append(resourceModel.getNamespace());
        resourceFilePath.append(".");
    }
    resourceFilePath.append(resourceModel.getName());
    resourceFilePath.append(RestConstants.RESOURCE_MODEL_FILENAME_EXTENSION);
    try {
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourceFilePath.toString());
        if (stream == null) {
            // restspec.json file not found, building one instead
            return buildResourceSchema(resourceModel);
        } else {
            DataMap resourceSchemaDataMap = codec.bytesToMap(IOUtils.toByteArray(stream));
            return new ResourceSchema(resourceSchemaDataMap);
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to read " + resourceFilePath.toString() + " from classpath.", e);
    }
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) InputStream(java.io.InputStream) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 19 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class ResourceModelEncoder method buildResourceSchema.

/**
   * @param resourceModel {@link ResourceModel} to build the schema for
   * @return {@link ResourceSchema} for the provided resource model
   */
public ResourceSchema buildResourceSchema(final ResourceModel resourceModel) {
    ResourceSchema rootNode = new ResourceSchema();
    switch(resourceModel.getResourceType()) {
        case ACTIONS:
            appendActionsModel(rootNode, resourceModel);
            break;
        case SIMPLE:
            appendSimple(rootNode, resourceModel);
            break;
        default:
            appendCollection(rootNode, resourceModel);
            break;
    }
    final DataMap customAnnotation = resourceModel.getCustomAnnotationData();
    if (!customAnnotation.isEmpty()) {
        rootNode.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
    }
    return rootNode;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) DataMap(com.linkedin.data.DataMap)

Example 20 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class ResourceModelEncoder method buildEntitySchema.

private EntitySchema buildEntitySchema(ResourceModel resourceModel) {
    EntitySchema entityNode = new EntitySchema();
    entityNode.setPath(buildPathForEntity(resourceModel));
    if (resourceModel.getResourceLevel() == ResourceLevel.COLLECTION) {
        ActionSchemaArray actions = createActions(resourceModel, ResourceLevel.ENTITY);
        if (actions.size() > 0) {
            entityNode.setActions(actions);
        }
    }
    // subresources
    ResourceSchemaArray subresources = new ResourceSchemaArray();
    for (ResourceModel subResourceModel : resourceModel.getSubResources()) {
        ResourceSchema subresource = new ResourceSchema();
        switch(subResourceModel.getResourceType()) {
            case COLLECTION:
            case ASSOCIATION:
                appendCollection(subresource, subResourceModel);
                break;
            case SIMPLE:
                appendSimple(subresource, subResourceModel);
                break;
            default:
                break;
        }
        final DataMap customAnnotation = subResourceModel.getCustomAnnotationData();
        if (!customAnnotation.isEmpty()) {
            subresource.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
        }
        subresources.add(subresource);
    }
    if (subresources.size() > 0) {
        Collections.sort(subresources, new Comparator<ResourceSchema>() {

            @Override
            public int compare(ResourceSchema resourceSchema, ResourceSchema resourceSchema2) {
                return resourceSchema.getName().compareTo(resourceSchema2.getName());
            }
        });
        entityNode.setSubresources(subresources);
    }
    return entityNode;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) ResourceSchemaArray(com.linkedin.restli.restspec.ResourceSchemaArray) EntitySchema(com.linkedin.restli.restspec.EntitySchema) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) 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