Search in sources :

Example 11 with TypeLoader

use of com.google.api.server.spi.TypeLoader 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 12 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class GetOpenApiDocAction method genOpenApiDoc.

/**
 * Generates an OpenAPI document for an array of service classes.
 *
 * @param classPath Class path to load service classes and their dependencies
 * @param outputFilePath File to store the OpenAPI document in
 * @param hostname The hostname to use for the OpenAPI document
 * @param basePath The base path to use for the OpenAPI document, e.g. /_ah/api
 * @param serviceClassNames Array of service class names of the API
 * @param outputToDisk Iff {@code true}, outputs a openapi.json to disk.
 * @return a single OpenAPI document representing all service classes.
 */
public String genOpenApiDoc(URL[] classPath, String outputFilePath, String hostname, String basePath, List<String> serviceClassNames, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputFilePath).getParentFile();
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputFilePath + " is not a file");
    }
    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    Class<?>[] serviceClasses = loadClasses(classLoader, serviceClassNames);
    List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClasses.length);
    TypeLoader typeLoader = new TypeLoader(classLoader);
    ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
    ServiceContext serviceContext = ServiceContext.create();
    for (Class<?> serviceClass : serviceClasses) {
        apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
    }
    SwaggerGenerator generator = new SwaggerGenerator();
    SwaggerContext swaggerContext = new SwaggerContext().setHostname(hostname).setBasePath(basePath);
    Swagger swagger = generator.writeSwagger(apiConfigs, true, swaggerContext);
    String swaggerStr = Json.mapper().writer(new EndpointsPrettyPrinter()).writeValueAsString(swagger);
    if (outputToDisk) {
        Files.write(swaggerStr, new File(outputFilePath), UTF_8);
        System.out.println("OpenAPI document written to " + outputFilePath);
    }
    return swaggerStr;
}
Also used : ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) EndpointsPrettyPrinter(com.google.api.server.spi.response.EndpointsPrettyPrinter) ServiceContext(com.google.api.server.spi.ServiceContext) TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) SwaggerGenerator(com.google.api.server.spi.swagger.SwaggerGenerator) URLClassLoader(java.net.URLClassLoader) Swagger(io.swagger.models.Swagger) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 13 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class SchemaRepositoryTest method setUp.

@Before
public void setUp() throws Exception {
    TypeLoader typeLoader = new TypeLoader(getClass().getClassLoader());
    ApiConfigAnnotationReader annotationReader = new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes());
    this.repo = new SchemaRepository(typeLoader);
    this.configLoader = new ApiConfigLoader(new ApiConfig.Factory(), typeLoader, annotationReader);
    this.config = configLoader.loadConfiguration(ServiceContext.create(), FooEndpoint.class);
}
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) Before(org.junit.Before)

Example 14 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class TypesTest method setUp.

@Before
public void setUp() throws Exception {
    ServiceContext serviceContext = ServiceContext.create();
    TypeLoader typeLoader = new TypeLoader();
    apiConfig = new ApiConfig.Factory().create(serviceContext, typeLoader, TestEndpoint.class);
    apiConfig.getSerializationConfig().addSerializationConfig(IntegerToStringSerializer.class);
    apiConfig.getSerializationConfig().addSerializationConfig(LongToStringSerializer.class);
    apiConfig.getSerializationConfig().addSerializationConfig(FloatToStringSerializer.class);
}
Also used : TestEndpoint(com.google.api.server.spi.testing.TestEndpoint) ServiceContext(com.google.api.server.spi.ServiceContext) TypeLoader(com.google.api.server.spi.TypeLoader) Before(org.junit.Before)

Example 15 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class ApiParameterConfigTest method setUp.

@Before
public void setUp() throws Exception {
    serializationConfig = new ApiSerializationConfig();
    typeLoader = new TypeLoader();
    Mockito.when(apiMethodConfig.getApiClassConfig()).thenReturn(apiClassConfig);
    Mockito.when(apiClassConfig.getApiConfig()).thenReturn(apiConfig);
    Mockito.when(apiConfig.getSerializationConfig()).thenReturn(serializationConfig);
    config = new ApiParameterConfig(apiMethodConfig, "bleh", null, false, null, TypeToken.of(String.class), typeLoader);
    configWithArray = new ApiParameterConfig(apiMethodConfig, "bleh", null, false, null, TypeToken.of(Boolean[].class), typeLoader);
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) Before(org.junit.Before)

Aggregations

TypeLoader (com.google.api.server.spi.TypeLoader)18 Before (org.junit.Before)11 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)7 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)6 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)6 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)5 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)5 ServiceContext (com.google.api.server.spi.ServiceContext)4 TestEndpoint (com.google.api.server.spi.testing.TestEndpoint)4 ApiKey (com.google.api.server.spi.config.model.ApiKey)3 File (java.io.File)3 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 Api (com.google.api.server.spi.config.Api)2 ApiSerializationConfig (com.google.api.server.spi.config.model.ApiSerializationConfig)2 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)2 Map (java.util.Map)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1