From 8ffe031cf853f155c4fe81ac77c3f8ead8f5c5e5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 20 May 2019 15:23:49 +0000 Subject: [PATCH] Merged from branch of release 4.14.0 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/gcat@179481 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 2 +- .settings/org.eclipse.jdt.core.prefs | 6 - distro/changelog.xml | 8 + pom.xml | 13 +- .../org/gcube/gcat/oldutils/Validator.java | 13 +- .../org/gcube/gcat/persistence/ckan/CKAN.java | 23 ++- .../gcat/persistence/ckan/CKANGroup.java | 5 +- .../gcat/persistence/ckan/CKANLicense.java | 33 ++++ .../gcat/persistence/ckan/CKANPackage.java | 77 +++++++-- .../gcat/persistence/ckan/CKANResource.java | 78 ++++++++- .../gcat/persistence/ckan/CKANUtility.java | 24 ++- .../org/gcube/gcat/profile/ISProfile.java | 20 ++- .../gcube/gcat/profile/MetadataUtility.java | 50 +----- .../java/org/gcube/gcat/rest/BaseREST.java | 2 +- .../java/org/gcube/gcat/rest/Namespace.java | 2 +- .../java/org/gcube/gcat/rest/Profile.java | 14 +- src/main/java/org/gcube/gcat/rest/REST.java | 1 + .../rest/ScienceCatalogueExceptionMapper.java | 7 + .../gcat/social/GcoreEndpointReaderSNL.java | 2 + .../org/gcube/gcat/social/SocialService.java | 160 +++++++++++++----- .../org/gcube/gcat/utils/URIResolver.java | 15 +- .../CatalogueStorageHubManagement.java | 6 +- src/test/java/org/gcube/gcat/ContextTest.java | 3 +- .../persistence/ckan/CKANLicenseTest.java | 28 ++- .../ckan/CKANOrganizationTest.java | 7 + .../persistence/ckan/CKANPackageTest.java | 17 +- .../persistence/ckan/CKANResourceTest.java | 19 +++ .../gcube/gcat/social/SocialServiceTest.java | 26 ++- 28 files changed, 494 insertions(+), 167 deletions(-) diff --git a/.classpath b/.classpath index ace8266..9d886d5 100644 --- a/.classpath +++ b/.classpath @@ -27,7 +27,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 6e80039..4ede96d 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,2 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/distro/changelog.xml b/distro/changelog.xml index 5ecf3e2..7e78c56 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -5,6 +5,14 @@ Separated REST class for Profile management from the logic which effectively manage profile on IS Tags are now properly created/added according to profile definition #16182 Groups are now properly created/added according to profile definition #16183 + Fixed bug on getting responses from social service #16330 + Setting format in resources #16347 + Validated provided LicenseId for Item #16354 + Capacity field is removed from the provided item content if any #16410 + Fixed bug on social post creation #16322 + Added the possibility to disable social post on item creation #16322 + Improved social post message #16322 + Added support to publish in specific organizations when the request is not performed at VRE level #16635 Added Item URL via URI Resolver in extras field #13309 diff --git a/pom.xml b/pom.xml index 1489d8d..867eb31 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + org.apache.tika + tika-core + 1.20 + + javax.cache @@ -104,11 +110,8 @@ org.glassfish.jersey.containers jersey-container-servlet - + org.glassfish.jersey.media diff --git a/src/main/java/org/gcube/gcat/oldutils/Validator.java b/src/main/java/org/gcube/gcat/oldutils/Validator.java index f53275f..aca0626 100644 --- a/src/main/java/org/gcube/gcat/oldutils/Validator.java +++ b/src/main/java/org/gcube/gcat/oldutils/Validator.java @@ -67,7 +67,7 @@ public class Validator { this.mapper = mapper; } - public ObjectNode validateAgainstProfile(ObjectNode objectNode) throws Exception { + public ObjectNode validateAgainstProfile(ObjectNode objectNode, MetadataUtility metadataUtility) throws Exception { ArrayNode extrasArrayOriginal = (ArrayNode) objectNode.get(CKANPackage.EXTRAS_KEY); if(extrasArrayOriginal == null || extrasArrayOriginal.size()==0) { @@ -107,7 +107,10 @@ public class Validator { String profileName = metadataTypeCF.getValue(); // fetch the profile by metadata type specified above - MetadataUtility metadataUtility = MetadataUtility.getInstance(); + + if(metadataUtility == null) { + metadataUtility = new MetadataUtility(); + } MetadataFormat profile = metadataUtility.getMetadataFormat(profileName); if(profile == null) { throw new BadRequestException("'" + CKANPackage.EXTRAS_KEY_VALUE_SYSTEM_TYPE + "' extra field's value ('" @@ -233,11 +236,11 @@ public class Validator { * @return * @throws Exception */ - public void createGroupAsSysAdmin(String groupName) throws Exception { + public void createGroupAsSysAdmin(String title) throws Exception { String sysAdminAPI = CKANUtility.getSysAdminAPI(); CKANGroup ckanGroup = new CKANGroup(); ckanGroup.setApiKey(sysAdminAPI); - ckanGroup.setName(CKANGroup.getGroupName(groupName)); + ckanGroup.setName(CKANGroup.getGroupName(title)); try { ckanGroup.read(); } catch (WebApplicationException e) { @@ -250,7 +253,7 @@ public class Validator { throw new InternalServerErrorException(e); }finally { try { - addUserToGroupAsSysAdmin(groupName); + addUserToGroupAsSysAdmin(title); }catch (WebApplicationException e) { throw e; }catch (Exception e) { diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java index 08f5209..b1677e4 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java @@ -14,8 +14,10 @@ import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.UriInfo; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; @@ -60,7 +62,7 @@ public abstract class CKAN { public final static String CKAN_API_PATH = "/api/3/action/"; // ckan header authorization - public final static String AUTH_CKAN_HEADER = "Authorization"; + public final static String AUTH_CKAN_HEADER = HttpHeaders.AUTHORIZATION; public final static String NAME_REGEX = "^[a-z0-9_\\\\-]{2,100}$"; @@ -82,6 +84,12 @@ public abstract class CKAN { protected String nameRegex; + protected UriInfo uriInfo; + + public void setUriInfo(UriInfo uriInfo) { + this.uriInfo = uriInfo; + } + public String getApiKey() { if(apiKey == null) { try { @@ -253,11 +261,16 @@ public abstract class CKAN { return result; } - protected GXHTTPStringRequest getGXHTTPStringRequest(String path) throws UnsupportedEncodingException { + protected GXHTTPStringRequest getGXHTTPStringRequest(String path, boolean post) throws UnsupportedEncodingException { String catalogueURL = dataCatalogue.getCatalogueUrl(); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(catalogueURL); gxhttpStringRequest.from(Constants.CATALOGUE_NAME); - gxhttpStringRequest.header("Content-type", MediaType.APPLICATION_JSON); + + if(post) { + gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + } + gxhttpStringRequest.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); + gxhttpStringRequest.isExternalCall(true); gxhttpStringRequest.header(AUTH_CKAN_HEADER, getApiKey()); gxhttpStringRequest.path(path); @@ -286,7 +299,7 @@ public abstract class CKAN { protected String sendGetRequest(String path, Map parameters) { try { logger.debug("Going to send GET request with parameters {}", parameters); - GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path); + GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path, false); gxhttpStringRequest.queryParams(parameters); HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); return getResultAsString(httpURLConnection); @@ -300,7 +313,7 @@ public abstract class CKAN { protected String sendPostRequest(String path, String body) { try { logger.debug("Going to send POST request with body {}", body); - GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path); + GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path, true); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body); return getResultAsString(httpURLConnection); } catch(WebApplicationException e) { diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java index a24932b..cbf8620 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java @@ -3,6 +3,8 @@ package org.gcube.gcat.persistence.ckan; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; +import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods; + import com.fasterxml.jackson.databind.node.ObjectNode; /** @@ -37,7 +39,8 @@ public class CKANGroup extends CKAN { } public static String getGroupName(String name) { - return name.trim().toLowerCase().replaceAll(" ", "_"); + return CatalogueUtilMethods.fromGroupTitleToName(name); + // return name.trim().toLowerCase().replaceAll(" ", "_"); } public String create() throws WebApplicationException { diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANLicense.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANLicense.java index fc79808..775c984 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANLicense.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANLicense.java @@ -1,5 +1,8 @@ package org.gcube.gcat.persistence.ckan; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; + /** * @author Luca Frosini (ISTI - CNR) */ @@ -13,4 +16,34 @@ public class CKANLicense extends CKAN { LIST = LICENSES_LIST; } + protected static ArrayNode getLicenses() { + CKANLicense ckanLicense = new CKANLicense(); + ckanLicense.list(-1,-1); + ArrayNode arrayNode = (ArrayNode) ckanLicense.getJsonNodeResult(); + return arrayNode; + } + + public static boolean checkLicenseId(String licenseId) throws Exception { + return checkLicenseId(getLicenses(), licenseId); + } + + // TODO Use a Cache + protected static boolean checkLicenseId(ArrayNode arrayNode, String licenseId) throws Exception { + try { + for(JsonNode jsonNode : arrayNode) { + try { + String id = jsonNode.get(ID_KEY).asText(); + if(id.compareTo(licenseId)==0) { + return true; + } + }catch (Exception e) { + + } + } + return false; + }catch (Exception e) { + throw e; + } + } + } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index c05c08f..1cbcdcc 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -14,11 +14,13 @@ import javax.ws.rs.NotAllowedException; import javax.ws.rs.OPTIONS; import javax.ws.rs.PUT; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response.Status; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.gcat.annotation.PURGE; +import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.oldutils.Validator; import org.gcube.gcat.profile.MetadataUtility; import org.gcube.gcat.social.SocialService; @@ -84,6 +86,7 @@ public class CKANPackage extends CKAN { protected static final String PRIVATE_KEY = "private"; protected static final String SEARCHABLE_KEY = "searchable"; + protected static final String CAPACITY_KEY = "capacity"; // protected static final String INCLUDE_PRIVATE_KEY = "include_private"; // protected static final String INCLUDE_DRAFTS_KEY = "include_drafts"; @@ -91,6 +94,8 @@ public class CKANPackage extends CKAN { public static final String GROUPS_KEY = "groups"; public static final String TAGS_KEY = "tags"; + protected SocialService socialService; + protected final List managedResources; protected String itemID; @@ -148,10 +153,29 @@ public class CKANPackage extends CKAN { if(licenseId == null || licenseId.isEmpty()) { throw new BadRequestException( "You must specify a license identifier for the item. License list can be retrieved using licence collection"); + }else { + try { + CKANLicense.checkLicenseId(licenseId); + }catch (Exception e) { + throw new BadRequestException( + "You must specify an existing license identifier for the item. License list can be retrieved using licence collection"); + } } - JsonNode userJsonNode = CKANUtility.getCKANUser(); - objectNode.put(AUTHOR_KEY, userJsonNode.get(CKANUser.NAME).asText()); + if(objectNode.has(CAPACITY_KEY)) { + /* + * When a client provides the 'capacity' field as 'private', the item is not counted in the + * total number of items in the GUI. We want to avoid such a behavior + * See https://support.d4science.org/issues/16410 + */ + objectNode.remove(CAPACITY_KEY); + } + + socialService = new SocialService(); + + JsonNode userJsonNode = CKANUtility.getCKANUser(false); + String ckanUsername = userJsonNode.get(CKANUser.NAME).asText(); + objectNode.put(AUTHOR_KEY, ckanUsername); objectNode.put(AUTHOR_EMAIL_KEY, userJsonNode.get(CKANUser.EMAIL).asText()); // owner organization must be specified if the token belongs to a VRE @@ -179,16 +203,17 @@ public class CKANPackage extends CKAN { } } } else { + gotOrganization = organizationFromContext; objectNode.put(OWNER_ORG_KEY, organizationFromContext); } } else { - // TODO check if the requested organization context is a sub context of current context - // TODO check if the check is correct for PARTHENOS if(gotOrganization == null) { throw new BadRequestException("You must specify an Organization usign " + OWNER_ORG_KEY + " field"); } } + CKANUtility.addUserToOrganization(ckanUsername, gotOrganization, CKANUtility.MEMBER_ROLE, false); + return objectNode; } @@ -198,10 +223,10 @@ public class CKANPackage extends CKAN { ObjectNode objectNode = checkBaseInformation(json); // Validating against profiles if any - MetadataUtility metadataUtility = MetadataUtility.getInstance(); + MetadataUtility metadataUtility = new MetadataUtility(); if(!metadataUtility.getMetadataProfiles().isEmpty()) { Validator validator = new Validator(mapper); - objectNode = validator.validateAgainstProfile(objectNode); + objectNode = validator.validateAgainstProfile(objectNode, metadataUtility); } return objectNode; @@ -314,6 +339,33 @@ public class CKANPackage extends CKAN { return catalogueItemURL; } + protected void sendSocialPost(String title, String catalogueItemURL) { + try { + boolean socialPost = true; + try { + MultivaluedMap queryParameters = uriInfo.getQueryParameters(); + if(queryParameters.containsKey(GCatConstants.SOCIAL_POST_PARAMETER)) { + socialPost = Boolean.parseBoolean(queryParameters.getFirst(GCatConstants.SOCIAL_POST_PARAMETER)); + } + }catch (Exception e) { + socialPost = true; + } + + if(socialPost) { + ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY); + socialService.setItemID(itemID); + socialService.setItemURL(catalogueItemURL); + socialService.setTags(arrayNode); + socialService.setItemTitle(title); + socialService.start(); + }else { + logger.info("The request explicitly disabled the Social Post."); + } + }catch (Exception e) { + logger.warn("error dealing with Social Post. The service will not raise the exception belove. Please contact the administrator to let him know about this message.", e); + } + } + // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create @Override public String create(String json) { @@ -328,7 +380,12 @@ public class CKANPackage extends CKAN { ((ObjectNode) jsonNode).remove(RESOURCES_KEY); } - String catalogueItemURL = addItemURLViaResolver(jsonNode); + ScopeBean scopeBean = new ScopeBean(ContextUtility.getCurrentContext()); + + String catalogueItemURL = ""; + if(scopeBean.is(Type.VRE)) { + catalogueItemURL = addItemURLViaResolver(jsonNode); + } super.create(getAsString(jsonNode)); @@ -342,9 +399,9 @@ public class CKANPackage extends CKAN { // Actions performed after a package has been correctly created on ckan. String title = result.get(TITLE_KEY).asText(); - ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY); - SocialService packagePostActions = new SocialService(catalogueItemURL, name, arrayNode, title); - packagePostActions.start(); + if(scopeBean.is(Type.VRE)) { + sendSocialPost(title, catalogueItemURL); + } return getAsString(result); } catch(WebApplicationException e) { diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java index 3d06e8a..4d506dc 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java @@ -16,6 +16,8 @@ import javax.ws.rs.PUT; import javax.ws.rs.WebApplicationException; import org.apache.commons.io.FilenameUtils; +import org.apache.tika.mime.MimeType; +import org.apache.tika.mime.MimeTypes; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.ContextUtility; @@ -49,6 +51,7 @@ public class CKANResource extends CKAN { private static final String RESOURCES_KEY = "resources"; private static final String PACKAGE_ID_KEY = "package_id"; + private static final String FORMAT_KEY = "format"; private static final String MIME_TYPE_KEY = "mimetype"; private static final String REVISION_ID_KEY = "revision_id"; @@ -56,6 +59,8 @@ public class CKANResource extends CKAN { public final static String RESOURCE_NAME_REGEX = "^[\\s\\S]*$"; + public static final MimeTypes ALL_MIME_TYPES; + /* 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"; private static final String URI_RESOLVER_STORAGE_HUB_HOST_DEV = "data1-d.d4science.org"; @@ -70,7 +75,10 @@ public class CKANResource extends CKAN { } else { URI_RESOLVER_STORAGE_HUB_HOST = URI_RESOLVER_STORAGE_HUB_HOST_PROD; } - + // 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(); } /* TODO END Code to be Removed */ @@ -86,6 +94,7 @@ public class CKANResource extends CKAN { protected URL persistedURL; protected String mimeType; + protected String originalFileExtension; protected JsonNode previousRepresentation; @@ -156,6 +165,32 @@ public class CKANResource extends CKAN { return getAsString(resources); } + protected String getFormat() { + String format = null; + if(originalFileExtension!=null) { + format = originalFileExtension; + }else { + try { + MimeType mimeTypeClzInstance = ALL_MIME_TYPES.forName(mimeType); + format = mimeTypeClzInstance.getExtension(); + // List extensions = mimeTypeClzInstance.getExtensions(); + if(format==null || format.compareTo("")==0) { + format = mimeType.split("/")[1].split(";")[0]; + } + } catch(Exception e) { + try { + format = mimeType.split("/")[1].split(";")[0]; + }catch (Exception ex) { + format = null; + } + } + } + if(format!=null && format.startsWith(".")) { + format = format.substring(1); + } + return format; + } + protected ObjectNode persistStorageFile(ObjectNode objectNode) { if(objectNode.has(URL_KEY)) { @@ -173,9 +208,16 @@ public class CKANResource extends CKAN { if(name != null) { objectNode.put(NAME_KEY, name); } - + if(mimeType != null) { objectNode.put(MIME_TYPE_KEY, mimeType); + + if(!objectNode.has(FORMAT_KEY)) { + String format = getFormat(); + if(format!=null) { + objectNode.put(FORMAT_KEY, format); + } + } } objectNode.put(URL_KEY, url.toString()); @@ -269,7 +311,9 @@ public class CKANResource extends CKAN { storageHubManagement = new CatalogueStorageHubManagement(); try { persistedURL = storageHubManagement.ensureResourcePersistence(persistedURL, itemID, resourceID); - name = FilenameUtils.removeExtension(storageHubManagement.getOriginalFilename()); + String originalFilename = storageHubManagement.getOriginalFilename(); + name = FilenameUtils.removeExtension(originalFilename); + originalFileExtension = FilenameUtils.getExtension(originalFilename); mimeType = storageHubManagement.getMimeType(); persisted = true; } catch(Exception e) { @@ -336,7 +380,7 @@ public class CKANResource extends CKAN { return sendGetRequest(READ, getMapWithID(resourceID)); } - protected String update(JsonNode jsonNode) { + protected String update(JsonNode jsonNode) throws Exception { ObjectNode resourceNode = (ObjectNode) jsonNode; // This cannot be moved outside otherwise we don't resourceNode = validate(resourceNode); @@ -368,8 +412,14 @@ public class CKANResource extends CKAN { @Override public String update(String json) { - JsonNode jsonNode = getAsJsonNode(json); - return update(jsonNode); + try { + JsonNode jsonNode = getAsJsonNode(json); + return update(jsonNode); + } catch(WebApplicationException e) { + throw e; + } catch(Exception e) { + throw new WebApplicationException(e); + } } @Override @@ -406,7 +456,13 @@ public class CKANResource extends CKAN { public JsonNode createOrUpdate(JsonNode jsonNode) { ObjectNode resourceNode = (ObjectNode) jsonNode; if(resourceNode.has(ID_KEY)) { - update(resourceNode); + try { + update(resourceNode); + } catch(WebApplicationException e) { + throw e; + } catch(Exception e) { + throw new WebApplicationException(e); + } } else { create(resourceNode); } @@ -427,7 +483,13 @@ public class CKANResource extends CKAN { public void rollback() { if(previousRepresentation != null) { - update(previousRepresentation); + try { + update(previousRepresentation); + } catch(WebApplicationException e) { + throw e; + } catch(Exception e) { + throw new WebApplicationException(e); + } } else { delete(); } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUtility.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUtility.java index bfcd871..64feeb6 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUtility.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUtility.java @@ -49,11 +49,15 @@ public class CKANUtility { return getCKANUsername(ContextUtility.getUsername()); } - public static JsonNode getCKANUser() { - return createCKANUser(getCKANUsername()); + public static JsonNode getCKANUser(boolean addToOrganization) { + return createCKANUser(getCKANUsername(), addToOrganization); } - protected static JsonNode createCKANUser(String ckanUsername) { + public static JsonNode getCKANUser() { + return getCKANUser(true); + } + + protected static JsonNode createCKANUser(String ckanUsername, boolean addToOrganization) { ckanUsername = CKANUtility.getCKANUsername(ckanUsername); CKANUser ckanUser = new CKANUser(); ckanUser.setApiKey(getSysAdminAPI()); @@ -69,18 +73,24 @@ public class CKANUtility { } } JsonNode jsonNode = ckanUser.getJsonNodeResult(); - addUserToOrganization(ckanUsername, MEMBER_ROLE, false); + if(addToOrganization) { + addUserToOrganization(ckanUsername, MEMBER_ROLE, false); + } return jsonNode; } - protected static void addUserToOrganization(String ckanUsername, String role, boolean force) { + public static void addUserToOrganization(String ckanUsername, String organizationName, String role, boolean force) { CKANOrganization ckanOrganization = new CKANOrganization(); ckanOrganization.setApiKey(getSysAdminAPI()); - String organizationName = CKANOrganization.getCKANOrganizationName(); ckanOrganization.setName(organizationName); ckanOrganization.addUserToOrganisation(ckanUsername, role, force); } + protected static void addUserToOrganization(String ckanUsername, String role, boolean force) { + String organizationName = CKANOrganization.getCKANOrganizationName(); + addUserToOrganization(ckanUsername, organizationName, role, force); + } + public static String getApiKey() throws Exception { String ckanUsername = getCKANUsername(); return getApiKey(ckanUsername); @@ -90,7 +100,7 @@ public class CKANUtility { try { String apiKey = getCatalogue().getApiKeyFromUsername(ckanUsername); if(apiKey == null) { - JsonNode jsonNode = createCKANUser(ckanUsername); + JsonNode jsonNode = createCKANUser(ckanUsername, true); apiKey = jsonNode.get(API_KEY).asText(); } return apiKey; diff --git a/src/main/java/org/gcube/gcat/profile/ISProfile.java b/src/main/java/org/gcube/gcat/profile/ISProfile.java index 0863df4..8f83cf5 100644 --- a/src/main/java/org/gcube/gcat/profile/ISProfile.java +++ b/src/main/java/org/gcube/gcat/profile/ISProfile.java @@ -4,6 +4,7 @@ import java.io.StringWriter; import java.util.List; import java.util.Set; +import javax.ws.rs.BadRequestException; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; @@ -23,6 +24,7 @@ import org.json.JSONObject; import org.json.XML; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -47,7 +49,7 @@ public class ISProfile { ArrayNode arrayNode = mapper.createArrayNode(); try { - Set names = MetadataUtility.getInstance().getProfilesNames(); + Set names = (new MetadataUtility()).getProfilesNames(); for(String name : names) { arrayNode.add(name); } @@ -134,7 +136,7 @@ public class ISProfile { public String read(String name, boolean xml) { try { - String profile = MetadataUtility.getInstance().getMetadataFormat(name).getMetadataSource(); + String profile = (new MetadataUtility()).getMetadataFormat(name).getMetadataSource(); if(profile != null) { if(xml) { return profile; @@ -156,8 +158,8 @@ public class ISProfile { public boolean createOrUpdate(String name, String xml) { try { - MetadataUtility metadataUtility = MetadataUtility.getInstance(); - metadataUtility.getDataCalogueMetadataFormatReader().validateProfile(xml); + MetadataUtility metadataUtility = new MetadataUtility(); + metadataUtility.validateProfile(xml); if(metadataUtility.getMetadataFormat(name) == null) { createGenericResource(name, xml); return true; @@ -167,18 +169,20 @@ public class ISProfile { } } catch(WebApplicationException e) { throw e; + } catch (SAXException e) { + throw new BadRequestException(e); } catch(Exception e) { - throw new InternalServerErrorException(e.getMessage()); + throw new InternalServerErrorException(e); } finally { // TOOD Actually Cache has been removed. Remove the following code if it will not be re-introduced // Cleaning the cache - MetadataUtility.clearCache(); + // MetadataUtility.clearCache(); } } public boolean delete(String name) { try { - MetadataUtility metadataUtility = MetadataUtility.getInstance(); + MetadataUtility metadataUtility = new MetadataUtility(); if(metadataUtility.getMetadataFormat(name) == null) { throw new NotFoundException("Profile with name " + name + " not found"); } else { @@ -191,7 +195,7 @@ public class ISProfile { throw new InternalServerErrorException(e.getMessage()); } finally { // Cleaning the cache - MetadataUtility.clearCache(); + // MetadataUtility.clearCache(); } } } diff --git a/src/main/java/org/gcube/gcat/profile/MetadataUtility.java b/src/main/java/org/gcube/gcat/profile/MetadataUtility.java index 810c881..b0ad224 100644 --- a/src/main/java/org/gcube/gcat/profile/MetadataUtility.java +++ b/src/main/java/org/gcube/gcat/profile/MetadataUtility.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import javax.ws.rs.InternalServerErrorException; - import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; @@ -21,52 +19,12 @@ public class MetadataUtility { */ private Map metadataProfiles; - private static DataCalogueMetadataFormatReader getDataCalogueMetadataFormatReaderInstance() throws Exception { - /* - Cache readerCache = CachesManager.getReaderCache(); - String context = ScopeProvider.instance.get(); - DataCalogueMetadataFormatReader reader - if(readerCache.containsKey(context)) { - reader = (DataCalogueMetadataFormatReader) readerCache.get(context); - } else { - reader = new DataCalogueMetadataFormatReader(); - readerCache.put(context, reader); - } - */ - return new DataCalogueMetadataFormatReader(); + public MetadataUtility() throws Exception{ + dataCalogueMetadataFormatReader = new DataCalogueMetadataFormatReader(); } - public static void clearCache() { - /* - Cache readerCache = CachesManager.getReaderCache(); - readerCache.clear(); - */ - } - - private MetadataUtility() throws Exception{ - dataCalogueMetadataFormatReader = getDataCalogueMetadataFormatReaderInstance(); - } - - private static final InheritableThreadLocal metadataUtility = new InheritableThreadLocal() { - - @Override - protected MetadataUtility initialValue() { - try { - return new MetadataUtility(); - } catch(Exception e) { - throw new InternalServerErrorException("Unable to instantiate MetadataUtility."); - } - } - - }; - - public static MetadataUtility getInstance() { - return metadataUtility.get(); - } - - - public DataCalogueMetadataFormatReader getDataCalogueMetadataFormatReader() { - return dataCalogueMetadataFormatReader; + public void validateProfile(String xmlProfile) throws Exception { + dataCalogueMetadataFormatReader.validateProfile(xmlProfile); } public Map getMetadataProfiles() throws Exception{ diff --git a/src/main/java/org/gcube/gcat/rest/BaseREST.java b/src/main/java/org/gcube/gcat/rest/BaseREST.java index 6164b57..6405e40 100644 --- a/src/main/java/org/gcube/gcat/rest/BaseREST.java +++ b/src/main/java/org/gcube/gcat/rest/BaseREST.java @@ -13,7 +13,7 @@ public class BaseREST { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Context - private UriInfo uriInfo; + protected UriInfo uriInfo; protected static final String LOCATION_HEADER = "Location"; diff --git a/src/main/java/org/gcube/gcat/rest/Namespace.java b/src/main/java/org/gcube/gcat/rest/Namespace.java index df16544..39d5733 100644 --- a/src/main/java/org/gcube/gcat/rest/Namespace.java +++ b/src/main/java/org/gcube/gcat/rest/Namespace.java @@ -30,7 +30,7 @@ public class Namespace extends BaseREST implements org.gcube.gcat.api.interfaces ArrayNode arrayNode = mapper.createArrayNode(); try { - List namespaces = MetadataUtility.getInstance().getNamespaceCategories(); + List namespaces = (new MetadataUtility()).getNamespaceCategories(); for(NamespaceCategory namespaceCategory : namespaces) { ObjectNode namespace = mapper.createObjectNode(); namespace.put("id", namespaceCategory.getId()); diff --git a/src/main/java/org/gcube/gcat/rest/Profile.java b/src/main/java/org/gcube/gcat/rest/Profile.java index 8cda655..1404845 100644 --- a/src/main/java/org/gcube/gcat/rest/Profile.java +++ b/src/main/java/org/gcube/gcat/rest/Profile.java @@ -10,13 +10,13 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; -import javax.xml.ws.WebServiceException; import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.profile.ISProfile; @@ -42,7 +42,7 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P ISProfile isProfile = new ISProfile(); ArrayNode arrayNode = isProfile.list(); return isProfile.getMapper().writeValueAsString(arrayNode); - } catch(WebServiceException e) { + } catch(WebApplicationException e) { throw e; } catch(Exception e) { throw new InternalServerErrorException(e); @@ -64,13 +64,13 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P xml = true; } return isProfile.read(name, xml); - } catch(WebServiceException e) { + } catch(WebApplicationException e) { throw e; } catch(Exception e) { throw new InternalServerErrorException(e); } } - + @PUT @Path("/{" + PROFILE_NAME_PARAMETER + "}") @Consumes(MediaType.APPLICATION_XML) @@ -84,12 +84,12 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P if(created) { responseBuilder = Response.status(Status.CREATED); responseBuilder.header(LOCATION_HEADER, uriInfo.getAbsolutePath()); - }else { + } else { responseBuilder = Response.status(Status.OK); } responseBuilder.entity(xml); return responseBuilder.type(MediaType.APPLICATION_XML).build(); - } catch(WebServiceException e) { + } catch(WebApplicationException e) { throw e; } catch(Exception e) { throw new InternalServerErrorException(e); @@ -104,7 +104,7 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P ISProfile isProfile = new ISProfile(); isProfile.delete(name); return Response.status(Status.NO_CONTENT).build(); - } catch(WebServiceException e) { + } catch(WebApplicationException e) { throw e; } catch(Exception e) { throw new InternalServerErrorException(e); diff --git a/src/main/java/org/gcube/gcat/rest/REST.java b/src/main/java/org/gcube/gcat/rest/REST.java index 09a1742..8160f8f 100644 --- a/src/main/java/org/gcube/gcat/rest/REST.java +++ b/src/main/java/org/gcube/gcat/rest/REST.java @@ -40,6 +40,7 @@ public class REST extends BaseREST implements CRUD tags; - protected final GcoreEndpointReaderSNL socialService; + protected final GcoreEndpointReaderSNL gcoreEndpointReaderSNL; protected final ObjectMapper objectMapper; + protected String itemID; + protected String itemURL; + protected String itemTitle; + protected List tags; + protected JsonNode gCubeUserProfile; + public SocialService() throws Exception { super(); - this.socialService = new GcoreEndpointReaderSNL(); + this.gcoreEndpointReaderSNL = new GcoreEndpointReaderSNL(); this.objectMapper = new ObjectMapper(); } - public SocialService(String id, String url, List tags, String title) throws Exception { - this(); - this.id = id; - this.url = url; - this.tags = tags; - this.title = title; + public String getItemID() { + return itemID; + } + + public void setItemID(String itemID) { + this.itemID = itemID; + } + + public String getItemURL() { + return itemURL; + } + + public void setItemURL(String itemURL) { + this.itemURL = itemURL; } - public SocialService(String id, String url, ArrayNode arrayNode, String title) throws Exception { - this(); - this.id = id; - this.url = url; - + public String getItemTitle() { + return itemTitle; + } + + public void setItemTitle(String itemTitle) { + this.itemTitle = itemTitle; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public void setTags(ArrayNode tags) { this.tags = new ArrayList<>(); - if(arrayNode != null && arrayNode.size() > 0) { - for(int i = 0; i < arrayNode.size(); i++) { - String tagName = arrayNode.get(i).get("display_name").asText(); - tags.add(tagName); + if(tags != null && tags.size() > 0) { + for(int i = 0; i < tags.size(); i++) { + JsonNode jsonNode = tags.get(i); + String tagName = ""; + if(jsonNode.has("display_name")) { + tagName = jsonNode.get("display_name").asText(); + }else { + tagName = jsonNode.get("name").asText(); + } + this.tags.add(tagName); } } - - this.title = title; } /** @@ -89,17 +123,47 @@ public class SocialService extends Thread { * @throws Exception */ public JsonNode getGCubeUserProfile() throws Exception { - String username = ContextUtility.getUsername(); - return getGCubeUserProfile(username); + if(gCubeUserProfile==null) { + String username = ContextUtility.getUsername(); + gCubeUserProfile = getGCubeUserProfile(username); + } + return gCubeUserProfile; } - public JsonNode getGCubeUserProfile(String username) throws Exception { + protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException { + StringBuilder result = new StringBuilder(); + try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while((line = reader.readLine()) != null) { + result.append(line); + } + } + + return result; + } + + protected String getResultAsString(HttpURLConnection httpURLConnection) throws IOException { + int responseCode = httpURLConnection.getResponseCode(); + if(responseCode >= Status.BAD_REQUEST.getStatusCode()) { + Status status = Status.fromStatusCode(responseCode); + InputStream inputStream = httpURLConnection.getErrorStream(); + StringBuilder result = getStringBuilder(inputStream); + logger.trace(result.toString()); + throw new WebApplicationException(status); + } + InputStream inputStream = httpURLConnection.getInputStream(); + String ret = getStringBuilder(inputStream).toString(); + logger.trace("Got Respose is {}", ret); + return ret; + } + + protected JsonNode getGCubeUserProfile(String username) throws Exception { Cache userCache = CachesManager.getUserCache(); if(userCache.containsKey(username)) return userCache.get(username); else { - String socialServiceBasePath = socialService.getServiceBasePath(); + String socialServiceBasePath = gcoreEndpointReaderSNL.getServiceBasePath(); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(socialServiceBasePath); gxhttpStringRequest.header("User-Agent", Constants.CATALOGUE_NAME); @@ -107,23 +171,29 @@ public class SocialService extends Thread { gxhttpStringRequest.path(SOCIAL_SERVICE_GET_USER_INFO_PATH); HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); - String message = httpURLConnection.getResponseMessage(); - JsonNode jsonNode = objectMapper.readTree(message); + + String ret = getResultAsString(httpURLConnection); + + JsonNode jsonNode = objectMapper.readTree(ret); userCache.put(username, jsonNode); return jsonNode; } } public String getFullName() throws Exception { - if(!ContextUtility.isApplication()) { - JsonNode jsonNode = getGCubeUserProfile(); - JsonNode result = jsonNode.get(RESULT_KEY); - return result.get(FULLNAME_IN_PROFILE_KEY).asText(); - } else { - return ContextUtility.getUsername(); + try { + if(!ContextUtility.isApplication()) { + JsonNode jsonNode = getGCubeUserProfile(); + JsonNode result = jsonNode.get(RESULT_KEY); + return result.get(FULLNAME_IN_PROFILE_KEY).asText(); + } + }catch (Exception e) { + logger.warn("Unable to get the full name of the requesting user via Social Service. The username corresponsing to the requester token will be used."); } + return ContextUtility.getUsername(); } + @Override public void run() { @@ -134,7 +204,7 @@ public class SocialService extends Thread { logger.info("Social Post are disabled in the context {}", ContextUtility.getCurrentContext()); return; } - logger.info("Going to send Social Post about the Item {} available at {}", id, url); + logger.info("Going to send Social Post about the Item {} available at {}", itemID, itemURL); boolean notifyUsers = dataCatalogue.isNotificationToUsersEnabled(); // write notification post @@ -150,7 +220,7 @@ public class SocialService extends Thread { try { String fullName = getFullName(); - String basePath = socialService.getServiceBasePath(); + String basePath = gcoreEndpointReaderSNL.getServiceBasePath(); if(basePath == null) { logger.info("Unable to write a post because there is no social networking service available"); return; @@ -158,7 +228,7 @@ public class SocialService extends Thread { basePath = basePath.endsWith("/") ? basePath : basePath + "/"; StringWriter messageWriter = new StringWriter(); - messageWriter.append(String.format(NOTIFICATION_MESSAGE, title, fullName, url)); + messageWriter.append(String.format(NOTIFICATION_MESSAGE, fullName, itemTitle, itemURL)); for(String tag : tags) { tag = tag.trim(); @@ -166,12 +236,13 @@ public class SocialService extends Thread { if(tag.endsWith("_")) { tag = tag.substring(0, tag.length() - 1); } - messageWriter.append(" #"); + messageWriter.append("#"); messageWriter.append(tag); + messageWriter.append(" "); } String message = messageWriter.toString(); - logger.debug("The post that is going to be written is {} " + message); + logger.debug("The post that is going to be written is\n{}",message); ObjectNode objectNode = objectMapper.createObjectNode(); objectNode.put(SOCIAL_POST_TEXT_KEY, message); @@ -182,13 +253,14 @@ public class SocialService extends Thread { GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); - gxhttpStringRequest.header("User-Agent", Constants.CATALOGUE_NAME); + gxhttpStringRequest.from(Constants.CATALOGUE_NAME); + gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); gxhttpStringRequest.setSecurityToken(Constants.getCatalogueApplicationToken()); gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode)); - String ret = httpURLConnection.getResponseMessage(); + String ret = getResultAsString(httpURLConnection); JsonNode jsonNode = objectMapper.readTree(ret); if(jsonNode.get(SOCIAL_POST_RESPONSE_SUCCESS_KEY).asBoolean()) { logger.info("Post written : {}", message); diff --git a/src/main/java/org/gcube/gcat/utils/URIResolver.java b/src/main/java/org/gcube/gcat/utils/URIResolver.java index 5dc6350..94bb69b 100644 --- a/src/main/java/org/gcube/gcat/utils/URIResolver.java +++ b/src/main/java/org/gcube/gcat/utils/URIResolver.java @@ -8,6 +8,8 @@ import java.net.HttpURLConnection; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; @@ -22,6 +24,10 @@ public class URIResolver { private static final String CATALOGUE_CONTEXT = "gcube_scope"; private static final String ENTITY_TYPE = "entity_context"; private static final String ENTITY_NAME = "entity_name"; + + @Deprecated + private static final String PRODUCT = "product"; + @Deprecated private static final String CATALOGUE_PLAIN_URL = "clear_url"; protected ObjectMapper mapper; @@ -48,13 +54,18 @@ public class URIResolver { ObjectNode requestContent = mapper.createObjectNode(); requestContent.put(CATALOGUE_CONTEXT, ContextUtility.getCurrentContext()); - requestContent.put(ENTITY_TYPE, EntityContext.PRODUCT.toString()); + + requestContent.put(ENTITY_TYPE, EntityContext.DATASET.toString()); requestContent.put(ENTITY_NAME, name); + + /* Uncomment the following two rows to build a version compliant with old URI RESOLVER + requestContent.put(ENTITY_TYPE, PRODUCT); requestContent.put(CATALOGUE_PLAIN_URL, String.valueOf(true)); + */ GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(uriResolverURL); gxhttpStringRequest.from(Constants.CATALOGUE_NAME); - // gxhttpStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); gxhttpStringRequest.isExternalCall(true); String body = mapper.writeValueAsString(requestContent); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body); diff --git a/src/main/java/org/gcube/gcat/workspace/CatalogueStorageHubManagement.java b/src/main/java/org/gcube/gcat/workspace/CatalogueStorageHubManagement.java index 2fd2826..306af54 100644 --- a/src/main/java/org/gcube/gcat/workspace/CatalogueStorageHubManagement.java +++ b/src/main/java/org/gcube/gcat/workspace/CatalogueStorageHubManagement.java @@ -74,7 +74,7 @@ public class CatalogueStorageHubManagement { } - protected void internalAddRevisionID(String resourceID, String revisionID) { + protected void internalAddRevisionID(String resourceID, String revisionID) throws Exception { FileContainer fileContainer = storageHubManagement.getCreatedFile(); Metadata metadata = fileContainer.get().getMetadata(); Map map = metadata.getMap(); @@ -84,7 +84,7 @@ public class CatalogueStorageHubManagement { fileContainer.setMetadata(metadata); } - public void renameFile(String resourceID, String revisionID) { + public void renameFile(String resourceID, String revisionID) throws Exception { ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken()); try { applicationMode.start(); @@ -97,7 +97,7 @@ public class CatalogueStorageHubManagement { } - public void addRevisionID(String resourceID, String revisionID) { + public void addRevisionID(String resourceID, String revisionID) throws Exception { ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken()); try { applicationMode.start(); diff --git a/src/test/java/org/gcube/gcat/ContextTest.java b/src/test/java/org/gcube/gcat/ContextTest.java index 2dbb01c..1666889 100644 --- a/src/test/java/org/gcube/gcat/ContextTest.java +++ b/src/test/java/org/gcube/gcat/ContextTest.java @@ -44,7 +44,8 @@ public class ContextTest { throw new RuntimeException(e); } - DEFAULT_TEST_SCOPE_NAME = "/gcube/preprod/preVRE"; + //DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE"; + DEFAULT_TEST_SCOPE_NAME = "/gcube/devNext/NextNext"; } public static String getCurrentScope(String token) throws ObjectNotFound, Exception { diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANLicenseTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANLicenseTest.java index de98b72..d7d666a 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANLicenseTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANLicenseTest.java @@ -1,5 +1,11 @@ package org.gcube.gcat.persistence.ckan; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + import org.gcube.gcat.ContextTest; import org.gcube.gcat.persistence.ckan.CKANLicense; import org.junit.Assert; @@ -18,11 +24,27 @@ public class CKANLicenseTest extends ContextTest { @Test public void list() throws Exception { CKANLicense license = new CKANLicense(); - String ret = license.list(-1,-1); - ObjectMapper mapper = new ObjectMapper(); - JsonNode gotList = mapper.readTree(ret); + license.list(-1,-1); + JsonNode gotList = license.getJsonNodeResult(); Assert.assertTrue(gotList instanceof ArrayNode); + ObjectMapper mapper = new ObjectMapper(); logger.debug("List :\n{}", mapper.writeValueAsString(gotList)); } + @Test + public void testCheckLicense() throws Exception { + ArrayNode arrayNode = CKANLicense.getLicenses(); + for(JsonNode jsonNode : arrayNode){ + String licenseId = jsonNode.get(CKAN.ID_KEY).asText(); + assertTrue(CKANLicense.checkLicenseId(arrayNode, licenseId)); + logger.debug("'{}' is a valid License ID", licenseId); + } + List invalidIds = new ArrayList<>(); + invalidIds.add("InvaliLicense"); + invalidIds.add("CCO"); + for(String licenseId : invalidIds) { + assertFalse(CKANLicense.checkLicenseId(arrayNode, licenseId)); + logger.debug("As expected '{}' is an INVALID License ID", licenseId); + } + } } diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java index 0e8567c..e4ed0a1 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java @@ -20,4 +20,11 @@ public class CKANOrganizationTest extends ContextTest { logger.debug("{}", ret); } + @Test + public void listOrganization() throws Exception { + ContextTest.setContextByName("/gcube"); + CKANOrganization ckanOrganization = new CKANOrganization(); + String ret = ckanOrganization.list(1000, 0); + logger.debug("{}", ret); + } } diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java index a2687bf..ec3eeea 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java @@ -27,7 +27,7 @@ public class CKANPackageTest extends ContextTest { private static final String ITEM_NAME_VALUE = "restful_transaction_model"; private static final String LICENSE_VALUE = "CC-BY-SA-4.0"; - private static final String EXTRAS_TYPE_VALUE_VALUE = "TestProfile"; + private static final String EXTRAS_TYPE_VALUE_VALUE = "TestEmptyProfile"; @Test @@ -89,18 +89,21 @@ public class CKANPackageTest extends ContextTest { itemObjectNode.put(PRIVATE_KEY, false); itemObjectNode.put(NOTES_KEY, "A research of Luca Frosini"); itemObjectNode.put(URL_KEY, "http://www.d4science.org"); + itemObjectNode.put(CKANPackage.OWNER_ORG_KEY, "nextnext"); ArrayNode tagArrayNode = itemObjectNode.putArray(CKANPackage.TAGS_KEY); ObjectNode tagNode = mapper.createObjectNode(); tagNode.put(CKANPackage.NAME_KEY, "REST"); tagArrayNode.add(tagNode); + /* ArrayNode resourceArrayNode = itemObjectNode.putArray(CKANPackage.RESOURCES_KEY); ObjectNode resourceNode = mapper.createObjectNode(); resourceNode.put(CKANResource.NAME_KEY, "RESTful Transaction Model"); // Workspace(luca.frosini) > RESTful Transaction Model v 1.0.pdf resourceNode.put(CKANResource.URL_KEY, "https://goo.gl/J8AwQW"); resourceArrayNode.add(resourceNode); + */ ArrayNode extraArrayNode = itemObjectNode.putArray(CKANPackage.EXTRAS_KEY); ObjectNode typeNode = mapper.createObjectNode(); @@ -110,7 +113,7 @@ public class CKANPackageTest extends ContextTest { ObjectNode modelNode = mapper.createObjectNode(); modelNode.put(CKANPackage.EXTRAS_KEY_KEY, "test"); - modelNode.put(CKANPackage.EXTRAS_VALUE_KEY, "test"); + modelNode.put(CKANPackage.EXTRAS_VALUE_KEY, "test 2.9°"); extraArrayNode.add(modelNode); ObjectNode populationNode = mapper.createObjectNode(); @@ -129,10 +132,20 @@ public class CKANPackageTest extends ContextTest { @Test public void create() throws Exception { + //ContextTest.setContextByName("/gcube/devNext"); ObjectMapper mapper = new ObjectMapper(); createPackage(mapper); } + @Test + public void testURIResolver() { + CKANPackage ckanPackage = new CKANPackage(); + ckanPackage.setName("Test"); + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = objectMapper.createObjectNode(); + ckanPackage.addItemURLViaResolver(objectNode); + } + @Test public void createReadUpdateUpdatePurge() throws Exception { ObjectMapper mapper = new ObjectMapper(); diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANResourceTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANResourceTest.java index 6af4dce..9d9a1c2 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANResourceTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANResourceTest.java @@ -6,6 +6,7 @@ import java.util.UUID; import org.gcube.gcat.ContextTest; import org.gcube.gcat.utils.Constants; import org.gcube.storagehub.ApplicationMode; +import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,4 +78,22 @@ public class CKANResourceTest extends ContextTest { ckanResource.delete(); } + @Test + public void testGetFormat() { + CKANResource ckanResource = new CKANResource(UUID.randomUUID().toString()); + ckanResource.mimeType = "application/zip"; + + ckanResource.originalFileExtension = ".fskk"; + String format = ckanResource.getFormat(); + Assert.assertTrue(format.compareTo("fskk")==0); + + ckanResource.originalFileExtension = null; + format = ckanResource.getFormat(); + Assert.assertTrue(format.compareTo("zip")==0); + + ckanResource.mimeType = "image/jpeg"; + format = ckanResource.getFormat(); + Assert.assertTrue(format.compareTo("jpg")==0); + } + } diff --git a/src/test/java/org/gcube/gcat/social/SocialServiceTest.java b/src/test/java/org/gcube/gcat/social/SocialServiceTest.java index 1957cdf..3fa4c49 100644 --- a/src/test/java/org/gcube/gcat/social/SocialServiceTest.java +++ b/src/test/java/org/gcube/gcat/social/SocialServiceTest.java @@ -1,7 +1,11 @@ package org.gcube.gcat.social; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + import org.gcube.gcat.ContextTest; -import org.gcube.gcat.social.SocialService; +import org.gcube.gcat.utils.Constants; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,4 +25,24 @@ public class SocialServiceTest extends ContextTest { logger.debug("gCube User Profile is {}", objectMapper.writeValueAsString(jsonNode)); } + @Test + public void testToken() throws Exception { + ContextTest.setContext(Constants.getCatalogueApplicationToken()); + } + + @Test + public void testSendPost() throws Exception { + SocialService socialService = new SocialService(); + socialService.setItemID(UUID.randomUUID().toString()); + socialService.setItemTitle("Test Item"); + socialService.setItemURL("http://www.d4science.org"); + List tags = new ArrayList<>(); + tags.add("Test"); + tags.add("ThisIsATest"); + tags.add("IgnoreIt"); + socialService.setTags(tags); + + socialService.sendSocialPost(false); + } + }