From 6e64c40b4b8cb266fd362bf9a93367c5c9966106 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 1 Aug 2024 15:14:07 +0200 Subject: [PATCH] Added Content-Location and fixed Location HTTP Headers --- .../gcube/grsf/publisher/ckan/record/Record.java | 15 ++++++++++++--- .../org/gcube/grsf/publisher/rest/BaseREST.java | 2 +- .../gcube/grsf/publisher/rest/BaseRESTAPIs.java | 6 +++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java b/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java index 8d194fc..a3358fa 100644 --- a/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java +++ b/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java @@ -86,6 +86,9 @@ public abstract class Record extends CKANPackage { protected String grsfUUID; + protected URIResolver uriResolver; + protected String recordURL; + protected ObjectMapper objectMapper; protected JsonNode jsonNode; @@ -97,6 +100,7 @@ public abstract class Record extends CKANPackage { super(GRSFCatalogueConfigurationFactory.getInstance()); this.objectMapper = new ObjectMapper(); this.patch = false; + this.uriResolver = URIResolver.getInstance(); } public abstract String getType(); @@ -246,13 +250,18 @@ public abstract class Record extends CKANPackage { return fileContainer; } + public String getRecordURL() { + if(recordURL==null) { + recordURL = uriResolver.getCatalogueItemURL(grsfUUID); + } + return recordURL; + } + protected Map getMapFromSourceJson(JsonNode jsonNode) throws Exception { Map map = objectMapper.convertValue(jsonNode, new TypeReference>(){}); grsfUUID = map.get(Record.GRSF_UUID_PROPERTY).toString(); - URIResolver uriResolver = URIResolver.getInstance(); - String recordURL = uriResolver.getCatalogueItemURL(grsfUUID); - map.put(RECORD_URL_TEMPLATE_PROPERTY_KEY, recordURL); + map.put(RECORD_URL_TEMPLATE_PROPERTY_KEY, getRecordURL()); map.put(INCLUDE_SENSITIVE_TEMPLATE_PROPERTY_KEY, ((GRSFCatalogueConfiguration) configuration).isIncludeSensitive()); diff --git a/src/main/java/org/gcube/grsf/publisher/rest/BaseREST.java b/src/main/java/org/gcube/grsf/publisher/rest/BaseREST.java index 1643b0c..a3a9c45 100644 --- a/src/main/java/org/gcube/grsf/publisher/rest/BaseREST.java +++ b/src/main/java/org/gcube/grsf/publisher/rest/BaseREST.java @@ -38,7 +38,7 @@ public class BaseREST { protected ResponseBuilder addLocation(ResponseBuilder responseBuilder, String id) { return responseBuilder.header(LOCATION_HEADER, - String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id)); + String.format("%s%s", uriInfo.getAbsolutePath().toString(), id)); } protected String createCountJson(int count) { diff --git a/src/main/java/org/gcube/grsf/publisher/rest/BaseRESTAPIs.java b/src/main/java/org/gcube/grsf/publisher/rest/BaseRESTAPIs.java index 369b575..8f8a337 100644 --- a/src/main/java/org/gcube/grsf/publisher/rest/BaseRESTAPIs.java +++ b/src/main/java/org/gcube/grsf/publisher/rest/BaseRESTAPIs.java @@ -35,7 +35,7 @@ public class BaseRESTAPIs extends BaseREST { @Context protected UriInfo uriInfo; - protected static final String LOCATION_HEADER = "Location"; + protected static final String CONTENT_LOCATION_HEADER = "Content-Location"; /* Used for accounting */ protected final String COLLECTION_PARAMETER; @@ -96,6 +96,9 @@ public class BaseRESTAPIs extends BaseREST { } + protected ResponseBuilder addContentLocation(ResponseBuilder responseBuilder, String url) { + return responseBuilder.header(CONTENT_LOCATION_HEADER, url); + } public Response create(String json) { setCalledMethod("POST /" + COLLECTION_PARAMETER); @@ -104,6 +107,7 @@ public class BaseRESTAPIs extends BaseREST { ResponseBuilder responseBuilder = Response.status(Status.CREATED).entity(ret); responseBuilder = addLocation(responseBuilder, record.getName()); + responseBuilder = addContentLocation(responseBuilder, record.getRecordURL()); return responseBuilder.type(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8).build(); }