137 lines
5.1 KiB
Java
137 lines
5.1 KiB
Java
package org.gcube.gcat.workspace;
|
|
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
|
import org.gcube.common.storagehub.model.Metadata;
|
|
import org.gcube.gcat.utils.Constants;
|
|
import org.gcube.storagehub.ApplicationMode;
|
|
import org.gcube.storagehub.StorageHubManagement;
|
|
import org.glassfish.jersey.media.multipart.ContentDisposition;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class CatalogueStorageHubManagement {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(CatalogueStorageHubManagement.class);
|
|
|
|
protected StorageHubManagement storageHubManagement;
|
|
|
|
protected String originalFilename;
|
|
protected String mimeType;
|
|
|
|
public String getOriginalFilename() {
|
|
return originalFilename;
|
|
}
|
|
|
|
public String getMimeType() {
|
|
return mimeType;
|
|
}
|
|
|
|
public CatalogueStorageHubManagement() {
|
|
this.storageHubManagement = new StorageHubManagement();
|
|
}
|
|
|
|
protected String getOriginalFileName(HttpURLConnection httpURLConnection) throws Exception {
|
|
String contentDisposition = httpURLConnection.getHeaderField("Content-Disposition");
|
|
contentDisposition = contentDisposition.replaceAll("= ", "=").replaceAll(" =", "=");
|
|
ContentDisposition formDataContentDisposition = new ContentDisposition(contentDisposition);
|
|
return formDataContentDisposition.getFileName();
|
|
}
|
|
|
|
public URL ensureResourcePersistence(URL persistedURL, String itemID, String resourceID) throws Exception {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
try {
|
|
applicationMode.start();
|
|
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
|
|
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
|
gxhttpStringRequest.isExternalCall(true);
|
|
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
|
|
mimeType = httpURLConnection.getContentType().split(";")[0];
|
|
originalFilename = getOriginalFileName(httpURLConnection);
|
|
CatalogueMetadata catalogueMetadata = new CatalogueMetadata(itemID);
|
|
storageHubManagement.setCheckMetadata(catalogueMetadata);
|
|
Metadata metadata = catalogueMetadata.getMetadata(persistedURL, originalFilename, resourceID);
|
|
persistedURL = storageHubManagement.persistFile(httpURLConnection.getInputStream(), resourceID, mimeType,
|
|
metadata);
|
|
mimeType = storageHubManagement.getMimeType();
|
|
return persistedURL;
|
|
} catch (Exception e) {
|
|
logger.error("Error while trying to persists the resource", e);
|
|
throw e;
|
|
} finally {
|
|
applicationMode.end();
|
|
}
|
|
}
|
|
|
|
public void deleteResourcePersistence(String itemID, String resourceID, String mimeType) throws Exception {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
try {
|
|
applicationMode.start();
|
|
storageHubManagement = new StorageHubManagement();
|
|
CatalogueMetadata catalogueMetadata = new CatalogueMetadata(itemID);
|
|
storageHubManagement.setCheckMetadata(catalogueMetadata);
|
|
storageHubManagement.removePersistedFile(resourceID, mimeType);
|
|
} finally {
|
|
applicationMode.end();
|
|
}
|
|
}
|
|
|
|
protected void internalAddRevisionID(String resourceID, String revisionID) throws Exception {
|
|
FileContainer fileContainer = storageHubManagement.getPersistedFile();
|
|
if(fileContainer != null) {
|
|
Metadata metadata = fileContainer.get().getMetadata();
|
|
Map<String,Object> map = metadata.getMap();
|
|
map.put(CatalogueMetadata.CATALOGUE_RESOURCE_ID, resourceID);
|
|
map.put(CatalogueMetadata.CATALOGUE_RESOURCE_REVISION_ID, revisionID);
|
|
metadata.setMap(map);
|
|
fileContainer.setMetadata(metadata);
|
|
} else {
|
|
logger.warn(
|
|
"Unable to set revision id {} to the file of resource with id {} because the file was NOT found on storage-hub. This could be related to an item not created via gCat. Skipping it.",
|
|
revisionID, resourceID);
|
|
}
|
|
}
|
|
|
|
public void renameFile(String resourceID, String revisionID) throws Exception {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
try {
|
|
applicationMode.start();
|
|
FileContainer createdfile = storageHubManagement.getPersistedFile();
|
|
createdfile.rename(resourceID);
|
|
internalAddRevisionID(resourceID, revisionID);
|
|
} finally {
|
|
applicationMode.end();
|
|
}
|
|
|
|
}
|
|
|
|
public void addRevisionID(String resourceID, String revisionID) throws Exception {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
try {
|
|
applicationMode.start();
|
|
internalAddRevisionID(resourceID, revisionID);
|
|
} finally {
|
|
applicationMode.end();
|
|
}
|
|
}
|
|
|
|
public FileContainer retrievePersistedFile(String id, String mimeType) throws Exception {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
try {
|
|
applicationMode.start();
|
|
return storageHubManagement.getPersistedFile(id, mimeType);
|
|
} finally {
|
|
applicationMode.end();
|
|
}
|
|
}
|
|
|
|
|
|
public FileContainer getPersistedFile() {
|
|
return storageHubManagement.getPersistedFile();
|
|
}
|
|
}
|