From 4bbc5bebbbc93dbe477f6e764a83b868b8ef0c83 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 17 Feb 2023 15:10:58 +0100 Subject: [PATCH] Added possibility to specify custom HTTP headers to every request --- .../org/gcube/gcat/client/GCatClient.java | 15 ++++++++---- .../org/gcube/gcat/client/LicenseTest.java | 23 ++++++++++++++++++- src/test/resources/.gitignore | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gcube/gcat/client/GCatClient.java b/src/main/java/org/gcube/gcat/client/GCatClient.java index 9256045..7b6c44f 100644 --- a/src/main/java/org/gcube/gcat/client/GCatClient.java +++ b/src/main/java/org/gcube/gcat/client/GCatClient.java @@ -34,6 +34,8 @@ abstract class GCatClient { protected GXHTTPStringRequest gxhttpStringRequest; + protected Map headers; + public void enforceServiceURL(URL enforcedServiceURL) { this.serviceURL = enforcedServiceURL; } @@ -43,13 +45,15 @@ abstract class GCatClient { this.basePaths = new ArrayList<>(); this.basePaths.add(basePath); this.basePaths.addAll(Arrays.asList(basePaths)); + this.headers = new HashMap<>(); } public GCatClient(String basePath, String... basePaths) throws MalformedURLException { - this.serviceURL = GCatClientDiscovery.getServiceURL(); - this.basePaths = new ArrayList<>(); - this.basePaths.add(basePath); - this.basePaths.addAll(Arrays.asList(basePaths)); + this(GCatClientDiscovery.getServiceURL(), basePath, basePaths); + } + + public void addHeader(String name, String value) { + headers.put(name, value); } protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException { @@ -101,6 +105,9 @@ abstract class GCatClient { protected void initRequest() throws UnsupportedEncodingException { gxhttpStringRequest = GXHTTPStringRequest.newRequest(serviceURL.toString()); + for(String name : headers.keySet()) { + gxhttpStringRequest.header(name, headers.get(name)); + } gxhttpStringRequest.from(GCatClient.class.getSimpleName()); for(String p : basePaths) { gxhttpStringRequest.path(p); diff --git a/src/test/java/org/gcube/gcat/client/LicenseTest.java b/src/test/java/org/gcube/gcat/client/LicenseTest.java index daf8762..b9e397e 100644 --- a/src/test/java/org/gcube/gcat/client/LicenseTest.java +++ b/src/test/java/org/gcube/gcat/client/LicenseTest.java @@ -4,9 +4,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import javax.ws.rs.core.HttpHeaders; + import org.gcube.com.fasterxml.jackson.databind.JavaType; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; +import org.gcube.gcat.api.GCatConstants; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -22,7 +26,7 @@ public class LicenseTest extends ContextTest { private static final String ID_KEY = "id"; @Test - public void safeTest() throws IOException { + public void testList() throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class); @@ -36,4 +40,21 @@ public class LicenseTest extends ContextTest { logger.debug("License : {}", licenseID); } } + + @Test + public void testListAsJSONAPI() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + + License license = new License(); + license.addHeader(HttpHeaders.ACCEPT, GCatConstants.APPLICATION_JSON_API); + String licencesStrings = license.list(); + logger.debug("Got licenses {}", licencesStrings); + JsonNode licencesWrapper = mapper.readTree(licencesStrings); + ArrayNode licences = (ArrayNode) licencesWrapper.get(GCatConstants.JSON_API_DATA_FIELD_NAME); + Assert.assertTrue(licences.size()>0); + for(JsonNode licenseJsonNode : licences) { + String licenseID = licenseJsonNode.get(ID_KEY).asText(); + logger.debug("License : {}", licenseID); + } + } } diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index eb42d60..c0383f3 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -1,2 +1,3 @@ /token.properties /gcat.properties +/config.ini