From 43babb94d1ba65e7b034f19b70937d340b7463cd Mon Sep 17 00:00:00 2001 From: Michael Wegener Date: Fri, 7 Jul 2017 13:34:04 +0200 Subject: [PATCH 1/2] change GravitonFileEndpoint so it uses the default headers from GravitonApi as well instead of replacing them completely --- .../github/libgraviton/gdk/GravitonApi.java | 7 +++---- .../libgraviton/gdk/GravitonFileEndpoint.java | 19 +++++++++++++------ .../gdk/GravitonFileEndpointTest.java | 6 +++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonApi.java b/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonApi.java index e7546b0..0c9873b 100644 --- a/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonApi.java +++ b/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonApi.java @@ -104,7 +104,7 @@ public String getBaseUrl() { */ public Request.Builder request() { return new Request.Builder(executor) - .setHeaders(getDefaultHeaders()); + .setHeaders(getDefaultHeaders().build()); } public Request.Builder head(String url) { @@ -233,10 +233,9 @@ public ObjectMapper getObjectMapper() { } // TODO make it configurable - protected HeaderBag getDefaultHeaders() { + protected HeaderBag.Builder getDefaultHeaders() { return new HeaderBag.Builder() .set("Content-Type", "application/json") - .set("Accept", "application/json") - .build(); + .set("Accept", "application/json"); } } diff --git a/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonFileEndpoint.java b/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonFileEndpoint.java index 5c006c8..bd9d16a 100644 --- a/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonFileEndpoint.java +++ b/gdk-core/src/main/java/com/github/libgraviton/gdk/GravitonFileEndpoint.java @@ -33,13 +33,12 @@ public Request.Builder getFile(String url) { LOG.debug("Requesting file"); // without the 'Accept' - 'application/json' header, we get the file instead of the metadata - HeaderBag headers = new HeaderBag.Builder() - .set("Content-Type", "application/json") - .build(); + HeaderBag.Builder headers = gravitonApi.getDefaultHeaders() + .unset("Accept"); return gravitonApi.request() .setUrl(url) - .setHeaders(headers) + .setHeaders(headers.build()) .get(); } @@ -68,9 +67,13 @@ public Request.Builder post(byte[] data, GravitonBase resource) throws Serializa Part dataPart = new Part(data, "upload"); Part metadataPart = new Part(gravitonApi.serializeResource(resource), "metadata"); + HeaderBag.Builder headers = gravitonApi.getDefaultHeaders() + .unset("Accept") + .unset("Content-Type"); + return gravitonApi.request() .setUrl(gravitonApi.getEndpointManager().getEndpoint(resource.getClass().getName()).getUrl()) - .setHeaders(new HeaderBag.Builder().build()) + .setHeaders(headers.build()) .post(dataPart, metadataPart); } @@ -78,10 +81,14 @@ public Request.Builder put(byte[] data, GravitonBase resource) throws Serializat Part dataPart = new Part(data, "upload"); Part metadataPart = new Part(gravitonApi.serializeResource(resource), "metadata"); + HeaderBag.Builder headers = gravitonApi.getDefaultHeaders() + .unset("Accept") + .unset("Content-Type"); + return gravitonApi.request() .setUrl(gravitonApi.getEndpointManager().getEndpoint(resource.getClass().getName()).getItemUrl()) .addParam("id", gravitonApi.extractId(resource)) - .setHeaders(new HeaderBag.Builder().build()) + .setHeaders(headers.build()) .put(dataPart, metadataPart); } diff --git a/gdk-core/src/test/java/com/github/libgraviton/gdk/GravitonFileEndpointTest.java b/gdk-core/src/test/java/com/github/libgraviton/gdk/GravitonFileEndpointTest.java index a184e4c..5f5e9ba 100644 --- a/gdk-core/src/test/java/com/github/libgraviton/gdk/GravitonFileEndpointTest.java +++ b/gdk-core/src/test/java/com/github/libgraviton/gdk/GravitonFileEndpointTest.java @@ -41,9 +41,9 @@ public void setupService() throws Exception { when(gravitonApi.getEndpointManager()).thenReturn(endpointManager); when(gravitonApi.extractId(any(GravitonBase.class))).thenCallRealMethod(); when(gravitonApi.serializeResource(any(SimpleClass.class))).thenReturn("{ \"id\":\"111\"}"); - HeaderBag headers = new HeaderBag.Builder() + HeaderBag.Builder headers = new HeaderBag.Builder() .set("whatever", "something") - .build(); + .set("Accept", "almost-everything"); when(gravitonApi.getDefaultHeaders()).thenReturn(headers); resource = new SimpleClass(); @@ -56,7 +56,7 @@ public void setupService() throws Exception { public void testGetFile() throws Exception { Request request = gravitonFileEndpoint.getFile(url).build(); assertEquals(0, request.getHeaders().get("Accept").all().size()); - assertEquals(1, request.getHeaders().get("Content-Type").all().size()); + assertEquals(1, request.getHeaders().get("whatever").all().size()); } @Test From f15ebe3d4a5e93abb9c38ef34c0c34952365f43f Mon Sep 17 00:00:00 2001 From: Michael Wegener Date: Fri, 7 Jul 2017 13:34:34 +0200 Subject: [PATCH 2/2] optimize imports --- .../main/java/com/github/libgraviton/gdk/RequestExecutor.java | 2 -- .../libgraviton/gdk/api/endpoint/GeneratedEndpointManager.java | 1 - .../java/com/github/libgraviton/gdk/api/NoopResponseTest.java | 2 -- 3 files changed, 5 deletions(-) diff --git a/gdk-core/src/main/java/com/github/libgraviton/gdk/RequestExecutor.java b/gdk-core/src/main/java/com/github/libgraviton/gdk/RequestExecutor.java index b407eb1..081f8f4 100644 --- a/gdk-core/src/main/java/com/github/libgraviton/gdk/RequestExecutor.java +++ b/gdk-core/src/main/java/com/github/libgraviton/gdk/RequestExecutor.java @@ -11,8 +11,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Arrays; - /** * This is the base class used for Graviton API calls. * diff --git a/gdk-core/src/main/java/com/github/libgraviton/gdk/api/endpoint/GeneratedEndpointManager.java b/gdk-core/src/main/java/com/github/libgraviton/gdk/api/endpoint/GeneratedEndpointManager.java index 169443e..02b98d5 100644 --- a/gdk-core/src/main/java/com/github/libgraviton/gdk/api/endpoint/GeneratedEndpointManager.java +++ b/gdk-core/src/main/java/com/github/libgraviton/gdk/api/endpoint/GeneratedEndpointManager.java @@ -7,7 +7,6 @@ import org.slf4j.LoggerFactory; import java.io.*; -import java.util.Iterator; import java.util.Map; /** diff --git a/gdk-core/src/test/java/com/github/libgraviton/gdk/api/NoopResponseTest.java b/gdk-core/src/test/java/com/github/libgraviton/gdk/api/NoopResponseTest.java index 8090212..8f0ea90 100644 --- a/gdk-core/src/test/java/com/github/libgraviton/gdk/api/NoopResponseTest.java +++ b/gdk-core/src/test/java/com/github/libgraviton/gdk/api/NoopResponseTest.java @@ -4,8 +4,6 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; public class NoopResponseTest {