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
This commit is contained in:
Luca Frosini 2019-05-20 15:23:49 +00:00
parent eebba6f67f
commit 8ffe031cf8
28 changed files with 494 additions and 167 deletions

View File

@ -27,7 +27,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>

View File

@ -1,8 +1,2 @@
eclipse.preferences.version=1 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.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -5,6 +5,14 @@
<Change>Separated REST class for Profile management from the logic which effectively manage profile on IS</Change> <Change>Separated REST class for Profile management from the logic which effectively manage profile on IS</Change>
<Change>Tags are now properly created/added according to profile definition #16182</Change> <Change>Tags are now properly created/added according to profile definition #16182</Change>
<Change>Groups are now properly created/added according to profile definition #16183</Change> <Change>Groups are now properly created/added according to profile definition #16183</Change>
<Change>Fixed bug on getting responses from social service #16330</Change>
<Change>Setting format in resources #16347</Change>
<Change>Validated provided LicenseId for Item #16354</Change>
<Change>Capacity field is removed from the provided item content if any #16410</Change>
<Change>Fixed bug on social post creation #16322</Change>
<Change>Added the possibility to disable social post on item creation #16322</Change>
<Change>Improved social post message #16322</Change>
<Change>Added support to publish in specific organizations when the request is not performed at VRE level #16635</Change>
</Changeset> </Changeset>
<Changeset component="org.gcube.data-publishing.gcat.1-1-0" date="2019-02-26"> <Changeset component="org.gcube.data-publishing.gcat.1-1-0" date="2019-02-26">
<Change>Added Item URL via URI Resolver in extras field #13309</Change> <Change>Added Item URL via URI Resolver in extras field #13309</Change>

13
pom.xml
View File

@ -60,6 +60,12 @@
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version> <version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.20</version>
</dependency>
<!-- ehCAChe --> <!-- ehCAChe -->
<dependency> <dependency>
<groupId>javax.cache</groupId> <groupId>javax.cache</groupId>
@ -104,11 +110,8 @@
<groupId>org.glassfish.jersey.containers</groupId> <groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId> <artifactId>jersey-container-servlet</artifactId>
</dependency> </dependency>
<!-- Required with jersey 2.27 <!-- Required with jersey 2.27 <dependency> <groupId>org.glassfish.jersey.inject</groupId>
<dependency> <artifactId>jersey-hk2</artifactId> </dependency> -->
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency> -->
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>

View File

