use of com.google.api.server.spi.ServiceContext in project endpoints-java by cloudendpoints.
the class SwaggerGeneratorTest method testWriteSwagger_MultiVersionEndpoint.
@Test
public void testWriteSwagger_MultiVersionEndpoint() throws Exception {
ServiceContext serviceContext = ServiceContext.create();
ImmutableList<ApiConfig> configs = ImmutableList.of(configLoader.loadConfiguration(serviceContext, Version1Endpoint.class), configLoader.loadConfiguration(serviceContext, Version2Endpoint.class));
Swagger swagger = generator.writeSwagger(configs, false, context);
Swagger expected = readExpectedAsSwagger("multi_version_endpoint.swagger");
checkSwagger(expected, swagger);
}
use of com.google.api.server.spi.ServiceContext in project endpoints-java by cloudendpoints.
the class SwaggerGeneratorTest method testWriteSwagger_MultiResourceEndpoint.
@Test
public void testWriteSwagger_MultiResourceEndpoint() throws Exception {
ServiceContext serviceContext = ServiceContext.create();
ImmutableList<ApiConfig> configs = ImmutableList.of(configLoader.loadConfiguration(serviceContext, NoResourceEndpoint.class), configLoader.loadConfiguration(serviceContext, Resource1Endpoint.class), configLoader.loadConfiguration(serviceContext, Resource2Endpoint.class));
Swagger swagger = generator.writeSwagger(configs, false, context);
Swagger expected = readExpectedAsSwagger("multi_resource_endpoint.swagger");
checkSwagger(expected, swagger);
}
use of com.google.api.server.spi.ServiceContext in project endpoints-java by cloudendpoints.
the class ProxyingDiscoveryProviderTest method setUp.
@Before
public void setUp() throws Exception {
ApiConfigLoader loader = new ApiConfigLoader();
ServiceContext context = ServiceContext.create();
ApiConfig apiConfig1 = loader.loadConfiguration(context, TestApi1.class);
ApiConfig apiConfig2 = loader.loadConfiguration(context, TestApi2.class);
ApiConfig apiConfig3 = loader.loadConfiguration(context, TestApiV2.class);
ApiConfig.Factory factory = new ApiConfig.Factory();
ApiConfig rewrittenApiConfig1 = factory.copy(apiConfig1);
ApiConfig rewrittenApiConfig2 = factory.copy(apiConfig2);
ApiConfig rewrittenApiConfig3 = factory.copy(apiConfig3);
rewrittenApiConfig1.setRoot(REWRITTEN_ROOT);
rewrittenApiConfig2.setRoot(REWRITTEN_ROOT);
rewrittenApiConfig3.setRoot(REWRITTEN_ROOT);
// Setup standard mocks on our discovery API.
when(discovery.apis()).thenReturn(apis);
when(apis.generateRest(any(com.google.api.services.discovery.model.ApiConfig.class))).thenReturn(restRequest);
when(apis.generateRpc(any(com.google.api.services.discovery.model.ApiConfig.class))).thenReturn(rpcRequest);
when(apis.generateDirectory(any(ApiConfigs.class))).thenReturn(directoryRequest);
// Used by individual document tests
when(configWriter.writeConfig(withConfigs(rewrittenApiConfig1, rewrittenApiConfig2))).thenReturn(ImmutableMap.of(V1_REWRITTEN_KEY, V1_JSON_API_CONFIG));
// Used by directory tests
when(configWriter.writeConfig(withConfigs(rewrittenApiConfig1, rewrittenApiConfig2, rewrittenApiConfig3))).thenReturn(ImmutableMap.of(V1_REWRITTEN_KEY, V1_JSON_API_CONFIG, V2_REWRITTEN_KEY, V2_JSON_API_CONFIG));
provider = new ProxyingDiscoveryProvider(ImmutableList.of(apiConfig1, apiConfig2, apiConfig3), configWriter, discovery);
}
use of com.google.api.server.spi.ServiceContext 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();
}
use of com.google.api.server.spi.ServiceContext 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 outputFile = new File(outputFilePath);
File outputDir = outputFile.getParentFile();
if (!outputDir.isDirectory() || outputFile.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, outputFile, UTF_8);
System.out.println("OpenAPI document written to " + outputFilePath);
}
return swaggerStr;
}
Aggregations