The removal of a file persited is resolved using the public link #27952

This commit is contained in:
Luca Frosini 2024-08-30 17:07:06 +02:00
parent b5b9dbd247
commit 7e5286441f
6 changed files with 58 additions and 29 deletions

View File

@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Groups is created with title #27716
- CKANPackageTrash constructor with configuration as paramter made public
- The removal of a file persited is resolved using the public link #27952
## [v1.0.0]

View File

@ -302,6 +302,7 @@ public class CKANResource extends CKAN {
if(urlToCheck.getHost().compareTo(URI_RESOLVER_STORAGE_HUB_HOST) == 0) {
if(urlToCheck.getPath().startsWith(URI_RESOLVER_STORAGE_HUB_PATH)) {
persistedURL = urlToCheck;
logger.trace("The file at {} is a StorageFile. The final URL is {}", url, urlToCheck);
return true;
}
}
@ -331,6 +332,13 @@ public class CKANResource extends CKAN {
return persistedURL;
}
/**
* USe {@link CKANResource#deleteStorageResource(URL)} instead
* @param url
* @param filename
* @param mimetype
*/
@Deprecated
protected void deleteStorageResource(URL url, String filename, String mimetype) {
persistedURL = url;
if(isStorageFile(persistedURL)) {
@ -353,6 +361,17 @@ public class CKANResource extends CKAN {
}
}
protected void deleteStorageResource(URL url) {
persistedURL = url;
if(isStorageFile(persistedURL)) {
try {
storageHubManagement.deleteResourcePersistence(url, itemID);
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
}
protected String create(JsonNode jsonNode) {
try {
ObjectNode objectNode = validate(jsonNode);
@ -497,7 +516,8 @@ public class CKANResource extends CKAN {
getPreviousRepresentation();
URL url = new URL(previousRepresentation.get(URL_KEY).asText());
mimeType = previousRepresentation.get(MIME_TYPE_KEY).asText();
deleteStorageResource(url, resourceID, mimeType);
// deleteStorageResource(url, resourceID, mimeType);
deleteStorageResource(url);
} catch(Exception e) {
logger.error("Unable to delete resource {}",
previousRepresentation != null ? getAsString(previousRepresentation) : "");

View File

@ -77,6 +77,10 @@ public class GcatStorageHubManagement {
}
}
/**
* Use {@link GcatStorageHubManagement#deleteResourcePersistence(URL, String)} instead
*/
@Deprecated
public void deleteResourcePersistence(String itemID, String resourceID, String mimeType) throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
@ -91,6 +95,22 @@ public class GcatStorageHubManagement {
}
}
public void deleteResourcePersistence(URL url, String itemID) throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
secretManager.startSession(secret);
storageHubManagement = new StorageHubManagement();
this.itemId = itemID;
storageHubManagement.setMetadataMatcher(getMetadataMatcher());
storageHubManagement.removePersistedFile(url);
} catch (Exception e) {
logger.error("Error while deleting resource from URL {}, itemID {}", url, itemID, e);
} finally {
secretManager.endSession();
}
}
// protected void internalAddRevisionID(String resourceID, String revisionID) throws Exception {
// try {
// FileContainer fileContainer = null;

View File

@ -41,13 +41,12 @@ public class ContextTest {
public static final String VO;
public static final String VRE;
private static final String ROOT_PRE;
private static final String VO_PREPROD;
protected static final String VRE_GRSF_PRE;
public static final String ROOT_PRE;
public static final String VO_PREPROD;
public static final String VRE_GRSF_PRE;
private static final String ROOT_PROD;
protected static final Properties properties;
public static final String TYPE_PROPERTY_KEY = "type";

View File

@ -18,37 +18,24 @@ public class CKANResourceTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(CKANResourceTest.class);
/*
* PRE
*
* Workspace(luca.frosini) > RESTful Transaction Model.pdf
* https://data1-d.d4science.org/shub/E_YjI4STdKKzRlNjgzMm9jQWxjcmtReDNwbDFYR3lpTHo3SjdtN1RDZ3c2OGk0ZHZhdE5iZElBKzNxUDAyTGFqZw==
* https://goo.gl/HcUWni
*
*
* Workspace(luca.frosini) > RESTful Transaction Model v 1.0.pdf
* https://data1-d.d4science.org/shub/E_aThRa1NpWFJpTGEydEU2bEJhMXNjZy8wK3BxekJKYnpYTy81cUkwZVdicEZ0aGFRZmY4MkRnUC8xWW0zYzVoVg==
* https://goo.gl/J8AwQW
*
*
* Workspace(luca.frosini) > RESTful Transaction Model v 1.1.pdf
* https://data1-d.d4science.org/shub/E_NkhrbVV4VTluT0RKVUtCRldobFZTQU5ySTZneFdpUzJ2UjJBNlZWNDlURDVHamo4WjY5RnlrcHZGTGNkT2prUg==
* https://goo.gl/78ViuR
*
*/
@Ignore
@Test
public void testCopyStorageResource() throws Exception {
// ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
URL url = new URL("https://data.d4science.net/gS9k");
/*
* ContextTest.setContextByName(GCUBE);
* URL url = new URL("https://data.dev.d4science.org/shub/E_QllPeWdoT09Qb0piUkwzdDBQUjhvbWFLT0kyQ3hrOHkzSFo0TXg4bkFvSnlnU1gvQVJxcW40eVBkWVFmQWVvTQ==");
*/
ContextTest.setContextByName(ROOT_PRE);
URL url = new URL("https://data-pre.d4science.org/shub/E_amYzbG9MaitXMW1JelJ3a1BqN3gwV29Kazh5V2wyUTJzQTZ2d1NIRDZlNmxjdVhUa0E2d1BlNmJDT0UzanZLWA==");
String itemID = UUID.randomUUID().toString();
CKANResource ckanResource = new CKANResource(itemID);
ckanResource.resourceID = UUID.randomUUID().toString();
URL finalURL = ckanResource.copyStorageResource(url);
logger.debug("Initial URL is {} - Final URL is {}", url, finalURL);
ckanResource.deleteStorageResource(finalURL, ckanResource.resourceID, ckanResource.mimeType);
logger.debug("Initial URL is {} - Persisted File URL is {}", url, finalURL);
//ckanResource.deleteStorageResource(finalURL, ckanResource.resourceID, ckanResource.mimeType);
ckanResource.deleteStorageResource(finalURL);
}
@Ignore
@ -58,7 +45,7 @@ public class CKANResourceTest extends ContextTest {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put(CKANResource.NAME_KEY, "MyTestTy_rest_upload");
objectNode.put(CKANResource.URL_KEY, "https://data.d4science.org/shub/58a13287-3e91-4afd-bd80-cf4605a0edaa");
objectNode.put("description", "i uploaded this file using the REST API");
objectNode.put("description", " uploaded this file using the REST API");
// objectNode.put(CKANResource.ID_KEY, "ba7ab7e8-c268-4219-98cd-c73470870999");
CKANResource ckanResource = new CKANResource("ba7ab7e8-c268-4219-98cd-c73470870999");

View File

@ -12,6 +12,7 @@
<logger name="org.gcube" level="ERROR" />
<logger name="org.gcube.gcat" level="INFO" />
<logger name="org.gcube.gcat.jsonpath" level="TRACE" />
<logger name="org.gcube.storagehub" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />