GetDiscoveryDocAction extends EndpointsToolAction { 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(); } GetDiscoveryDocAction(); @Override boolean execute(); Map<String, String> getDiscoveryDoc(URL[] classPath, String outputDirPath,
      List<String> serviceClassNames, String hostname, String basePath, boolean outputToDisk); @Override String getUsageString(); static final String NAME; }
@Test public void testGetDiscoveryDoc() throws Exception { tool.execute(new String[]{GetDiscoveryDocAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath", option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir", option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com", option(EndpointsToolAction.OPTION_BASE_PATH_SHORT), "/api", "MyService", "MyService2"}); assertFalse(usagePrinted); assertThat(Lists.newArrayList(classPath)) .containsExactly(new File("classPath").toURI().toURL(), new File(new File(EndpointsToolAction.DEFAULT_WAR_PATH).getAbsoluteFile(), "/WEB-INF/classes") .toURI() .toURL()); assertEquals("outputDir", outputDirPath); assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames); assertTrue(outputToDisk); assertThat(hostname).isEqualTo("foo.com"); assertThat(basePath).isEqualTo("/api"); }