diff --git a/server/rest/src/main/java/org/infinispan/rest/CacheKeyInputStream.java b/server/rest/src/main/java/org/infinispan/rest/CacheKeyInputStream.java index 3d60d0376298..e77d72a5a46e 100644 --- a/server/rest/src/main/java/org/infinispan/rest/CacheKeyInputStream.java +++ b/server/rest/src/main/java/org/infinispan/rest/CacheKeyInputStream.java @@ -43,7 +43,9 @@ public int available() { } private byte[] escape(byte[] content) { - return ("\"" + new String(content, UTF_8) + "\"").getBytes(UTF_8); + String stringified = new String(content, UTF_8); + String escaped = stringified.replaceAll("\"", "\\\\\""); + return ("\"" + escaped + "\"").getBytes(UTF_8); } @Override diff --git a/server/rest/src/main/java/org/infinispan/rest/resources/CacheResourceV2.java b/server/rest/src/main/java/org/infinispan/rest/resources/CacheResourceV2.java index 8a9284f7f870..f9234989ea80 100644 --- a/server/rest/src/main/java/org/infinispan/rest/resources/CacheResourceV2.java +++ b/server/rest/src/main/java/org/infinispan/rest/resources/CacheResourceV2.java @@ -12,6 +12,7 @@ import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM; import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_XML; import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_YAML; +import static org.infinispan.commons.dataconversion.MediaType.MATCH_ALL; import static org.infinispan.commons.dataconversion.MediaType.TEXT_EVENT_STREAM; import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN; import static org.infinispan.rest.framework.Method.DELETE; @@ -361,7 +362,7 @@ private CompletionStage streamKeys(RestRequest request) { int batch = batchParam == null || batchParam.isEmpty() ? STREAM_BATCH_SIZE : Integer.parseInt(batchParam); int limit = limitParam == null || limitParam.isEmpty() ? -1 : Integer.parseInt(limitParam); - Cache cache = invocationHelper.getRestCacheManager().getCache(cacheName, TEXT_PLAIN, TEXT_PLAIN, request); + Cache cache = invocationHelper.getRestCacheManager().getCache(cacheName, TEXT_PLAIN, MATCH_ALL, request); if (cache == null) return notFoundResponseFuture(); diff --git a/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java b/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java index c17e8fff5581..987b2e4da965 100644 --- a/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java +++ b/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java @@ -668,6 +668,53 @@ public void testGetAllKeys() { assertEquals(5, keysLimited.size()); } + @Test + public void testGetAllKeysWithDifferentType() { + String cacheJson = "{ \"distributed-cache\": { \"mode\": \"SYNC\"," + + " \"encoding\": {" + + " \"key\": {\"media-type\": \"application/json\"}," + + " \"value\": {\"media-type\": \"application/xml\"}}}}"; + String value = "\n" + + "\n" + + " \n" + + " \n" + + "\n"; + String cacheName = "xmlCaching"; + RestCacheClient cacheClient = client.cache(cacheName); + + RestEntity jsonEntity = RestEntity.create(APPLICATION_JSON, cacheJson); + CompletionStage r = cacheClient.createWithConfiguration(jsonEntity, VOLATILE); + assertThat(r).isOk(); + + RestResponse response = join(client.cache(cacheName).keys()); + Collection emptyKeys = Json.read(response.getBody()).asJsonList(); + assertEquals(0, emptyKeys.size()); + + // Test key with escape. + putInCache(cacheName, "{\"text\": \"I'm right \\\\\"here\\\\\".\"}", APPLICATION_JSON_TYPE, value, APPLICATION_XML); + response = join(client.cache(cacheName).keys()); + Collection singleSet = Json.read(response.getBody()).asJsonList(); + assertEquals(1, singleSet.size()); + join(client.cache(cacheName).clear()); + + int entries = 10; + for (int i = 0; i < entries; i++) { + putInCache(cacheName, String.format("{\"v\": %d}", i), APPLICATION_JSON_TYPE, value, APPLICATION_XML); + } + response = join(client.cache(cacheName).keys()); + List keys = Json.read(response.getBody()).asJsonList(); + assertEquals(entries, keys.size()); + + response = join(client.cache(cacheName).keys(5)); + List keysLimited = Json.read(response.getBody()).asJsonList(); + assertEquals(5, keysLimited.size()); + } + + private void putInCache(String cacheName, String key, String keyType, String value, MediaType valueType) { + CompletionStage r = client.cache(cacheName).put(key, keyType, RestEntity.create(valueType, value)); + ResponseAssertion.assertThat(r).isOk(); + } + @Test public void testStreamEntries() { RestResponse response = join(client.cache("default").entries());