Added the possibility for a client to add additional HTTP headers
This commit is contained in:
parent
82fa4ec559
commit
c529b03d97
|
@ -2,7 +2,9 @@ package org.gcube.informationsystem.resourceregistry.contexts;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
@ -32,8 +34,13 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientImpl.class);
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientImpl.class);
|
||||||
|
|
||||||
|
private static final String ACCEPT_HTTP_HEADER_KEY = "Accept";
|
||||||
|
private static final String CONTENT_TYPE_HTTP_HEADER_KEY = "Content-Type";
|
||||||
|
|
||||||
protected final String address;
|
protected final String address;
|
||||||
|
|
||||||
|
protected Map<String, String> headers;
|
||||||
|
|
||||||
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -43,8 +50,22 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public void addHeader(String name, String value) {
|
||||||
|
headers.put(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected GXHTTPStringRequest getGXHTTPStringRequest() {
|
||||||
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
||||||
|
gxHTTPStringRequest.from(this.getClass().getSimpleName());
|
||||||
|
for(String name : headers.keySet()) {
|
||||||
|
gxHTTPStringRequest.header(name, headers.get(name));
|
||||||
|
}
|
||||||
|
return gxHTTPStringRequest;
|
||||||
|
}
|
||||||
|
|
||||||
public ResourceRegistryContextClientImpl(String address) {
|
public ResourceRegistryContextClientImpl(String address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
|
this.headers = new HashMap<>();
|
||||||
ContextCache contextCache = ContextCache.getInstance();
|
ContextCache contextCache = ContextCache.getInstance();
|
||||||
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
||||||
}
|
}
|
||||||
|
@ -62,9 +83,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
protected List<Context> getAllContextFromServer() throws ResourceRegistryException {
|
protected List<Context> getAllContextFromServer() throws ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
logger.trace("Going to read {} with UUID {}", Context.NAME);
|
logger.trace("Going to read {} with UUID {}", Context.NAME);
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
|
|
||||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
||||||
|
@ -101,10 +121,9 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
logger.trace("Going to create {}", contextString);
|
logger.trace("Going to create {}", contextString);
|
||||||
|
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
gxHTTPStringRequest.path(uuid.toString());
|
gxHTTPStringRequest.path(uuid.toString());
|
||||||
|
|
||||||
|
@ -156,9 +175,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
public boolean existFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
public boolean existFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
gxHTTPStringRequest.path(uuid);
|
gxHTTPStringRequest.path(uuid);
|
||||||
|
|
||||||
|
@ -262,9 +280,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
public String readFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
public String readFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
gxHTTPStringRequest.path(uuid);
|
gxHTTPStringRequest.path(uuid);
|
||||||
|
|
||||||
|
@ -289,10 +306,9 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
UUID uuid = context.getHeader().getUUID();
|
UUID uuid = context.getHeader().getUUID();
|
||||||
|
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
gxHTTPStringRequest.path(uuid.toString());
|
gxHTTPStringRequest.path(uuid.toString());
|
||||||
|
|
||||||
|
@ -355,9 +371,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
logger.trace("Going to delete {} with UUID {}", Context.NAME, uuid);
|
logger.trace("Going to delete {} with UUID {}", Context.NAME, uuid);
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||||
gxHTTPStringRequest.path(uuid);
|
gxHTTPStringRequest.path(uuid);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue