Search in sources :

Example 1 with SchemaRepository

use of com.google.api.server.spi.config.model.SchemaRepository in project endpoints-java by cloudendpoints.

the class GenApiConfigAction method genApiConfig.

/**
 * Generates API configuration for an array of service classes.
 * @param classPath Class path to load service classes and their dependencies
 * @param outputDirPath Directory to write API configuration files into
 * @param serviceClassNames Array of service class names of the API
 * @param warPath Directory or file containing a WAR layout
 * @param outputToDisk Iff {@code true}, outputs a *.api file to disk for each API.
 * @return JSON-formatted configurations for each API.
 */
public Iterable<String> genApiConfig(URL[] classPath, String outputDirPath, String warPath, List<String> serviceClassNames, 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);
    JsonConfigWriter jsonConfigWriter = new JsonConfigWriter(typeLoader, validator);
    ApiConfigGenerator generator = new AnnotationApiConfigGenerator(jsonConfigWriter, classLoader, configFactory);
    Map<String, String> apiConfigs = generator.generateConfig(ServiceContext.create(AppEngineUtil.getApplicationId(warPath), ServiceContext.DEFAULT_API_NAME), loadClasses(classLoader, serviceClassNames));
    if (outputToDisk) {
        for (Map.Entry<String, String> entry : apiConfigs.entrySet()) {
            String apiConfigFileName = entry.getKey();
            String apiConfigFileContent = entry.getValue();
            String apiConfigFilePath = outputDir + "/" + apiConfigFileName;
            Files.asCharSink(new File(apiConfigFilePath), UTF_8).write(apiConfigFileContent);
            System.out.println("API configuration written to " + apiConfigFilePath);
        }
    }
    return apiConfigs.values();
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) JsonConfigWriter(com.google.api.server.spi.config.jsonwriter.JsonConfigWriter) URLClassLoader(java.net.URLClassLoader) ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Map(java.util.Map) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository)

Example 2 with SchemaRepository

use of com.google.api.server.spi.config.model.SchemaRepository in project endpoints-java by cloudendpoints.

the class ApiConfigValidatorTest method setUp.

@Before
public void setUp() throws Exception {
    TypeLoader typeLoader = new TypeLoader(ApiConfigValidator.class.getClassLoader());
    SchemaRepository schemaRepository = new SchemaRepository(typeLoader);
    validator = new ApiConfigValidator(typeLoader, schemaRepository);
    configFactory = new ApiConfig.Factory();
    configLoader = new ApiConfigLoader();
    config = configLoader.loadConfiguration(ServiceContext.create(), TestEndpoint.class);
}
Also used : TestEndpoint(com.google.api.server.spi.testing.TestEndpoint) ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository) Before(org.junit.Before)

Example 3 with SchemaRepository

use of com.google.api.server.spi.config.model.SchemaRepository in project endpoints-java by cloudendpoints.

the class DiscoveryGeneratorTest method setUp.

@Before
public void setUp() throws Exception {
    TypeLoader typeLoader = new TypeLoader(getClass().getClassLoader());
    ApiConfigAnnotationReader annotationReader = new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes());
    this.configLoader = new ApiConfigLoader(new ApiConfig.Factory(), typeLoader, annotationReader);
    this.generator = new DiscoveryGenerator(typeLoader);
    this.schemaRepository = new SchemaRepository(typeLoader);
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) TypeLoader(com.google.api.server.spi.TypeLoader) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository) Before(org.junit.Before)

Example 4 with SchemaRepository

use of com.google.api.server.spi.config.model.SchemaRepository in project endpoints-java by cloudendpoints.

the class SwaggerGenerator method writeSwagger.

public Swagger writeSwagger(Iterable<ApiConfig> configs, boolean writeInternal, SwaggerContext context) throws ApiConfigException {
    try {
        TypeLoader typeLoader = new TypeLoader(SwaggerGenerator.class.getClassLoader());
        SchemaRepository repo = new SchemaRepository(typeLoader);
        GenerationContext genCtx = new GenerationContext();
        genCtx.validator = new ApiConfigValidator(typeLoader, repo);
        genCtx.writeInternal = writeInternal;
        genCtx.schemata = new SchemaRepository(typeLoader);
        return writeSwagger(configs, context, genCtx);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) TypeLoader(com.google.api.server.spi.TypeLoader) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository)

Example 5 with SchemaRepository

use of com.google.api.server.spi.config.model.SchemaRepository 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)

Aggregations

TypeLoader (com.google.api.server.spi.TypeLoader)5 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)5 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)3 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)3 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)3 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)2 File (java.io.File)2 URLClassLoader (java.net.URLClassLoader)2 Map (java.util.Map)2 Before (org.junit.Before)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 ServiceContext (com.google.api.server.spi.ServiceContext)1 JsonConfigWriter (com.google.api.server.spi.config.jsonwriter.JsonConfigWriter)1 ApiKey (com.google.api.server.spi.config.model.ApiKey)1 DiscoveryGenerator (com.google.api.server.spi.discovery.DiscoveryGenerator)1 DiscoveryContext (com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext)1 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)1 TestEndpoint (com.google.api.server.spi.testing.TestEndpoint)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1