Added possibility to specify custom HTTP headers to every request

This commit is contained in:
Luca Frosini 2023-02-17 15:10:58 +01:00
parent 848f4e3119
commit 4bbc5bebbb
3 changed files with 34 additions and 5 deletions

View File

@ -34,6 +34,8 @@ abstract class GCatClient {
protected GXHTTPStringRequest gxhttpStringRequest;
protected Map<String, String> 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);

View File

@ -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);
}
}
}

View File

@ -1,2 +1,3 @@
/token.properties
/gcat.properties
/config.ini