diff --git a/documentation/src/main/asciidoc/topics/ref_rest_cache_managers.adoc b/documentation/src/main/asciidoc/topics/ref_rest_cache_managers.adoc index 6d73d4a43d45..0d5c8e8a8fb5 100644 --- a/documentation/src/main/asciidoc/topics/ref_rest_cache_managers.adoc +++ b/documentation/src/main/asciidoc/topics/ref_rest_cache_managers.adoc @@ -134,6 +134,14 @@ configuration, as in the following example: include::json_examples/rest_cache_config_response.json[] ---- +[id='rest_v2_cache_manager_cache_templates'] += Listing Cache Templates +List all templates available for creating caches with `GET` requests. + +[source,options="nowrap",subs=attributes+] +---- +GET /rest/v2/cache-managers/{cacheManagerName}/cache-configs/templates +---- [id='rest_v2_cache_manager_cache_list'] = (Experimental) Obtaining Cache Status and Information diff --git a/server/rest/src/main/java/org/infinispan/rest/resources/CacheManagerResource.java b/server/rest/src/main/java/org/infinispan/rest/resources/CacheManagerResource.java index df5835c4c0fc..590839cb83fc 100644 --- a/server/rest/src/main/java/org/infinispan/rest/resources/CacheManagerResource.java +++ b/server/rest/src/main/java/org/infinispan/rest/resources/CacheManagerResource.java @@ -84,6 +84,7 @@ public Invocations getInvocations() { // Config .invocation().methods(GET).path("/v2/cache-managers/{name}/cache-configs").handleWith(this::getAllCachesConfiguration) + .invocation().methods(GET).path("/v2/cache-managers/{name}/cache-configs/templates").handleWith(this::getAllCachesConfigurationTemplates) .invocation().methods(POST).path("/v2/cache-managers/{name}/config").withAction("toJSON").handleWith(this::convertToJson) // Cache Manager config @@ -236,13 +237,40 @@ private CompletionStage getAllCachesConfiguration(RestRequest requ try { Set cacheConfigurationNames = cacheManager.getCacheConfigurationNames(); - Set configurations = cacheConfigurationNames.stream() + List configurations = cacheConfigurationNames.stream() .filter(n -> !internalCacheRegistry.isInternalCache(n)) + .distinct() .map(n -> { Configuration cacheConfiguration = cacheManager.getCacheConfiguration(n); String json = jsonWriter.toJSON(cacheConfiguration); return new NamedCacheConfiguration(n, json); - }).collect(Collectors.toSet()); + }) + .sorted(Comparator.comparing(c -> c.name)) + .collect(Collectors.toList()); + + byte[] bytes = objectMapper.writeValueAsBytes(configurations); + responseBuilder.contentType(APPLICATION_JSON).entity(bytes); + } catch (JsonProcessingException e) { + responseBuilder.status(HttpResponseStatus.INTERNAL_SERVER_ERROR); + } + + return completedFuture(responseBuilder.build()); + } + + private CompletionStage getAllCachesConfigurationTemplates(RestRequest request) { + NettyRestResponse.Builder responseBuilder = checkCacheManager(request); + if (responseBuilder.getHttpStatus() == NOT_FOUND) return completedFuture(responseBuilder.build()); + + try { + Set cacheConfigurationNames = cacheManager.getCacheConfigurationNames(); + + List configurations = cacheConfigurationNames.stream() + .filter(n -> !internalCacheRegistry.isInternalCache(n)) + .filter(n -> cacheManager.getCacheConfiguration(n).isTemplate()) + .distinct() + .map(n -> new NamedCacheConfiguration(n, jsonWriter.toJSON(cacheManager.getCacheConfiguration(n)))) + .sorted(Comparator.comparing(c -> c.name)) + .collect(Collectors.toList()); byte[] bytes = objectMapper.writeValueAsBytes(configurations); responseBuilder.contentType(APPLICATION_JSON).entity(bytes); diff --git a/server/rest/src/test/java/org/infinispan/rest/resources/CacheManagerResourceTest.java b/server/rest/src/test/java/org/infinispan/rest/resources/CacheManagerResourceTest.java index 2ee754da2f7b..bfe09588c11e 100644 --- a/server/rest/src/test/java/org/infinispan/rest/resources/CacheManagerResourceTest.java +++ b/server/rest/src/test/java/org/infinispan/rest/resources/CacheManagerResourceTest.java @@ -41,13 +41,18 @@ public class CacheManagerResourceTest extends AbstractRestResourceTest { private Configuration cache2Config; private ObjectMapper mapper = new ObjectMapper(); private JsonWriter jsonWriter = new JsonWriter(); + private Configuration templateConfig; @Override protected void defineCaches(EmbeddedCacheManager cm) { cache1Config = getCache1Config(); cache2Config = getCache2Config(); + ConfigurationBuilder templateConfigBuilder = new ConfigurationBuilder(); + templateConfigBuilder.template(true).clustering().cacheMode(LOCAL).encoding().key().mediaType(TEXT_PLAIN_TYPE); + templateConfig = templateConfigBuilder.build(); cm.defineConfiguration("cache1", cache1Config); cm.defineConfiguration("cache2", cache2Config); + cm.defineConfiguration("template", templateConfig); } private Configuration getCache1Config() { @@ -92,13 +97,30 @@ public void testCacheConfigs() throws Exception { ResponseAssertion.assertThat(response).isOk(); String json = response.getContentAsString(); - JsonNode jsonNode = mapper.readTree(json); - Map cachesAndConfig = cacheAndConfig((ArrayNode) jsonNode); + ArrayNode jsonNode = (ArrayNode) mapper.readTree(json); + Map cachesAndConfig = cacheAndConfig(jsonNode); - assertEquals(cachesAndConfig.get("cache1"), jsonWriter.toJSON(cache1Config)); + assertEquals(cachesAndConfig.get("template"), jsonWriter.toJSON(templateConfig)); + assertEquals(cachesAndConfig.get("cache2"), jsonWriter.toJSON(cache2Config)); assertEquals(cachesAndConfig.get("cache2"), jsonWriter.toJSON(cache2Config)); } + @Test + public void testCacheConfigsTemplates() throws Exception { + String accept = "text/plain; q=0.9, application/json; q=0.6"; + String url = String.format("http://localhost:%d/rest/v2/cache-managers/default/cache-configs/templates", restServer().getPort()); + ContentResponse response = client.newRequest(url).header("Accept", accept).send(); + ResponseAssertion.assertThat(response).isOk(); + + String json = response.getContentAsString(); + ArrayNode jsonNode = (ArrayNode) mapper.readTree(json); + Map cachesAndConfig = cacheAndConfig(jsonNode); + + assertEquals(cachesAndConfig.get("template"), jsonWriter.toJSON(templateConfig)); + assertFalse(cachesAndConfig.containsKey("cache1")); + assertFalse(cachesAndConfig.containsKey("cache2")); + } + @Test public void testCaches() throws Exception { String accept = "text/plain; q=0.9, application/json; q=0.6";