Search in sources :

Example 46 with ApiConfig

use of com.google.api.server.spi.config.model.ApiConfig 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 47 with ApiConfig

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

the class SystemService method registerService.

/**
 * Registers a service class.  Only public methods in this class and all its superclasses, except
 * Object, are registered.  Two methods are not allowed to have the same name.  Registering a
 * different service with an existing name is a no-op.
 *
 * @param serviceClass is the class to start parsing endpoints
 * @param service Service object
 * @return number of service methods added, -1 on duplicate insertion
 * @throws ApiConfigException
 */
public int registerService(Class<?> serviceClass, Object service) throws ApiConfigException {
    Preconditions.checkArgument(serviceClass.isInstance(service), "service is not an instance of " + serviceClass.getName());
    ApiConfig apiConfig = configLoader.loadConfiguration(serviceContext, serviceClass);
    return registerLoadedService(serviceClass, service, apiConfig);
}
Also used : ApiConfig(com.google.api.server.spi.config.model.ApiConfig)

Example 48 with ApiConfig

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

the class ApiConfigLoader method loadConfiguration.

public ApiConfig loadConfiguration(ServiceContext serviceContext, Class<?> endpointClass) throws ApiConfigException {
    ApiConfig config = configFactory.create(serviceContext, typeLoader, endpointClass);
    annotationSource.loadEndpointClass(serviceContext, endpointClass, config);
    for (ApiConfigSource apiConfigSource : apiConfigSources) {
        apiConfigSource.loadEndpointClass(serviceContext, endpointClass, config);
    }
    annotationSource.loadEndpointMethods(serviceContext, endpointClass, config.getApiClassConfig().getMethods());
    for (ApiConfigSource apiConfigSource : apiConfigSources) {
        apiConfigSource.loadEndpointMethods(serviceContext, endpointClass, config.getApiClassConfig().getMethods());
    }
    return config;
}
Also used : ApiConfig(com.google.api.server.spi.config.model.ApiConfig)

Example 49 with ApiConfig

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

the class ApiConfigLoader method reloadConfiguration.

public ApiConfig reloadConfiguration(ServiceContext serviceContext, Class<?> endpointClass, ApiConfig oldConfig) throws ApiConfigException {
    ApiConfig config = configFactory.copy(oldConfig);
    // ApiConfigAnnotationReader is static, so ignore.
    List<ApiConfigSource> apiMethodConfigSources = Lists.newArrayList();
    for (ApiConfigSource apiConfigSource : apiConfigSources) {
        if (!apiConfigSource.isStaticConfig(config)) {
            apiConfigSource.loadEndpointClass(serviceContext, endpointClass, config);
            apiMethodConfigSources.add(apiConfigSource);
        }
    }
    for (ApiConfigSource apiConfigSource : apiMethodConfigSources) {
        apiConfigSource.loadEndpointMethods(serviceContext, endpointClass, config.getApiClassConfig().getMethods());
    }
    return config;
}
Also used : ApiConfig(com.google.api.server.spi.config.model.ApiConfig)

Example 50 with ApiConfig

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

the class ApiConfigAnnotationReaderTest method testParameterAnnotations_none.

@Test
public void testParameterAnnotations_none() throws Exception {
    @Api
    class Endpoint {

        @SuppressWarnings("unused")
        public void method(int foo) {
        }
    }
    ApiConfig config = createConfig(Endpoint.class);
    annotationReader.loadEndpointClass(serviceContext, Endpoint.class, config);
    annotationReader.loadEndpointMethods(serviceContext, Endpoint.class, config.getApiClassConfig().getMethods());
    ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
    ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs());
    validateParameter(parameterConfig, null, false, null, int.class, null, int.class);
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Aggregations

ApiConfig (com.google.api.server.spi.config.model.ApiConfig)91 Test (org.junit.Test)72 Api (com.google.api.server.spi.config.Api)29 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)26 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)20 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)18 ApiKey (com.google.api.server.spi.config.model.ApiKey)9 Swagger (io.swagger.models.Swagger)8 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)7 TypeLoader (com.google.api.server.spi.TypeLoader)5 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)4 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)4 ServiceContext (com.google.api.server.spi.ServiceContext)3 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ApiClass (com.google.api.server.spi.config.ApiClass)2 ApiFrontendLimits (com.google.api.server.spi.config.ApiFrontendLimits)2 ApiReference (com.google.api.server.spi.config.ApiReference)2 Named (com.google.api.server.spi.config.Named)2 ApiAuthConfig (com.google.api.server.spi.config.model.ApiAuthConfig)2