Search in sources :

Example 6 with DiscoveryContext

use of com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext in project endpoints-java by cloudendpoints.

the class DiscoveryGeneratorTest method testWriteDiscovery_MapEndpoint_Legacy.

@Test
public void testWriteDiscovery_MapEndpoint_Legacy() throws Exception {
    System.setProperty(MAP_SCHEMA_FORCE_JSON_MAP_SCHEMA.systemPropertyName, "");
    try {
        RestDescription doc = getDiscovery(new DiscoveryContext(), MapEndpoint.class);
        RestDescription expected = readExpectedAsDiscovery("map_endpoint_legacy.json");
        compareDiscovery(expected, doc);
    } finally {
        System.clearProperty(MAP_SCHEMA_FORCE_JSON_MAP_SCHEMA.systemPropertyName);
    }
}
Also used : DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) RestDescription(com.google.api.services.discovery.model.RestDescription) Test(org.junit.Test)

Example 7 with DiscoveryContext

use of com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext in project endpoints-java by cloudendpoints.

the class LocalDiscoveryProvider method ensureDiscoveryResult.

private synchronized void ensureDiscoveryResult() {
    if (discoveryDocs == null) {
        DiscoveryGenerator.Result result = generator.writeDiscovery(getAllApiConfigs(), new DiscoveryContext().setApiRoot(PLACEHOLDER_ROOT).setGenerateAll(false), repository);
        directoryList = result.directory();
        ImmutableMap.Builder<ApiKey, RestDescription> builder = ImmutableMap.builder();
        for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {
            ApiKey rootedKey = entry.getKey();
            builder.put(new ApiKey(rootedKey.getName(), rootedKey.getVersion(), null), entry.getValue());
        }
        discoveryDocs = builder.build();
    }
}
Also used : DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) ApiKey(com.google.api.server.spi.config.model.ApiKey) RestDescription(com.google.api.services.discovery.model.RestDescription) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with DiscoveryContext

use of com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext in project endpoints-java by cloudendpoints.

the class GetDiscoveryDocAction method getDiscoveryDoc.

/**
 * Generates a Java client library for an API.  Combines the steps of generating API
 * configuration, generating Discovery doc and generating client library into one.
 * @param classPath Class path to load service classes and their dependencies
 * @param outputDirPath Directory to write output files into
 * @param serviceClassNames Array of service class names of the API
 * @param hostname The hostname to use
 * @param basePath The base path to use
 * @param outputToDisk Whether or not to output discovery docs to disk
 */
public Map<String, String> getDiscoveryDoc(URL[] classPath, String outputDirPath, List<String> serviceClassNames, String hostname, String basePath, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputDirPath);
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputDirPath + " is not a directory");
    }
    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    TypeLoader typeLoader = new TypeLoader(classLoader);
    SchemaRepository schemaRepository = new SchemaRepository(typeLoader);
    ApiConfigValidator validator = new ApiConfigValidator(typeLoader, schemaRepository);
    DiscoveryGenerator discoveryGenerator = new DiscoveryGenerator(typeLoader);
    List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClassNames.size());
    ImmutableListMultimap<ApiKey, ApiConfig> configsByKey = Multimaps.index(apiConfigs, new Function<ApiConfig, ApiKey>() {

        @Override
        public ApiKey apply(ApiConfig input) {
            return input.getApiKey();
        }
    });
    for (ApiKey key : configsByKey.keys()) {
        validator.validate(configsByKey.get(key));
    }
    ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
    ServiceContext serviceContext = ServiceContext.createFromHostname(hostname, ServiceContext.DEFAULT_API_NAME);
    for (Class<?> serviceClass : loadClasses(classLoader, serviceClassNames)) {
        apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
    }
    DiscoveryGenerator.Result result = discoveryGenerator.writeDiscovery(apiConfigs, new DiscoveryContext().setHostname(hostname).setBasePath(basePath), schemaRepository);
    ObjectWriter writer = ObjectMapperUtil.createStandardObjectMapper().writer(new EndpointsPrettyPrinter());
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {
        ApiKey key = entry.getKey();
        String discoveryDocFilePath = outputDir + "/" + key.getName() + "-" + key.getVersion() + "-rest.discovery";
        String docString = writer.writeValueAsString(entry.getValue());
        if (outputToDisk) {
            Files.write(docString, new File(discoveryDocFilePath), UTF_8);
            System.out.println("API Discovery Document written to " + discoveryDocFilePath);
        }
        builder.put(discoveryDocFilePath, docString);
    }
    return builder.build();
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) URLClassLoader(java.net.URLClassLoader) ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) EndpointsPrettyPrinter(com.google.api.server.spi.response.EndpointsPrettyPrinter) ServiceContext(com.google.api.server.spi.ServiceContext) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) RestDescription(com.google.api.services.discovery.model.RestDescription) DiscoveryGenerator(com.google.api.server.spi.discovery.DiscoveryGenerator) ImmutableMap(com.google.common.collect.ImmutableMap) DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository)

Example 9 with DiscoveryContext

use of com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext in project endpoints-java by cloudendpoints.

the class DiscoveryGeneratorTest method testWriteDiscovery_FooEndpointLocalhost.

@Test
public void testWriteDiscovery_FooEndpointLocalhost() throws Exception {
    RestDescription doc = getDiscovery(new DiscoveryContext().setApiRoot("http://localhost:8080/api"), FooEndpoint.class);
    RestDescription expected = readExpectedAsDiscovery("foo_endpoint_localhost.json");
    compareDiscovery(expected, doc);
}
Also used : DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) RestDescription(com.google.api.services.discovery.model.RestDescription) Test(org.junit.Test)

Example 10 with DiscoveryContext

use of com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext in project endpoints-java by cloudendpoints.

the class DiscoveryGeneratorTest method testWriteDiscovery_ArrayEndpoint.

@Test
public void testWriteDiscovery_ArrayEndpoint() throws Exception {
    RestDescription doc = getDiscovery(new DiscoveryContext(), ArrayEndpoint.class);
    RestDescription expected = readExpectedAsDiscovery("array_endpoint.json");
    compareDiscovery(expected, doc);
}
Also used : DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) RestDescription(com.google.api.services.discovery.model.RestDescription) Test(org.junit.Test)

Aggregations

DiscoveryContext (com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext)16 RestDescription (com.google.api.services.discovery.model.RestDescription)14 Test (org.junit.Test)14 ApiKey (com.google.api.server.spi.config.model.ApiKey)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ServiceContext (com.google.api.server.spi.ServiceContext)1 TypeLoader (com.google.api.server.spi.TypeLoader)1 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)1 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)1 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)1 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)1 DiscoveryGenerator (com.google.api.server.spi.discovery.DiscoveryGenerator)1 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)1 File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1