@ -67,7 +67,7 @@ public class Validator {
this.mapper = mapper; 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); ArrayNode extrasArrayOriginal = (ArrayNode) objectNode.get(CKANPackage.EXTRAS_KEY);
if(extrasArrayOriginal == null || extrasArrayOriginal.size()==0) { if(extrasArrayOriginal == null || extrasArrayOriginal.size()==0) {
@ -107,7 +107,10 @@ public class Validator {
String profileName = metadataTypeCF.getValue(); String profileName = metadataTypeCF.getValue();
// fetch the profile by metadata type specified above // fetch the profile by metadata type specified above
MetadataUtility metadataUtility = MetadataUtility.getInstance();
if(metadataUtility == null) {
metadataUtility = new MetadataUtility();
}
MetadataFormat profile = metadataUtility.getMetadataFormat(profileName); MetadataFormat profile = metadataUtility.getMetadataFormat(profileName);
if(profile == null) { if(profile == null) {
throw new BadRequestException("'" + CKANPackage.EXTRAS_KEY_VALUE_SYSTEM_TYPE + "' extra field's value ('" throw new BadRequestException("'" + CKANPackage.EXTRAS_KEY_VALUE_SYSTEM_TYPE + "' extra field's value ('"
@ -233,11 +236,11 @@ public class Validator {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void createGroupAsSysAdmin(String groupName) throws Exception { public void createGroupAsSysAdmin(String title) throws Exception {
String sysAdminAPI = CKANUtility.getSysAdminAPI(); String sysAdminAPI = CKANUtility.getSysAdminAPI();
CKANGroup ckanGroup = new CKANGroup(); CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(sysAdminAPI); ckanGroup.setApiKey(sysAdminAPI);
ckanGroup.setName(CKANGroup.getGroupName(groupName)); ckanGroup.setName(CKANGroup.getGroupName(title));
try { try {
ckanGroup.read(); ckanGroup.read();
} catch (WebApplicationException e) { } catch (WebApplicationException e) {
@ -250,7 +253,7 @@ public class Validator {
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);
}finally { }finally {
try { try {
addUserToGroupAsSysAdmin(groupName); addUserToGroupAsSysAdmin(title);
}catch (WebApplicationException e) { }catch (WebApplicationException e) {
throw e; throw e;
}catch (Exception e) { }catch (Exception e) {

View File

@ -14,8 +14,10 @@ import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; 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/"; public final static String CKAN_API_PATH = "/api/3/action/";
// ckan header authorization // 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}$"; public final static String NAME_REGEX = "^[a-z0-9_\\\\-]{2,100}$";
@ -82,6 +84,12 @@ public abstract class CKAN {
protected String nameRegex; protected String nameRegex;
protected UriInfo uriInfo;
public void setUriInfo(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
public String getApiKey() { public String getApiKey() {
if(apiKey == null) { if(apiKey == null) {
try { try {
@ -253,11 +261,16 @@ public abstract class CKAN {
return result; return result;
} }
protected GXHTTPStringRequest getGXHTTPStringRequest(String path) throws UnsupportedEncodingException { protected GXHTTPStringRequest getGXHTTPStringRequest(String path, boolean post) throws UnsupportedEncodingException {
String catalogueURL = dataCatalogue.getCatalogueUrl(); String catalogueURL = dataCatalogue.getCatalogueUrl();
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(catalogueURL); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(catalogueURL);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME); 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.isExternalCall(true);
gxhttpStringRequest.header(AUTH_CKAN_HEADER, getApiKey()); gxhttpStringRequest.header(AUTH_CKAN_HEADER, getApiKey());
gxhttpStringRequest.path(path); gxhttpStringRequest.path(path);
@ -286,7 +299,7 @@ public abstract class CKAN {
protected String sendGetRequest(String path, Map<String,String> parameters) { protected String sendGetRequest(String path, Map<String,String> parameters) {
try { try {
logger.debug("Going to send GET request with parameters {}", parameters); logger.debug("Going to send GET request with parameters {}", parameters);
GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path); GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path, false);
gxhttpStringRequest.queryParams(parameters); gxhttpStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
return getResultAsString(httpURLConnection); return getResultAsString(httpURLConnection);
@ -300,7 +313,7 @@ public abstract class CKAN {
protected String sendPostRequest(String path, String body) { protected String sendPostRequest(String path, String body) {
try { try {
logger.debug("Going to send POST request with body {}", body); logger.debug("Going to send POST request with body {}", body);
GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path); GXHTTPStringRequest gxhttpStringRequest = getGXHTTPStringRequest(path, true);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body);
return getResultAsString(httpURLConnection); return getResultAsString(httpURLConnection);
} catch(WebApplicationException e) { } catch(WebApplicationException e) {

View File

@ -3,6 +3,8 @@ package org.gcube.gcat.persistence.ckan;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
/** /**
@ -37,7 +39,8 @@ public class CKANGroup extends CKAN {
} }
public static String getGroupName(String name) { 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 { public String create() throws WebApplicationException {

View File

@ -1,5 +1,8 @@
package org.gcube.gcat.persistence.ckan; package org.gcube.gcat.persistence.ckan;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@ -13,4 +16,34 @@ public class CKANLicense extends CKAN {
LIST = LICENSES_LIST; 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;
}
}
} }

View File

@ -14,11 +14,13 @@ import javax.ws.rs.NotAllowedException;
import javax.ws.rs.OPTIONS; import javax.ws.rs.OPTIONS;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.oldutils.Validator; import org.gcube.gcat.oldutils.Validator;
import org.gcube.gcat.profile.MetadataUtility; import org.gcube.gcat.profile.MetadataUtility;
import org.gcube.gcat.social.SocialService; 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 PRIVATE_KEY = "private";
protected static final String SEARCHABLE_KEY = "searchable"; 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_PRIVATE_KEY = "include_private";
// protected static final String INCLUDE_DRAFTS_KEY = "include_drafts"; // 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 GROUPS_KEY = "groups";
public static final String TAGS_KEY = "tags"; public static final String TAGS_KEY = "tags";
protected SocialService socialService;
protected final List<CKANResource> managedResources; protected final List<CKANResource> managedResources;
protected String itemID; protected String itemID;
@ -148,10 +153,29 @@ public class CKANPackage extends CKAN {
if(licenseId == null || licenseId.isEmpty()) { if(licenseId == null || licenseId.isEmpty()) {
throw new BadRequestException( throw new BadRequestException(
"You must specify a license identifier for the item. License list can be retrieved using licence collection"); "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(); if(objectNode.has(CAPACITY_KEY)) {
objectNode.put(AUTHOR_KEY, userJsonNode.get(CKANUser.NAME).asText()); /*
* 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()); objectNode.put(AUTHOR_EMAIL_KEY, userJsonNode.get(CKANUser.EMAIL).asText());
// owner organization must be specified if the token belongs to a VRE // owner organization must be specified if the token belongs to a VRE
@ -179,16 +203,17 @@ public class CKANPackage extends CKAN {
} }
} }
} else { } else {
gotOrganization = organizationFromContext;
objectNode.put(OWNER_ORG_KEY, organizationFromContext); objectNode.put(OWNER_ORG_KEY, organizationFromContext);
} }
} else { } 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) { if(gotOrganization == null) {
throw new BadRequestException("You must specify an Organization usign " + OWNER_ORG_KEY + " field"); throw new BadRequestException("You must specify an Organization usign " + OWNER_ORG_KEY + " field");
} }
} }
CKANUtility.addUserToOrganization(ckanUsername, gotOrganization, CKANUtility.MEMBER_ROLE, false);
return objectNode; return objectNode;
} }
@ -198,10 +223,10 @@ public class CKANPackage extends CKAN {
ObjectNode objectNode = checkBaseInformation(json); ObjectNode objectNode = checkBaseInformation(json);
// Validating against profiles if any // Validating against profiles if any
MetadataUtility metadataUtility = MetadataUtility.getInstance(); MetadataUtility metadataUtility = new MetadataUtility();
if(!metadataUtility.getMetadataProfiles().isEmpty()) { if(!metadataUtility.getMetadataProfiles().isEmpty()) {
Validator validator = new Validator(mapper); Validator validator = new Validator(mapper);
objectNode = validator.validateAgainstProfile(objectNode); objectNode = validator.validateAgainstProfile(objectNode, metadataUtility);
} }
return objectNode; return objectNode;
@ -314,6 +339,33 @@ public class CKANPackage extends CKAN {
return catalogueItemURL; return catalogueItemURL;
} }
protected void sendSocialPost(String title, String catalogueItemURL) {
try {
boolean socialPost = true;
try {
MultivaluedMap<String, String> 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 // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create
@Override @Override
public String create(String json) { public String create(String json) {
@ -328,7 +380,12 @@ public class CKANPackage extends CKAN {
((ObjectNode) jsonNode).remove(RESOURCES_KEY); ((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)); super.create(getAsString(jsonNode));
@ -342,9 +399,9 @@ public class CKANPackage extends CKAN {
// Actions performed after a package has been correctly created on ckan. // Actions performed after a package has been correctly created on ckan.
String title = result.get(TITLE_KEY).asText(); String title = result.get(TITLE_KEY).asText();
ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY); if(scopeBean.is(Type.VRE)) {
SocialService packagePostActions = new SocialService(catalogueItemURL, name, arrayNode, title); sendSocialPost(title, catalogueItemURL);
packagePostActions.start(); }
return getAsString(result); return getAsString(result);
} catch(WebApplicationException e) { } catch(WebApplicationException e) {

View File

@ -16,6 +16,8 @@ import javax.ws.rs.PUT;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import org.apache.commons.io.FilenameUtils; 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.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;
import org.gcube.gcat.utils.ContextUtility; 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 RESOURCES_KEY = "resources";
private static final String PACKAGE_ID_KEY = "package_id"; 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 MIME_TYPE_KEY = "mimetype";
private static final String REVISION_ID_KEY = "revision_id"; 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 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 */ /* 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_PROD = "data.d4science.org";
private static final String URI_RESOLVER_STORAGE_HUB_HOST_DEV = "data1-d.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 { } else {
URI_RESOLVER_STORAGE_HUB_HOST = URI_RESOLVER_STORAGE_HUB_HOST_PROD; 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 */ /* TODO END Code to be Removed */
@ -86,6 +94,7 @@ public class CKANResource extends CKAN {
protected URL persistedURL; protected URL persistedURL;
protected String mimeType; protected String mimeType;
protected String originalFileExtension;
protected JsonNode previousRepresentation; protected JsonNode previousRepresentation;
@ -156,6 +165,32 @@ public class CKANResource extends CKAN {
return getAsString(resources); 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<String> 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) { protected ObjectNode persistStorageFile(ObjectNode objectNode) {
if(objectNode.has(URL_KEY)) { if(objectNode.has(URL_KEY)) {
@ -176,6 +211,13 @@ public class CKANResource extends CKAN {
if(mimeType != null) { if(mimeType != null) {
objectNode.put(MIME_TYPE_KEY, mimeType); 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()); objectNode.put(URL_KEY, url.toString());
@ -269,7 +311,9 @@ public class CKANResource extends CKAN {
storageHubManagement = new CatalogueStorageHubManagement(); storageHubManagement = new CatalogueStorageHubManagement();
try { try {
persistedURL = storageHubManagement.ensureResourcePersistence(persistedURL, itemID, resourceID); 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(); mimeType = storageHubManagement.getMimeType();
persisted = true; persisted = true;
} catch(Exception e) { } catch(Exception e) {
@ -336,7 +380,7 @@ public class CKANResource extends CKAN {
return sendGetRequest(READ, getMapWithID(resourceID)); return sendGetRequest(READ, getMapWithID(resourceID));
} }
protected String update(JsonNode jsonNode) { protected String update(JsonNode jsonNode) throws Exception {
ObjectNode resourceNode = (ObjectNode) jsonNode; ObjectNode resourceNode = (ObjectNode) jsonNode;
// This cannot be moved outside otherwise we don't // This cannot be moved outside otherwise we don't
resourceNode = validate(resourceNode); resourceNode = validate(resourceNode);
@ -368,8 +412,14 @@ public class CKANResource extends CKAN {
@Override @Override
public String update(String json) { public String update(String json) {
try {
JsonNode jsonNode = getAsJsonNode(json); JsonNode jsonNode = getAsJsonNode(json);
return update(jsonNode); return update(jsonNode);
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new WebApplicationException(e);
}
} }
@Override @Override
@ -406,7 +456,13 @@ public class CKANResource extends CKAN {
public JsonNode createOrUpdate(JsonNode jsonNode) { public JsonNode createOrUpdate(JsonNode jsonNode) {
ObjectNode resourceNode = (ObjectNode) jsonNode; ObjectNode resourceNode = (ObjectNode) jsonNode;
if(resourceNode.has(ID_KEY)) { if(resourceNode.has(ID_KEY)) {
try {
update(resourceNode); update(resourceNode);
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new WebApplicationException(e);
}
} else { } else {
create(resourceNode); create(resourceNode);
} }
@ -427,7 +483,13 @@ public class CKANResource extends CKAN {
public void rollback() { public void rollback() {
if(previousRepresentation != null) { if(previousRepresentation != null) {
try {
update(previousRepresentation); update(previousRepresentation);
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new WebApplicationException(e);
}
} else { } else {
delete(); delete();
} }

View File

@ -49,11 +49,15 @@ public class CKANUtility {
return getCKANUsername(ContextUtility.getUsername()); return getCKANUsername(ContextUtility.getUsername());
} }
public static JsonNode getCKANUser() { public static JsonNode getCKANUser(boolean addToOrganization) {
return createCKANUser(getCKANUsername()); 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); ckanUsername = CKANUtility.getCKANUsername(ckanUsername);
CKANUser ckanUser = new CKANUser(); CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(getSysAdminAPI()); ckanUser.setApiKey(getSysAdminAPI());
@ -69,18 +73,24 @@ public class CKANUtility {
} }
} }
JsonNode jsonNode = ckanUser.getJsonNodeResult(); JsonNode jsonNode = ckanUser.getJsonNodeResult();
if(addToOrganization) {
addUserToOrganization(ckanUsername, MEMBER_ROLE, false); addUserToOrganization(ckanUsername, MEMBER_ROLE, false);
}
return jsonNode; 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 ckanOrganization = new CKANOrganization();
ckanOrganization.setApiKey(getSysAdminAPI()); ckanOrganization.setApiKey(getSysAdminAPI());
String organizationName = CKANOrganization.getCKANOrganizationName();
ckanOrganization.setName(organizationName); ckanOrganization.setName(organizationName);
ckanOrganization.addUserToOrganisation(ckanUsername, role, force); 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 { public static String getApiKey() throws Exception {
String ckanUsername = getCKANUsername(); String ckanUsername = getCKANUsername();
return getApiKey(ckanUsername); return getApiKey(ckanUsername);
@ -90,7 +100,7 @@ public class CKANUtility {
try { try {
String apiKey = getCatalogue().getApiKeyFromUsername(ckanUsername); String apiKey = getCatalogue().getApiKeyFromUsername(ckanUsername);
if(apiKey == null) { if(apiKey == null) {
JsonNode jsonNode = createCKANUser(ckanUsername); JsonNode jsonNode = createCKANUser(ckanUsername, true);
apiKey = jsonNode.get(API_KEY).asText(); apiKey = jsonNode.get(API_KEY).asText();
} }
return apiKey; return apiKey;

View File

@ -4,6 +4,7 @@ import java.io.StringWriter;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
@ -23,6 +24,7 @@ import org.json.JSONObject;
import org.json.XML; import org.json.XML;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
@ -47,7 +49,7 @@ public class ISProfile {
ArrayNode arrayNode = mapper.createArrayNode(); ArrayNode arrayNode = mapper.createArrayNode();
try { try {
Set<String> names = MetadataUtility.getInstance().getProfilesNames(); Set<String> names = (new MetadataUtility()).getProfilesNames();
for(String name : names) { for(String name : names) {
arrayNode.add(name); arrayNode.add(name);
} }
@ -134,7 +136,7 @@ public class ISProfile {
public String read(String name, boolean xml) { public String read(String name, boolean xml) {
try { try {
String profile = MetadataUtility.getInstance().getMetadataFormat(name).getMetadataSource(); String profile = (new MetadataUtility()).getMetadataFormat(name).getMetadataSource();
if(profile != null) { if(profile != null) {
if(xml) { if(xml) {
return profile; return profile;
@ -156,8 +158,8 @@ public class ISProfile {
public boolean createOrUpdate(String name, String xml) { public boolean createOrUpdate(String name, String xml) {
try { try {
MetadataUtility metadataUtility = MetadataUtility.getInstance(); MetadataUtility metadataUtility = new MetadataUtility();
metadataUtility.getDataCalogueMetadataFormatReader().validateProfile(xml); metadataUtility.validateProfile(xml);
if(metadataUtility.getMetadataFormat(name) == null) { if(metadataUtility.getMetadataFormat(name) == null) {
createGenericResource(name, xml); createGenericResource(name, xml);
return true; return true;
@ -167,18 +169,20 @@ public class ISProfile {
} }
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch (SAXException e) {
throw new BadRequestException(e);
} catch(Exception e) { } catch(Exception e) {
throw new InternalServerErrorException(e.getMessage()); throw new InternalServerErrorException(e);
} finally { } finally {
// TOOD Actually Cache has been removed. Remove the following code if it will not be re-introduced // TOOD Actually Cache has been removed. Remove the following code if it will not be re-introduced
// Cleaning the cache // Cleaning the cache
MetadataUtility.clearCache(); // MetadataUtility.clearCache();
} }
} }
public boolean delete(String name) { public boolean delete(String name) {
try { try {
MetadataUtility metadataUtility = MetadataUtility.getInstance(); MetadataUtility metadataUtility = new MetadataUtility();
if(metadataUtility.getMetadataFormat(name) == null) { if(metadataUtility.getMetadataFormat(name) == null) {
throw new NotFoundException("Profile with name " + name + " not found"); throw new NotFoundException("Profile with name " + name + " not found");
} else { } else {
@ -191,7 +195,7 @@ public class ISProfile {
throw new InternalServerErrorException(e.getMessage()); throw new InternalServerErrorException(e.getMessage());
} finally { } finally {
// Cleaning the cache // Cleaning the cache
MetadataUtility.clearCache(); // MetadataUtility.clearCache();
} }
} }
} }

View File

@ -5,8 +5,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader; import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
@ -21,52 +19,12 @@ public class MetadataUtility {
*/ */
private Map<String,MetadataProfile> metadataProfiles; private Map<String,MetadataProfile> metadataProfiles;
private static DataCalogueMetadataFormatReader getDataCalogueMetadataFormatReaderInstance() throws Exception { public MetadataUtility() throws Exception{
/* dataCalogueMetadataFormatReader = new DataCalogueMetadataFormatReader();
Cache<String,DataCalogueMetadataFormatReader> 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 static void clearCache() { public void validateProfile(String xmlProfile) throws Exception {
/* dataCalogueMetadataFormatReader.validateProfile(xmlProfile);
Cache<String,DataCalogueMetadataFormatReader> readerCache = CachesManager.getReaderCache();
readerCache.clear();
*/
}
private MetadataUtility() throws Exception{
dataCalogueMetadataFormatReader = getDataCalogueMetadataFormatReaderInstance();
}
private static final InheritableThreadLocal<MetadataUtility> metadataUtility = new InheritableThreadLocal<MetadataUtility>() {
@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 Map<String, MetadataProfile> getMetadataProfiles() throws Exception{ public Map<String, MetadataProfile> getMetadataProfiles() throws Exception{

View File

@ -13,7 +13,7 @@ public class BaseREST {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Context @Context
private UriInfo uriInfo; protected UriInfo uriInfo;
protected static final String LOCATION_HEADER = "Location"; protected static final String LOCATION_HEADER = "Location";

View File

@ -30,7 +30,7 @@ public class Namespace extends BaseREST implements org.gcube.gcat.api.interfaces
ArrayNode arrayNode = mapper.createArrayNode(); ArrayNode arrayNode = mapper.createArrayNode();
try { try {
List<NamespaceCategory> namespaces = MetadataUtility.getInstance().getNamespaceCategories(); List<NamespaceCategory> namespaces = (new MetadataUtility()).getNamespaceCategories();
for(NamespaceCategory namespaceCategory : namespaces) { for(NamespaceCategory namespaceCategory : namespaces) {
ObjectNode namespace = mapper.createObjectNode(); ObjectNode namespace = mapper.createObjectNode();
namespace.put("id", namespaceCategory.getId()); namespace.put("id", namespaceCategory.getId());

View File

@ -10,13 +10,13 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.profile.ISProfile; 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(); ISProfile isProfile = new ISProfile();
ArrayNode arrayNode = isProfile.list(); ArrayNode arrayNode = isProfile.list();
return isProfile.getMapper().writeValueAsString(arrayNode); return isProfile.getMapper().writeValueAsString(arrayNode);
} catch(WebServiceException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);
@ -64,7 +64,7 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P
xml = true; xml = true;
} }
return isProfile.read(name, xml); return isProfile.read(name, xml);
} catch(WebServiceException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);
@ -89,7 +89,7 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P
} }
responseBuilder.entity(xml); responseBuilder.entity(xml);
return responseBuilder.type(MediaType.APPLICATION_XML).build(); return responseBuilder.type(MediaType.APPLICATION_XML).build();
} catch(WebServiceException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new InternalServerErrorException(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 isProfile = new ISProfile();
isProfile.delete(name); isProfile.delete(name);
return Response.status(Status.NO_CONTENT).build(); return Response.status(Status.NO_CONTENT).build();
} catch(WebServiceException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);

View File

@ -40,6 +40,7 @@ public class REST<C extends CKAN> extends BaseREST implements CRUD<Response, Res
public Response create(String json) { public Response create(String json) {
setCalledMethod("POST /" + COLLECTION_PARAMETER); setCalledMethod("POST /" + COLLECTION_PARAMETER);
C ckan = getInstance(); C ckan = getInstance();
ckan.setUriInfo(uriInfo);
String ret = ckan.create(json); String ret = ckan.create(json);
ResponseBuilder responseBuilder = Response.status(Status.CREATED).entity(ret); ResponseBuilder responseBuilder = Response.status(Status.CREATED).entity(ret);

View File

@ -18,6 +18,13 @@ public class ScienceCatalogueExceptionMapper implements ExceptionMapper<Excepti
Status status = Status.INTERNAL_SERVER_ERROR; Status status = Status.INTERNAL_SERVER_ERROR;
String exceptionMessage = exception.getMessage(); String exceptionMessage = exception.getMessage();
try {
if(exception.getCause()!=null) {
exceptionMessage = exception.getCause().getMessage();
}
}catch (Exception e) {
exceptionMessage = exception.getMessage();
}
MediaType mediaType = MediaType.TEXT_PLAIN_TYPE; MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;

View File

@ -17,6 +17,8 @@ import org.slf4j.LoggerFactory;
* @author Costantino Perciante (ISTI - CNR) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
// TODO Add a context based cache
public class GcoreEndpointReaderSNL { public class GcoreEndpointReaderSNL {
private static final String RESOURCE = "jersey-servlet"; private static final String RESOURCE = "jersey-servlet";

View File

@ -1,12 +1,19 @@
package org.gcube.gcat.social; package org.gcube.gcat.social;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.cache.Cache; import javax.cache.Cache;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
@ -36,8 +43,9 @@ public class SocialService extends Thread {
// https://wiki.gcube-system.org/gcube/Social_Networking_Service // https://wiki.gcube-system.org/gcube/Social_Networking_Service
protected static final String SOCIAL_SERVICE_GET_USER_INFO_PATH = "2/users/get-profile"; protected static final String SOCIAL_SERVICE_GET_USER_INFO_PATH = "2/users/get-profile";
// String.format(NOTIFICATION_MESSAGE, title, fullName, url) // String.format(NOTIFICATION_MESSAGE, fullName, title, url)
protected static final String NOTIFICATION_MESSAGE = "Dear members,<br>The item '%s' has been just published by %s.<br>You can find it at: %s <br>"; protected static final String NOTIFICATION_MESSAGE = "%s just published the item \"%s\"\n" +
"Please find it at %s\n";
protected static final String RESULT_KEY = "result"; protected static final String RESULT_KEY = "result";
protected static final String FULLNAME_IN_PROFILE_KEY = "fullname"; protected static final String FULLNAME_IN_PROFILE_KEY = "fullname";
@ -46,41 +54,67 @@ public class SocialService extends Thread {
protected static final String SOCIAL_POST_RESPONSE_SUCCESS_KEY = "success"; protected static final String SOCIAL_POST_RESPONSE_SUCCESS_KEY = "success";
protected static final String SOCIAL_POST_RESPONSE_MESSAGE_KEY = "message"; protected static final String SOCIAL_POST_RESPONSE_MESSAGE_KEY = "message";
protected String id; protected final GcoreEndpointReaderSNL gcoreEndpointReaderSNL;
protected String url;
protected String title;
protected List<String> tags;
protected final GcoreEndpointReaderSNL socialService;
protected final ObjectMapper objectMapper; protected final ObjectMapper objectMapper;
protected String itemID;
protected String itemURL;
protected String itemTitle;
protected List<String> tags;
protected JsonNode gCubeUserProfile;
public SocialService() throws Exception { public SocialService() throws Exception {
super(); super();
this.socialService = new GcoreEndpointReaderSNL(); this.gcoreEndpointReaderSNL = new GcoreEndpointReaderSNL();
this.objectMapper = new ObjectMapper(); this.objectMapper = new ObjectMapper();
} }
public SocialService(String id, String url, List<String> tags, String title) throws Exception { public String getItemID() {
this(); return itemID;
this.id = id; }
this.url = url;
public void setItemID(String itemID) {
this.itemID = itemID;
}
public String getItemURL() {
return itemURL;
}
public void setItemURL(String itemURL) {
this.itemURL = itemURL;
}
public String getItemTitle() {
return itemTitle;
}
public void setItemTitle(String itemTitle) {
this.itemTitle = itemTitle;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags; this.tags = tags;
this.title = title;
} }
public SocialService(String id, String url, ArrayNode arrayNode, String title) throws Exception { public void setTags(ArrayNode tags) {
this();
this.id = id;
this.url = url;
this.tags = new ArrayList<>(); this.tags = new ArrayList<>();
if(arrayNode != null && arrayNode.size() > 0) { if(tags != null && tags.size() > 0) {
for(int i = 0; i < arrayNode.size(); i++) { for(int i = 0; i < tags.size(); i++) {
String tagName = arrayNode.get(i).get("display_name").asText(); JsonNode jsonNode = tags.get(i);
tags.add(tagName); 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 * @throws Exception
*/ */
public JsonNode getGCubeUserProfile() throws Exception { public JsonNode getGCubeUserProfile() throws Exception {
if(gCubeUserProfile==null) {
String username = ContextUtility.getUsername(); String username = ContextUtility.getUsername();
return getGCubeUserProfile(username); 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<String,JsonNode> userCache = CachesManager.getUserCache(); Cache<String,JsonNode> userCache = CachesManager.getUserCache();
if(userCache.containsKey(username)) if(userCache.containsKey(username))
return userCache.get(username); return userCache.get(username);
else { else {
String socialServiceBasePath = socialService.getServiceBasePath(); String socialServiceBasePath = gcoreEndpointReaderSNL.getServiceBasePath();
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(socialServiceBasePath); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(socialServiceBasePath);
gxhttpStringRequest.header("User-Agent", Constants.CATALOGUE_NAME); gxhttpStringRequest.header("User-Agent", Constants.CATALOGUE_NAME);
@ -107,22 +171,28 @@ public class SocialService extends Thread {
gxhttpStringRequest.path(SOCIAL_SERVICE_GET_USER_INFO_PATH); gxhttpStringRequest.path(SOCIAL_SERVICE_GET_USER_INFO_PATH);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); 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); userCache.put(username, jsonNode);
return jsonNode; return jsonNode;
} }
} }
public String getFullName() throws Exception { public String getFullName() throws Exception {
try {
if(!ContextUtility.isApplication()) { if(!ContextUtility.isApplication()) {
JsonNode jsonNode = getGCubeUserProfile(); JsonNode jsonNode = getGCubeUserProfile();
JsonNode result = jsonNode.get(RESULT_KEY); JsonNode result = jsonNode.get(RESULT_KEY);
return result.get(FULLNAME_IN_PROFILE_KEY).asText(); return result.get(FULLNAME_IN_PROFILE_KEY).asText();
} else { }
}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(); return ContextUtility.getUsername();
} }
}
@Override @Override
public void run() { public void run() {
@ -134,7 +204,7 @@ public class SocialService extends Thread {
logger.info("Social Post are disabled in the context {}", ContextUtility.getCurrentContext()); logger.info("Social Post are disabled in the context {}", ContextUtility.getCurrentContext());
return; 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(); boolean notifyUsers = dataCatalogue.isNotificationToUsersEnabled();
// write notification post // write notification post
@ -150,7 +220,7 @@ public class SocialService extends Thread {
try { try {
String fullName = getFullName(); String fullName = getFullName();
String basePath = socialService.getServiceBasePath(); String basePath = gcoreEndpointReaderSNL.getServiceBasePath();
if(basePath == null) { if(basePath == null) {
logger.info("Unable to write a post because there is no social networking service available"); logger.info("Unable to write a post because there is no social networking service available");
return; return;
@ -158,7 +228,7 @@ public class SocialService extends Thread {
basePath = basePath.endsWith("/") ? basePath : basePath + "/"; basePath = basePath.endsWith("/") ? basePath : basePath + "/";
StringWriter messageWriter = new StringWriter(); 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) { for(String tag : tags) {
tag = tag.trim(); tag = tag.trim();
@ -168,10 +238,11 @@ public class SocialService extends Thread {
} }
messageWriter.append("#"); messageWriter.append("#");
messageWriter.append(tag); messageWriter.append(tag);
messageWriter.append(" ");
} }
String message = messageWriter.toString(); 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 objectNode = objectMapper.createObjectNode();
objectNode.put(SOCIAL_POST_TEXT_KEY, message); objectNode.put(SOCIAL_POST_TEXT_KEY, message);
@ -182,13 +253,14 @@ public class SocialService extends Thread {
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); 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.setSecurityToken(Constants.getCatalogueApplicationToken());
gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH); gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode)); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode));
String ret = httpURLConnection.getResponseMessage(); String ret = getResultAsString(httpURLConnection);
JsonNode jsonNode = objectMapper.readTree(ret); JsonNode jsonNode = objectMapper.readTree(ret);
if(jsonNode.get(SOCIAL_POST_RESPONSE_SUCCESS_KEY).asBoolean()) { if(jsonNode.get(SOCIAL_POST_RESPONSE_SUCCESS_KEY).asBoolean()) {
logger.info("Post written : {}", message); logger.info("Post written : {}", message);

View File

@ -8,6 +8,8 @@ import java.net.HttpURLConnection;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException; 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.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; 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 CATALOGUE_CONTEXT = "gcube_scope";
private static final String ENTITY_TYPE = "entity_context"; private static final String ENTITY_TYPE = "entity_context";
private static final String ENTITY_NAME = "entity_name"; 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"; private static final String CATALOGUE_PLAIN_URL = "clear_url";
protected ObjectMapper mapper; protected ObjectMapper mapper;
@ -48,13 +54,18 @@ public class URIResolver {
ObjectNode requestContent = mapper.createObjectNode(); ObjectNode requestContent = mapper.createObjectNode();
requestContent.put(CATALOGUE_CONTEXT, ContextUtility.getCurrentContext()); 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); 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)); requestContent.put(CATALOGUE_PLAIN_URL, String.valueOf(true));
*/
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(uriResolverURL); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(uriResolverURL);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME); 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); gxhttpStringRequest.isExternalCall(true);
String body = mapper.writeValueAsString(requestContent); String body = mapper.writeValueAsString(requestContent);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body);

View File

@ -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(); FileContainer fileContainer = storageHubManagement.getCreatedFile();
Metadata metadata = fileContainer.get().getMetadata(); Metadata metadata = fileContainer.get().getMetadata();
Map<String,Object> map = metadata.getMap(); Map<String,Object> map = metadata.getMap();
@ -84,7 +84,7 @@ public class CatalogueStorageHubManagement {
fileContainer.setMetadata(metadata); 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()); ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
try { try {
applicationMode.start(); 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()); ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
try { try {
applicationMode.start(); applicationMode.start();

View File

@ -44,7 +44,8 @@ public class ContextTest {
throw new RuntimeException(e); 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 { public static String getCurrentScope(String token) throws ObjectNotFound, Exception {

View File

@ -1,5 +1,11 @@
package org.gcube.gcat.persistence.ckan; 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.ContextTest;
import org.gcube.gcat.persistence.ckan.CKANLicense; import org.gcube.gcat.persistence.ckan.CKANLicense;
import org.junit.Assert; import org.junit.Assert;
@ -18,11 +24,27 @@ public class CKANLicenseTest extends ContextTest {
@Test @Test
public void list() throws Exception { public void list() throws Exception {
CKANLicense license = new CKANLicense(); CKANLicense license = new CKANLicense();
String ret = license.list(-1,-1); license.list(-1,-1);
ObjectMapper mapper = new ObjectMapper(); JsonNode gotList = license.getJsonNodeResult();
JsonNode gotList = mapper.readTree(ret);
Assert.assertTrue(gotList instanceof ArrayNode); Assert.assertTrue(gotList instanceof ArrayNode);
ObjectMapper mapper = new ObjectMapper();
logger.debug("List :\n{}", mapper.writeValueAsString(gotList)); 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<String> 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);
}
}
} }

View File

@ -20,4 +20,11 @@ public class CKANOrganizationTest extends ContextTest {
logger.debug("{}", ret); 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);
}
} }

View File

@ -27,7 +27,7 @@ public class CKANPackageTest extends ContextTest {
private static final String ITEM_NAME_VALUE = "restful_transaction_model"; 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 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 @Test
@ -89,18 +89,21 @@ public class CKANPackageTest extends ContextTest {
itemObjectNode.put(PRIVATE_KEY, false); itemObjectNode.put(PRIVATE_KEY, false);
itemObjectNode.put(NOTES_KEY, "A research of Luca Frosini"); itemObjectNode.put(NOTES_KEY, "A research of Luca Frosini");
itemObjectNode.put(URL_KEY, "http://www.d4science.org"); itemObjectNode.put(URL_KEY, "http://www.d4science.org");
itemObjectNode.put(CKANPackage.OWNER_ORG_KEY, "nextnext");
ArrayNode tagArrayNode = itemObjectNode.putArray(CKANPackage.TAGS_KEY); ArrayNode tagArrayNode = itemObjectNode.putArray(CKANPackage.TAGS_KEY);
ObjectNode tagNode = mapper.createObjectNode(); ObjectNode tagNode = mapper.createObjectNode();
tagNode.put(CKANPackage.NAME_KEY, "REST"); tagNode.put(CKANPackage.NAME_KEY, "REST");
tagArrayNode.add(tagNode); tagArrayNode.add(tagNode);
/*
ArrayNode resourceArrayNode = itemObjectNode.putArray(CKANPackage.RESOURCES_KEY); ArrayNode resourceArrayNode = itemObjectNode.putArray(CKANPackage.RESOURCES_KEY);
ObjectNode resourceNode = mapper.createObjectNode(); ObjectNode resourceNode = mapper.createObjectNode();
resourceNode.put(CKANResource.NAME_KEY, "RESTful Transaction Model"); resourceNode.put(CKANResource.NAME_KEY, "RESTful Transaction Model");
// Workspace(luca.frosini) > RESTful Transaction Model v 1.0.pdf // Workspace(luca.frosini) > RESTful Transaction Model v 1.0.pdf
resourceNode.put(CKANResource.URL_KEY, "https://goo.gl/J8AwQW"); resourceNode.put(CKANResource.URL_KEY, "https://goo.gl/J8AwQW");
resourceArrayNode.add(resourceNode); resourceArrayNode.add(resourceNode);
*/
ArrayNode extraArrayNode = itemObjectNode.putArray(CKANPackage.EXTRAS_KEY); ArrayNode extraArrayNode = itemObjectNode.putArray(CKANPackage.EXTRAS_KEY);
ObjectNode typeNode = mapper.createObjectNode(); ObjectNode typeNode = mapper.createObjectNode();
@ -110,7 +113,7 @@ public class CKANPackageTest extends ContextTest {
ObjectNode modelNode = mapper.createObjectNode(); ObjectNode modelNode = mapper.createObjectNode();
modelNode.put(CKANPackage.EXTRAS_KEY_KEY, "test"); 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); extraArrayNode.add(modelNode);
ObjectNode populationNode = mapper.createObjectNode(); ObjectNode populationNode = mapper.createObjectNode();
@ -129,10 +132,20 @@ public class CKANPackageTest extends ContextTest {
@Test @Test
public void create() throws Exception { public void create() throws Exception {
//ContextTest.setContextByName("/gcube/devNext");
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
createPackage(mapper); 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 @Test
public void createReadUpdateUpdatePurge() throws Exception { public void createReadUpdateUpdatePurge() throws Exception {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();

View File

@ -6,6 +6,7 @@ import java.util.UUID;
import org.gcube.gcat.ContextTest; import org.gcube.gcat.ContextTest;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;
import org.gcube.storagehub.ApplicationMode; import org.gcube.storagehub.ApplicationMode;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -77,4 +78,22 @@ public class CKANResourceTest extends ContextTest {
ckanResource.delete(); 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);
}
} }

View File

@ -1,7 +1,11 @@
package org.gcube.gcat.social; 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.ContextTest;
import org.gcube.gcat.social.SocialService; import org.gcube.gcat.utils.Constants;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -21,4 +25,24 @@ public class SocialServiceTest extends ContextTest {
logger.debug("gCube User Profile is {}", objectMapper.writeValueAsString(jsonNode)); 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<String> tags = new ArrayList<>();
tags.add("Test");
tags.add("ThisIsATest");
tags.add("IgnoreIt");
socialService.setTags(tags);
socialService.sendSocialPost(false);
}
} }