Added possibility to specify custom HTTP headers to every request
This commit is contained in:
parent
848f4e3119
commit
4bbc5bebbb
|
@ -34,6 +34,8 @@ abstract class GCatClient {
|
||||||
|
|
||||||
protected GXHTTPStringRequest gxhttpStringRequest;
|
protected GXHTTPStringRequest gxhttpStringRequest;
|
||||||
|
|
||||||
|
protected Map<String, String> headers;
|
||||||
|
|
||||||
public void enforceServiceURL(URL enforcedServiceURL) {
|
public void enforceServiceURL(URL enforcedServiceURL) {
|
||||||
this.serviceURL = enforcedServiceURL;
|
this.serviceURL = enforcedServiceURL;
|
||||||
}
|
}
|
||||||
|
@ -43,13 +45,15 @@ abstract class GCatClient {
|
||||||
this.basePaths = new ArrayList<>();
|
this.basePaths = new ArrayList<>();
|
||||||
this.basePaths.add(basePath);
|
this.basePaths.add(basePath);
|
||||||
this.basePaths.addAll(Arrays.asList(basePaths));
|
this.basePaths.addAll(Arrays.asList(basePaths));
|
||||||
|
this.headers = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GCatClient(String basePath, String... basePaths) throws MalformedURLException {
|
public GCatClient(String basePath, String... basePaths) throws MalformedURLException {
|
||||||
this.serviceURL = GCatClientDiscovery.getServiceURL();
|
this(GCatClientDiscovery.getServiceURL(), basePath, basePaths);
|
||||||
this.basePaths = new ArrayList<>();
|
}
|
||||||
this.basePaths.add(basePath);
|
|
||||||
this.basePaths.addAll(Arrays.asList(basePaths));
|
public void addHeader(String name, String value) {
|
||||||
|
headers.put(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
||||||
|
@ -101,6 +105,9 @@ abstract class GCatClient {
|
||||||
|
|
||||||
protected void initRequest() throws UnsupportedEncodingException {
|
protected void initRequest() throws UnsupportedEncodingException {
|
||||||
gxhttpStringRequest = GXHTTPStringRequest.newRequest(serviceURL.toString());
|
gxhttpStringRequest = GXHTTPStringRequest.newRequest(serviceURL.toString());
|
||||||
|
for(String name : headers.keySet()) {
|
||||||
|
gxhttpStringRequest.header(name, headers.get(name));
|
||||||
|
}
|
||||||
gxhttpStringRequest.from(GCatClient.class.getSimpleName());
|
gxhttpStringRequest.from(GCatClient.class.getSimpleName());
|
||||||
for(String p : basePaths) {
|
for(String p : basePaths) {
|
||||||
gxhttpStringRequest.path(p);
|
gxhttpStringRequest.path(p);
|
||||||
|
|
|
@ -4,9 +4,13 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.JavaType;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
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.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -22,7 +26,7 @@ public class LicenseTest extends ContextTest {
|
||||||
private static final String ID_KEY = "id";
|
private static final String ID_KEY = "id";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void safeTest() throws IOException {
|
public void testList() throws IOException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
|
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
|
||||||
|
@ -36,4 +40,21 @@ public class LicenseTest extends ContextTest {
|
||||||
logger.debug("License : {}", licenseID);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
/token.properties
|
/token.properties
|
||||||
/gcat.properties
|
/gcat.properties
|
||||||
|
/config.ini
|
||||||
|
|
Loading…
Reference in New Issue