Search in sources :

Example 1 with ResourceModelEncoder

use of com.linkedin.restli.internal.server.model.ResourceModelEncoder in project rest.li by linkedin.

the class ResourceSchemaCollection method loadOrCreateResourceSchema.

/**
   * For each given {@link ResourceModel}, the classpath is checked for a .restspec.json 
   * matching the name of the {@link ResourceModel},  if found it is loaded.  If a .restspec.json file 
   * is not found, one is created {@link ResourceSchemaCollection} from specified root {@link ResourceModel}.
   * All resources will be recursively traversed to discover subresources.
   * Root resources not specified are excluded.
   *
   * @param rootResources root resources in ResourceModel type
   * @return constructed ResourceSchemaCollection
   */
public static ResourceSchemaCollection loadOrCreateResourceSchema(Map<String, ResourceModel> rootResources) {
    final ResourceModelEncoder encoder = new ResourceModelEncoder(new NullDocsProvider());
    final Map<String, ResourceSchema> schemaMap = new TreeMap<String, ResourceSchema>();
    for (ResourceModel resource : rootResources.values()) {
        schemaMap.put(resource.getName(), encoder.loadOrBuildResourceSchema(resource));
    }
    return new ResourceSchemaCollection(schemaMap);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceModelEncoder(com.linkedin.restli.internal.server.model.ResourceModelEncoder) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) TreeMap(java.util.TreeMap) NullDocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.NullDocsProvider)

Example 2 with ResourceModelEncoder

use of com.linkedin.restli.internal.server.model.ResourceModelEncoder in project rest.li by linkedin.

the class RestLiSnapshotExporter method generateSnapshotFiles.

private GeneratorResult generateSnapshotFiles(String apiName, String outdir, Map<String, ResourceModel> rootResourceMap, DocsProvider docsProvider) throws IOException {
    SnapshotResult result = new SnapshotResult();
    final File outdirFile = new File(outdir);
    if (!outdirFile.exists()) {
        if (!outdirFile.mkdirs()) {
            throw new RuntimeException("Output directory '" + outdir + "' could not be created!");
        }
    }
    if (!outdirFile.isDirectory()) {
        throw new RuntimeException("Output directory '" + outdir + "' is not a directory");
    }
    if (!outdirFile.canRead() || !outdirFile.canWrite()) {
        throw new RuntimeException("Output directory '" + outdir + "' must be writeable");
    }
    final ResourceModelEncoder encoder = new ResourceModelEncoder(docsProvider);
    final List<ResourceSchema> rootResourceNodes = new ArrayList<ResourceSchema>();
    for (Map.Entry<String, ResourceModel> entry : rootResourceMap.entrySet()) {
        final ResourceSchema rootResourceNode = encoder.buildResourceSchema(entry.getValue());
        rootResourceNodes.add(rootResourceNode);
    }
    for (ResourceSchema rootResourceNode : rootResourceNodes) {
        String fileName = rootResourceNode.getName();
        if (rootResourceNode.hasNamespace()) {
            final String namespace = rootResourceNode.getNamespace();
            fileName = namespace + "." + fileName;
        }
        if (apiName != null && !apiName.isEmpty()) {
            fileName = apiName + "-" + fileName;
        }
        File writtenFile = writeSnapshotFile(outdirFile, fileName, rootResourceNode);
        result.addModifiedFile(writtenFile);
        result.addTargetFile(writtenFile);
    }
    return result;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceModelEncoder(com.linkedin.restli.internal.server.model.ResourceModelEncoder) ArrayList(java.util.ArrayList) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ResourceModelEncoder

use of com.linkedin.restli.internal.server.model.ResourceModelEncoder in project rest.li by linkedin.

the class RestLiResourceModelExporter method generateIDLFiles.

private GeneratorResult generateIDLFiles(String apiName, String outdir, Map<String, ResourceModel> rootResourceMap, DocsProvider docsProvider) throws IOException {
    Result result = new Result();
    final File outdirFile = new File(outdir);
    if (!outdirFile.exists()) {
        if (!outdirFile.mkdirs()) {
            throw new RuntimeException("Output directory '" + outdir + "' could not be created!");
        }
    }
    if (!outdirFile.isDirectory()) {
        throw new RuntimeException("Output directory '" + outdir + "' is not a directory");
    }
    if (!outdirFile.canRead() || !outdirFile.canWrite()) {
        throw new RuntimeException("Output directory '" + outdir + "' must be writeable");
    }
    final ResourceModelEncoder encoder = new ResourceModelEncoder(docsProvider);
    final List<ResourceSchema> rootResourceNodes = new ArrayList<ResourceSchema>();
    for (Entry<String, ResourceModel> entry : rootResourceMap.entrySet()) {
        final ResourceSchema rootResourceNode = encoder.buildResourceSchema(entry.getValue());
        rootResourceNodes.add(rootResourceNode);
    }
    for (ResourceSchema rootResourceNode : rootResourceNodes) {
        String fileName = rootResourceNode.getName();
        if (rootResourceNode.getNamespace() != null) {
            final String namespace = rootResourceNode.getNamespace();
            fileName = namespace + "." + fileName;
        }
        if (apiName != null && !apiName.isEmpty()) {
            fileName = apiName + "-" + fileName;
        }
        File writtenFile = writeIDLFile(outdirFile, fileName, rootResourceNode);
        result.addModifiedFile(writtenFile);
        result.addTargetFile(writtenFile);
    }
    return result;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceModelEncoder(com.linkedin.restli.internal.server.model.ResourceModelEncoder) ArrayList(java.util.ArrayList) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) File(java.io.File) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult)

Aggregations

ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)3 ResourceModelEncoder (com.linkedin.restli.internal.server.model.ResourceModelEncoder)3 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GeneratorResult (com.linkedin.pegasus.generator.GeneratorResult)1 NullDocsProvider (com.linkedin.restli.internal.server.model.ResourceModelEncoder.NullDocsProvider)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1