2018-12-04 12:06:22 +01:00
|
|
|
package org.gcube.gcat.persistence.ckan;
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2018-12-04 12:06:22 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.ws.rs.BadRequestException;
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
import javax.ws.rs.HEAD;
|
|
|
|
import javax.ws.rs.InternalServerErrorException;
|
|
|
|
import javax.ws.rs.NotAllowedException;
|
|
|
|
import javax.ws.rs.OPTIONS;
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
import org.apache.commons.io.FilenameUtils;
|
2019-05-20 17:23:49 +02:00
|
|
|
import org.apache.tika.mime.MimeType;
|
|
|
|
import org.apache.tika.mime.MimeTypes;
|
2019-01-10 12:29:47 +01:00
|
|
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
|
|
|
import org.gcube.gcat.utils.Constants;
|
2018-12-04 12:06:22 +01:00
|
|
|
import org.gcube.gcat.utils.ContextUtility;
|
|
|
|
import org.gcube.gcat.utils.HTTPCall;
|
2019-01-10 12:29:47 +01:00
|
|
|
import org.gcube.gcat.workspace.CatalogueStorageHubManagement;
|
2018-12-04 12:06:22 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class CKANResource extends CKAN {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(CKANResource.class);
|
|
|
|
|
|
|
|
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
|
|
|
|
public static final String RESOURCE_CREATE = CKAN.CKAN_API_PATH + "resource_create";
|
|
|
|
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.resource_show
|
|
|
|
public static final String RESOURCE_SHOW = CKAN.CKAN_API_PATH + "resource_show";
|
|
|
|
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.resource_update
|
|
|
|
public static final String RESOURCE_UPDATE = CKAN.CKAN_API_PATH + "resource_update";
|
|
|
|
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.patch.resource_patch
|
|
|
|
public static final String RESOURCE_PATCH = CKAN.CKAN_API_PATH + "resource_patch";
|
|
|
|
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.resource_delete
|
|
|
|
public static final String RESOURCE_DELETE = CKAN.CKAN_API_PATH + "resource_delete";
|
|
|
|
|
|
|
|
protected static final String URL_KEY = "url";
|
|
|
|
|
|
|
|
private static final String RESOURCES_KEY = "resources";
|
|
|
|
private static final String PACKAGE_ID_KEY = "package_id";
|
2019-05-20 17:23:49 +02:00
|
|
|
private static final String FORMAT_KEY = "format";
|
2018-12-04 12:06:22 +01:00
|
|
|
private static final String MIME_TYPE_KEY = "mimetype";
|
|
|
|
private static final String REVISION_ID_KEY = "revision_id";
|
|
|
|
|
|
|
|
private static final String TEMP = "TEMP_";
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
public final static String RESOURCE_NAME_REGEX = "^[\\s\\S]*$";
|
2018-12-04 12:06:22 +01:00
|
|
|
|
2019-05-20 17:23:49 +02:00
|
|
|
public static final MimeTypes ALL_MIME_TYPES;
|
|
|
|
|
2019-02-08 11:10:59 +01:00
|
|
|
/* TODO Remove this code ASAP. It requires a function from Storage HUB */
|
|
|
|
private static final String URI_RESOLVER_STORAGE_HUB_HOST_PROD = "data.d4science.org";
|
2019-12-19 14:13:44 +01:00
|
|
|
private static final String URI_RESOLVER_STORAGE_HUB_HOST_PRE = "data-pre.d4science.org";
|
2019-12-19 12:41:38 +01:00
|
|
|
private static final String URI_RESOLVER_STORAGE_HUB_HOST_DEV = "data-d.d4science.org";
|
2019-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
public static final String URI_RESOLVER_STORAGE_HUB_HOST;
|
|
|
|
public static final String URI_RESOLVER_STORAGE_HUB_PATH = "/shub/";
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
static {
|
|
|
|
String context = ContextUtility.getCurrentContext();
|
|
|
|
if(context.startsWith("/gcube")) {
|
|
|
|
URI_RESOLVER_STORAGE_HUB_HOST = URI_RESOLVER_STORAGE_HUB_HOST_DEV;
|
2019-12-19 14:13:44 +01:00
|
|
|
} else if(context.startsWith("/pred4s")){
|
|
|
|
URI_RESOLVER_STORAGE_HUB_HOST = URI_RESOLVER_STORAGE_HUB_HOST_PRE;
|
2018-12-04 12:06:22 +01:00
|
|
|
} else {
|
|
|
|
URI_RESOLVER_STORAGE_HUB_HOST = URI_RESOLVER_STORAGE_HUB_HOST_PROD;
|
|
|
|
}
|
2019-05-20 17:23:49 +02:00
|
|
|
// If you might be dealing with custom mimetypes too, then Tika supports those, and change line one to be:
|
|
|
|
// TikaConfig config = TikaConfig.getDefaultConfig();
|
|
|
|
// MimeTypes ALL_MIME_TYPES = config.getMimeRepository();
|
|
|
|
ALL_MIME_TYPES = MimeTypes.getDefaultMimeTypes();
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
/* TODO END Code to be Removed */
|
|
|
|
|
|
|
|
protected String itemID;
|
|
|
|
|
|
|
|
public String getItemID() {
|
|
|
|
return itemID;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String resourceID;
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
protected boolean persisted;
|
2018-12-04 12:06:22 +01:00
|
|
|
protected URL persistedURL;
|
|
|
|
|
|
|
|
protected String mimeType;
|
2019-05-20 17:23:49 +02:00
|
|
|
protected String originalFileExtension;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
|
|
|
protected JsonNode previousRepresentation;
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
protected CatalogueStorageHubManagement storageHubManagement;
|
2019-02-08 11:10:59 +01:00
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
public URL getPersistedURL() {
|
|
|
|
return persistedURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String extractResourceID(JsonNode jsonNode) {
|
|
|
|
String resourceID = null;
|
|
|
|
if(jsonNode.has(ID_KEY)) {
|
|
|
|
resourceID = jsonNode.get(ID_KEY).asText();
|
|
|
|
}
|
|
|
|
return resourceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getResourceID() {
|
2019-02-08 11:10:59 +01:00
|
|
|
if(resourceID == null && previousRepresentation != null) {
|
2018-12-04 12:06:22 +01:00
|
|
|
resourceID = CKANResource.extractResourceID(previousRepresentation);
|
|
|
|
}
|
|
|
|
return resourceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setResourceID(String resourceID) {
|
|
|
|
this.resourceID = resourceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPreviousRepresentation(JsonNode jsonNode) {
|
2019-01-10 12:29:47 +01:00
|
|
|
validate(jsonNode);
|
2018-12-04 12:06:22 +01:00
|
|
|
previousRepresentation = jsonNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonNode getPreviousRepresentation() {
|
2019-02-08 11:10:59 +01:00
|
|
|
if(previousRepresentation == null && resourceID != null) {
|
2019-01-29 17:46:22 +01:00
|
|
|
sendGetRequest(READ, getMapWithID(resourceID));
|
2018-12-04 12:06:22 +01:00
|
|
|
validate(result);
|
|
|
|
previousRepresentation = result;
|
|
|
|
}
|
|
|
|
return previousRepresentation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CKANResource(String itemID) {
|
|
|
|
super();
|
2019-01-10 12:29:47 +01:00
|
|
|
this.nameRegex = RESOURCE_NAME_REGEX;
|
2018-12-04 12:06:22 +01:00
|
|
|
this.itemID = itemID;
|
|
|
|
CREATE = RESOURCE_CREATE;
|
|
|
|
READ = RESOURCE_SHOW;
|
|
|
|
UPDATE = RESOURCE_UPDATE;
|
|
|
|
PATCH = RESOURCE_PATCH;
|
|
|
|
DELETE = RESOURCE_DELETE;
|
|
|
|
PURGE = null;
|
2019-01-10 12:29:47 +01:00
|
|
|
persisted = false;
|
2018-12-04 12:06:22 +01:00
|
|
|
previousRepresentation = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-10 12:29:47 +01:00
|
|
|
public String list(int limit, int offeset) {
|
|
|
|
return list();
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
public String list() {
|
|
|
|
CKANPackage ckanPackage = new CKANPackage();
|
|
|
|
ckanPackage.setName(itemID);
|
|
|
|
String itemJson = ckanPackage.read();
|
|
|
|
JsonNode item = getAsJsonNode(itemJson);
|
|
|
|
JsonNode resources = item.get(RESOURCES_KEY);
|
|
|
|
return getAsString(resources);
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:23:49 +02:00
|
|
|
protected String getFormat() {
|
|
|
|
String format = null;
|
2019-09-16 14:48:18 +02:00
|
|
|
if(originalFileExtension != null) {
|
2019-05-20 17:23:49 +02:00
|
|
|
format = originalFileExtension;
|
2019-09-16 14:48:18 +02:00
|
|
|
} else {
|
2019-05-20 17:23:49 +02:00
|
|
|
try {
|
|
|
|
MimeType mimeTypeClzInstance = ALL_MIME_TYPES.forName(mimeType);
|
|
|
|
format = mimeTypeClzInstance.getExtension();
|
|
|
|
// List<String> extensions = mimeTypeClzInstance.getExtensions();
|
2019-09-16 14:48:18 +02:00
|
|
|
if(format == null || format.compareTo("") == 0) {
|
2019-05-20 17:23:49 +02:00
|
|
|
format = mimeType.split("/")[1].split(";")[0];
|
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
|
|
|
try {
|
|
|
|
format = mimeType.split("/")[1].split(";")[0];
|
2019-09-16 14:48:18 +02:00
|
|
|
} catch(Exception ex) {
|
2019-05-20 17:23:49 +02:00
|
|
|
format = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 14:48:18 +02:00
|
|
|
if(format != null && format.startsWith(".")) {
|
2019-05-20 17:23:49 +02:00
|
|
|
format = format.substring(1);
|
|
|
|
}
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
protected ObjectNode persistStorageFile(ObjectNode objectNode) {
|
|
|
|
|
|
|
|
if(objectNode.has(URL_KEY)) {
|
|
|
|
String urlString = objectNode.get(URL_KEY).asText();
|
|
|
|
|
|
|
|
URL url;
|
|
|
|
try {
|
|
|
|
url = new URL(urlString);
|
|
|
|
} catch(MalformedURLException e) {
|
|
|
|
throw new BadRequestException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
url = copyStorageResource(url);
|
|
|
|
|
2019-02-08 11:10:59 +01:00
|
|
|
if(name != null) {
|
2019-01-10 12:29:47 +01:00
|
|
|
objectNode.put(NAME_KEY, name);
|
|
|
|
}
|
2019-09-16 14:48:18 +02:00
|
|
|
|
2019-02-08 11:10:59 +01:00
|
|
|
if(mimeType != null) {
|
2018-12-04 12:06:22 +01:00
|
|
|
objectNode.put(MIME_TYPE_KEY, mimeType);
|
2019-05-20 17:23:49 +02:00
|
|
|
|
|
|
|
if(!objectNode.has(FORMAT_KEY)) {
|
|
|
|
String format = getFormat();
|
2019-09-16 14:48:18 +02:00
|
|
|
if(format != null) {
|
2019-05-20 17:23:49 +02:00
|
|
|
objectNode.put(FORMAT_KEY, format);
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
objectNode.put(URL_KEY, url.toString());
|
|
|
|
return objectNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
String error = String.format("The content must contains the %s property", URL_KEY);
|
|
|
|
throw new BadRequestException(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ObjectNode validate(String json) throws MalformedURLException {
|
|
|
|
JsonNode jsonNode = getAsJsonNode(json);
|
|
|
|
return validate(jsonNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ObjectNode validate(JsonNode jsonNode) {
|
|
|
|
|
|
|
|
ObjectNode objectNode = (ObjectNode) jsonNode;
|
|
|
|
|
|
|
|
if(objectNode.has(PACKAGE_ID_KEY)) {
|
|
|
|
String packageId = objectNode.get(PACKAGE_ID_KEY).asText();
|
|
|
|
if(packageId.compareTo(itemID) != 0) {
|
|
|
|
String error = String.format(
|
|
|
|
"Item ID %s does not match %s which is the value of %s contained in the representation.",
|
|
|
|
itemID, packageId, PACKAGE_ID_KEY);
|
|
|
|
throw new BadRequestException(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
objectNode.put(PACKAGE_ID_KEY, itemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(objectNode.has(ID_KEY)) {
|
|
|
|
String gotId = objectNode.get(ID_KEY).asText();
|
|
|
|
if(resourceID == null) {
|
|
|
|
resourceID = gotId;
|
|
|
|
} else {
|
|
|
|
if(resourceID.compareTo(gotId) != 0) {
|
|
|
|
String error = String.format(
|
|
|
|
"Resource ID %s does not match %s which is the value of %s contained in the representation.",
|
|
|
|
resourceID, gotId, ID_KEY);
|
|
|
|
throw new BadRequestException(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resourceID = TEMP + UUID.randomUUID().toString();
|
|
|
|
logger.trace(
|
|
|
|
"The id of the resource with name {} for package {} has not been provided. It has been generated : {}",
|
|
|
|
name, itemID, resourceID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return objectNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected URL getFinalURL(String url) {
|
|
|
|
try {
|
|
|
|
URL urlURL = new URL(url);
|
2019-01-10 12:29:47 +01:00
|
|
|
return CKANResource.getFinalURL(urlURL);
|
2018-12-04 12:06:22 +01:00
|
|
|
} catch(MalformedURLException e) {
|
|
|
|
throw new BadRequestException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
public static URL getFinalURL(URL url) {
|
2018-12-04 12:06:22 +01:00
|
|
|
HTTPCall httpCall = new HTTPCall(url.toString());
|
|
|
|
httpCall.setgCubeTargetService(false);
|
|
|
|
URL finalURL = httpCall.getFinalURL(url);
|
|
|
|
return finalURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isStorageFile(URL url) {
|
2019-07-22 11:54:05 +02:00
|
|
|
URL urlToCheck = url;
|
|
|
|
try {
|
|
|
|
urlToCheck = getFinalURL(url);
|
2019-09-16 14:48:18 +02:00
|
|
|
} catch(Exception e) {
|
2019-12-19 14:13:44 +01:00
|
|
|
// TODO Evaluate if we want to validate the URL. If the URL does not exists the service
|
2019-07-22 11:54:05 +02:00
|
|
|
// could decide refuse the Resource Creation
|
|
|
|
}
|
2019-02-08 11:10:59 +01:00
|
|
|
if(urlToCheck.getHost().compareTo(URI_RESOLVER_STORAGE_HUB_HOST) == 0) {
|
|
|
|
if(urlToCheck.getPath().startsWith(URI_RESOLVER_STORAGE_HUB_PATH)) {
|
|
|
|
persistedURL = urlToCheck;
|
2018-12-04 12:06:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the URl is a workspace URL so that is has to copy the resource to guarantee
|
|
|
|
* the resource remain persistent
|
|
|
|
* @param url the URL to check
|
|
|
|
* @return the public URL of the copied resource if any. It return the original URL otherwise
|
|
|
|
*/
|
|
|
|
protected URL copyStorageResource(URL url) {
|
2019-02-26 14:58:06 +01:00
|
|
|
persistedURL = url;
|
|
|
|
if(isStorageFile(persistedURL)) {
|
2019-01-10 12:29:47 +01:00
|
|
|
storageHubManagement = new CatalogueStorageHubManagement();
|
|
|
|
try {
|
|
|
|
persistedURL = storageHubManagement.ensureResourcePersistence(persistedURL, itemID, resourceID);
|
2019-05-20 17:23:49 +02:00
|
|
|
String originalFilename = storageHubManagement.getOriginalFilename();
|
|
|
|
name = FilenameUtils.removeExtension(originalFilename);
|
|
|
|
originalFileExtension = FilenameUtils.getExtension(originalFilename);
|
2019-01-10 12:29:47 +01:00
|
|
|
mimeType = storageHubManagement.getMimeType();
|
|
|
|
persisted = true;
|
2019-02-08 11:10:59 +01:00
|
|
|
} catch(Exception e) {
|
2019-01-10 12:29:47 +01:00
|
|
|
throw new InternalServerErrorException(e);
|
|
|
|
}
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
return persistedURL;
|
|
|
|
}
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
protected void deleteStorageResource(URL url, String resourceID, String mimetype) {
|
2019-02-26 14:58:06 +01:00
|
|
|
persistedURL = url;
|
|
|
|
if(isStorageFile(persistedURL)) {
|
2019-01-10 12:29:47 +01:00
|
|
|
try {
|
2019-02-08 11:10:59 +01:00
|
|
|
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
|
2019-01-10 12:29:47 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxhttpStringRequest.from(Constants.CATALOGUE_NAME).head();
|
|
|
|
String storageHubContentType = httpURLConnection.getContentType().split(";")[0];
|
2019-02-08 11:10:59 +01:00
|
|
|
if(mimetype.compareTo(storageHubContentType) != 0) {
|
2019-01-10 12:29:47 +01:00
|
|
|
mimetype = storageHubContentType;
|
|
|
|
// Using storage hub mimetype
|
|
|
|
}
|
2019-02-08 11:10:59 +01:00
|
|
|
} catch(Exception e) {
|
2019-01-10 12:29:47 +01:00
|
|
|
// using provided mimetype
|
|
|
|
}
|
|
|
|
storageHubManagement = new CatalogueStorageHubManagement();
|
|
|
|
try {
|
|
|
|
storageHubManagement.deleteResourcePersistence(itemID, resourceID, mimetype);
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new InternalServerErrorException(e);
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String create(JsonNode jsonNode) {
|
|
|
|
try {
|
|
|
|
ObjectNode objectNode = validate(jsonNode);
|
|
|
|
objectNode = persistStorageFile(objectNode);
|
|
|
|
String ret = super.create(getAsString(objectNode));
|
2019-01-10 12:29:47 +01:00
|
|
|
if(persisted) {
|
|
|
|
String gotResourceID = result.get(ID_KEY).asText();
|
2019-02-08 11:10:59 +01:00
|
|
|
if(gotResourceID != null && gotResourceID.compareTo(resourceID) != 0) {
|
2019-01-10 12:29:47 +01:00
|
|
|
resourceID = gotResourceID;
|
|
|
|
String revisionID = result.get(REVISION_ID_KEY).asText();
|
|
|
|
storageHubManagement.renameFile(resourceID, revisionID);
|
|
|
|
}
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} catch(WebApplicationException e) {
|
2019-01-10 12:29:47 +01:00
|
|
|
// TODO Remove created file if any
|
2018-12-04 12:06:22 +01:00
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
2019-01-10 12:29:47 +01:00
|
|
|
// TODO Remove created file if any
|
2018-12-04 12:06:22 +01:00
|
|
|
throw new InternalServerErrorException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String create(String json) {
|
|
|
|
JsonNode jsonNode = getAsJsonNode(json);
|
|
|
|
return create(jsonNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String read() {
|
|
|
|
return sendGetRequest(READ, getMapWithID(resourceID));
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:23:49 +02:00
|
|
|
protected String update(JsonNode jsonNode) throws Exception {
|
2018-12-04 12:06:22 +01:00
|
|
|
ObjectNode resourceNode = (ObjectNode) jsonNode;
|
|
|
|
// This cannot be moved outside otherwise we don't
|
|
|
|
resourceNode = validate(resourceNode);
|
|
|
|
|
|
|
|
getPreviousRepresentation();
|
|
|
|
|
|
|
|
String oldURL = previousRepresentation.get(CKANResource.URL_KEY).asText();
|
|
|
|
String newURL = resourceNode.get(CKANResource.URL_KEY).asText();
|
2019-02-08 11:10:59 +01:00
|
|
|
if(oldURL.compareTo(newURL) == 0) {
|
2018-12-04 12:06:22 +01:00
|
|
|
logger.trace("The URL of the resource with id {} was not changed", resourceID);
|
2019-02-08 11:10:59 +01:00
|
|
|
} else {
|
|
|
|
logger.trace("The URL of resource with id {} has been changed the old URL was {}, the new URL is {}",
|
|
|
|
resourceID, oldURL, newURL);
|
2018-12-04 12:06:22 +01:00
|
|
|
resourceNode = persistStorageFile(resourceNode);
|
|
|
|
/*
|
|
|
|
try {
|
|
|
|
URL urlOLD = new URL(oldURL);
|
|
|
|
deleteStorageResource(urlOLD);
|
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("Unable to remove old file at URL {}", oldURL);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
String ret = super.update(getAsString(resourceNode));
|
|
|
|
String revisionID = result.get(REVISION_ID_KEY).asText();
|
|
|
|
storageHubManagement.addRevisionID(resourceID, revisionID);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String update(String json) {
|
2019-05-20 17:23:49 +02:00
|
|
|
try {
|
|
|
|
JsonNode jsonNode = getAsJsonNode(json);
|
|
|
|
return update(jsonNode);
|
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new WebApplicationException(e);
|
|
|
|
}
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String patch(String json) {
|
|
|
|
String[] moreAllowed = new String[] {HEAD.class.getSimpleName(), GET.class.getSimpleName(),
|
|
|
|
PUT.class.getSimpleName(), DELETE.class.getSimpleName()};
|
|
|
|
throw new NotAllowedException(OPTIONS.class.getSimpleName(), moreAllowed);
|
|
|
|
}
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
@Override
|
|
|
|
public void delete(boolean purge) {
|
|
|
|
delete();
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
@Override
|
|
|
|
public void delete() {
|
|
|
|
try {
|
|
|
|
deleteFile();
|
2019-01-29 17:46:22 +01:00
|
|
|
sendPostRequest(DELETE, createJsonNodeWithID(resourceID));
|
2018-12-04 12:06:22 +01:00
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new WebApplicationException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void purge() {
|
|
|
|
String[] moreAllowed = new String[] {HEAD.class.getSimpleName(), GET.class.getSimpleName(),
|
|
|
|
PUT.class.getSimpleName(), DELETE.class.getSimpleName()};
|
|
|
|
throw new NotAllowedException(OPTIONS.class.getSimpleName(), moreAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonNode createOrUpdate(JsonNode jsonNode) {
|
|
|
|
ObjectNode resourceNode = (ObjectNode) jsonNode;
|
|
|
|
if(resourceNode.has(ID_KEY)) {
|
2019-05-20 17:23:49 +02:00
|
|
|
try {
|
|
|
|
update(resourceNode);
|
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new WebApplicationException(e);
|
|
|
|
}
|
2019-02-08 11:10:59 +01:00
|
|
|
} else {
|
2018-12-04 12:06:22 +01:00
|
|
|
create(resourceNode);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteFile() {
|
|
|
|
try {
|
|
|
|
getPreviousRepresentation();
|
|
|
|
URL url = new URL(previousRepresentation.get(URL_KEY).asText());
|
2019-01-10 12:29:47 +01:00
|
|
|
mimeType = previousRepresentation.get(MIME_TYPE_KEY).asText();
|
|
|
|
deleteStorageResource(url, resourceID, mimeType);
|
2018-12-04 12:06:22 +01:00
|
|
|
} catch(Exception e) {
|
2019-02-08 11:10:59 +01:00
|
|
|
logger.error("Unable to delete resource {}",
|
|
|
|
previousRepresentation != null ? getAsString(previousRepresentation) : "");
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 11:10:59 +01:00
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
public void rollback() {
|
2019-02-08 11:10:59 +01:00
|
|
|
if(previousRepresentation != null) {
|
2019-05-20 17:23:49 +02:00
|
|
|
try {
|
|
|
|
update(previousRepresentation);
|
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new WebApplicationException(e);
|
|
|
|
}
|
2019-02-08 11:10:59 +01:00
|
|
|
} else {
|
2018-12-04 12:06:22 +01:00
|
|
|
delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|