diff --git a/pom.xml b/pom.xml index 7c3909e..49b7467 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ io.prismic java-kit jar - 2.0.0 + 2.1.0-SNAPSHOT java-kit The developer kit to access Prismic.io repositories using the Java language. http://maven.apache.org diff --git a/src/main/java/io/prismic/Api.java b/src/main/java/io/prismic/Api.java index bdc8d7b..738db19 100644 --- a/src/main/java/io/prismic/Api.java +++ b/src/main/java/io/prismic/Api.java @@ -8,6 +8,7 @@ import java.net.Proxy; import java.util.*; import java.util.Map.Entry; +import java.util.concurrent.CompletableFuture; /** * Embodies an API endpoint, from which it is possible to make queries. @@ -92,7 +93,7 @@ public static Api get(String endpoint, String accessToken, String defaultReferen JsonNode json = cache.getOrSet( url, 5000L, - () -> HttpClient.fetch(url, logger, null, proxy) + CompletableFuture.supplyAsync(() -> HttpClient.fetch(url, logger, null, proxy)) ); ApiData apiData = ApiData.parse(json); diff --git a/src/main/java/io/prismic/Cache.java b/src/main/java/io/prismic/Cache.java index 3a2bb17..249caf0 100644 --- a/src/main/java/io/prismic/Cache.java +++ b/src/main/java/io/prismic/Cache.java @@ -3,13 +3,34 @@ import com.fasterxml.jackson.databind.JsonNode; import org.apache.commons.collections4.map.LRUMap; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import static java.util.Collections.synchronizedMap; + public interface Cache { void set(String key, Long ttl, JsonNode response); + JsonNode get(String key); - JsonNode getOrSet(String key, Long ttl, Callback f); + + default JsonNode getOrSet(String key, Long ttl, Future callback) { + JsonNode found = this.get(key); + if (found != null) { + return found; + } + try { + JsonNode json = callback.get(); + this.set(key, ttl, json); + return json; + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } // -- + class NoCache implements Cache { @Override @@ -21,39 +42,32 @@ public JsonNode get(String key) { return null; } - @Override - public JsonNode getOrSet(String key, Long ttl, Callback f) { - return f.execute(); - } - } // -- class DefaultCache { - private static final Cache defaultCache = new BuiltInCache(999); + private static final Cache DEFAULT_CACHE = new BuiltInCache(999); - private DefaultCache() {} + private DefaultCache() { + } public static Cache getInstance() { - return defaultCache; + return DEFAULT_CACHE; } } // -- - interface Callback { - JsonNode execute(); - } - class BuiltInCache implements Cache { - private final java.util.Map cache; + private final Map cache; static class Entry { public final Long expiration; public final JsonNode value; + public Entry(Long expiration, JsonNode value) { this.expiration = expiration; this.value = value; @@ -61,7 +75,7 @@ public Entry(Long expiration, JsonNode value) { } public BuiltInCache(int maxDocuments) { - this.cache = java.util.Collections.synchronizedMap(new LRUMap(maxDocuments)); + this.cache = synchronizedMap(new LRUMap(maxDocuments)); } @Override @@ -80,21 +94,9 @@ public void set(String key, Long ttl, JsonNode response) { this.cache.put(key, new Entry(expiration, response)); } - @Override - public JsonNode getOrSet(String key, Long ttl, Callback f) { - JsonNode found = this.get(key); - if(found == null) { - JsonNode json = f.execute(); - this.set(key, ttl, json); - return json; - } else { - return found; - } - } - private Boolean isExpired(String key) { Entry entry = this.cache.get(key); - return entry != null && entry.expiration !=0 && entry.expiration < System.currentTimeMillis(); + return entry != null && entry.expiration != 0 && entry.expiration < System.currentTimeMillis(); } } diff --git a/src/main/java/io/prismic/Callback.java b/src/main/java/io/prismic/Callback.java deleted file mode 100644 index 896f5a7..0000000 --- a/src/main/java/io/prismic/Callback.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.prismic; - -public interface Callback { - void execute(); -} diff --git a/src/test/java/io/prismic/AppTest.java b/src/test/java/io/prismic/AppTest.java index 3221300..f88dcdb 100644 --- a/src/test/java/io/prismic/AppTest.java +++ b/src/test/java/io/prismic/AppTest.java @@ -4,6 +4,7 @@ import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -51,9 +52,17 @@ public void apiIsInitialized() { /** * Make sure a call to a private repository without a token returns the expected error */ - @Test(expected = Api.Error.class) + @Test public void invalidToken() { - Api api = Api.get("https://private-test.prismic.io/api"); + Throwable thrown = null; + try { + Api.get("https://private-test.prismic.io/api"); + } catch (Throwable t) { + thrown = t; + } + Throwable rootCause = ExceptionUtils.getRootCause(thrown); + Assert.assertEquals(Api.Error.class, rootCause.getClass()); + Assert.assertEquals("Invalid access token", rootCause.getMessage()); } /**