Merged from private branch

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@166966 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-04-12 15:56:38 +00:00
parent 0a49656129
commit a881355db7
27 changed files with 1825 additions and 1856 deletions

View File

@ -6,6 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>

26
pom.xml
View File

@ -11,7 +11,7 @@
<groupId>org.gcube.data-catalogue</groupId> <groupId>org.gcube.data-catalogue</groupId>
<artifactId>catalogue-ws</artifactId> <artifactId>catalogue-ws</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>1.2.0-SNAPSHOT</version> <version>1.3.0-SNAPSHOT</version>
<name>catalogue-ws</name> <name>catalogue-ws</name>
<description> <description>
@ -22,7 +22,14 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.gcube.distribution</groupId> <groupId>org.gcube.distribution</groupId>
<artifactId>maven-smartgears-bom</artifactId> <artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>LATEST</version> <version>LATEST</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
@ -40,11 +47,19 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- ehCAChe -->
<dependency> <dependency>
<groupId>net.sf.ehcache</groupId> <groupId>javax.cache</groupId>
<artifactId>ehcache</artifactId> <artifactId>cache-api</artifactId>
<version>2.10.0</version> <version>1.0.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.5.2</version>
<scope>runtime</scope>
</dependency>
<!-- END ehCAChe -->
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId> <artifactId>ic-client</artifactId>
@ -103,6 +118,7 @@
<artifactId>jersey-media-multipart</artifactId> <artifactId>jersey-media-multipart</artifactId>
<version>${version.jersey}</version> <version>${version.jersey}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId> <artifactId>jersey-media-sse</artifactId>

View File

@ -6,36 +6,56 @@ import org.json.simple.JSONObject;
* A custom field bean. It also stores index of the category and of the metadata field associated. * A custom field bean. It also stores index of the category and of the metadata field associated.
* These are used to sort them before pushing the content to CKAN. * These are used to sort them before pushing the content to CKAN.
* If they are missing, indexes are set to Integer.MAX_VALUE. * If they are missing, indexes are set to Integer.MAX_VALUE.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class CustomField implements Comparable<CustomField>{ public class CustomField implements Comparable<CustomField> {
private String key; private String key;
private String qualifiedKey; private String qualifiedKey;
private String value; private String value;
private int indexMetadataField = Integer.MAX_VALUE;
private int indexCategory = Integer.MAX_VALUE; private int indexCategory = Integer.MAX_VALUE;
private int indexMetadataField = Integer.MAX_VALUE;
private void init(String key, String value, int indexCategory, int indexMetadataField) {
if(key == null || value == null || key.isEmpty()) {
throw new IllegalArgumentException(
"A custom field must have a key and a value! Provided values are " + key + "=" + value);
}
this.key = key;
this.qualifiedKey = key;
this.value = value;
this.indexMetadataField = indexMetadataField;
this.indexCategory = indexCategory;
if(this.indexCategory < 0) {
this.indexCategory = Integer.MAX_VALUE;
}
if(this.indexMetadataField < 0) {
this.indexMetadataField = Integer.MAX_VALUE;
}
}
public CustomField(JSONObject object) { public CustomField(JSONObject object) {
super(); super();
this.key = (String)object.get("key"); init((String) object.get("key"), (String) object.get("value"), -1, -1);
this.qualifiedKey = key;
this.value = (String)object.get("value");
if(key == null || value == null || key.isEmpty())
throw new IllegalArgumentException("A custom field must have a key and a value! Provided object is " + object.toString());
} }
/** /**
* @param key * @param key
* @param value * @param value
*/ */
public CustomField(String key, String value) { public CustomField(String key, String value) {
super(); super();
this.key = key; init(key, value, -1, -1);
this.qualifiedKey = key;
this.value = value;
} }
/** /**
* @param key * @param key
* @param value * @param value
@ -44,79 +64,71 @@ public class CustomField implements Comparable<CustomField>{
*/ */
public CustomField(String key, String value, int indexCategory, int indexMetadataField) { public CustomField(String key, String value, int indexCategory, int indexMetadataField) {
super(); super();
init(key, value, indexCategory, indexMetadataField);
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key; this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value; this.value = value;
this.indexMetadataField = indexMetadataField;
this.indexCategory = indexCategory;
this.qualifiedKey = key;
if(this.indexCategory < 0)
this.indexCategory = Integer.MAX_VALUE;
if(this.indexMetadataField < 0)
this.indexMetadataField = Integer.MAX_VALUE;
} }
public int getIndexMetadataField() {
return indexMetadataField;
}
public String getQualifiedKey() { public String getQualifiedKey() {
return qualifiedKey; return qualifiedKey;
} }
public void setQualifiedKey(String qualifiedKey) { public void setQualifiedKey(String qualifiedKey) {
this.qualifiedKey = qualifiedKey; this.qualifiedKey = qualifiedKey;
} }
public void setIndexMetadataField(int indexMetadataField) {
this.indexMetadataField = indexMetadataField;
if(this.indexMetadataField < 0)
this.indexMetadataField = Integer.MAX_VALUE;
}
public int getIndexCategory() { public int getIndexCategory() {
return indexCategory; return indexCategory;
} }
public void setIndexCategory(int indexCategory) { public void setIndexCategory(int indexCategory) {
this.indexCategory = indexCategory; this.indexCategory = indexCategory;
if(this.indexCategory < 0) if(this.indexCategory < 0)
this.indexCategory = Integer.MAX_VALUE; this.indexCategory = Integer.MAX_VALUE;
} }
public String getKey() { public int getIndexMetadataField() {
return key; return indexMetadataField;
} }
public void setKey(String key) { public void setIndexMetadataField(int indexMetadataField) {
this.key = key; this.indexMetadataField = indexMetadataField;
if(this.indexMetadataField < 0) {
this.indexMetadataField = Integer.MAX_VALUE;
}
} }
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override @Override
public String toString() { public String toString() {
return "CustomField [key=" + key + ", qualifiedKey=" + qualifiedKey return "CustomField [key=" + key + ", qualifiedKey=" + qualifiedKey + ", value=" + value
+ ", value=" + value + ", indexMetadataField=" + ", indexMetadataField=" + indexMetadataField + ", indexCategory=" + indexCategory + "]";
+ indexMetadataField + ", indexCategory=" + indexCategory + "]";
} }
@Override @Override
public int compareTo(CustomField o) { public int compareTo(CustomField o) {
if(this.indexCategory == o.indexCategory){ if(this.indexCategory == o.indexCategory) {
if(this.indexMetadataField == o.indexMetadataField) if(this.indexMetadataField == o.indexMetadataField) {
return 0; return 0;
else } else {
return this.indexMetadataField > o.indexMetadataField ? 1 : -1; return this.indexMetadataField > o.indexMetadataField ? 1 : -1;
} }
else } else {
return this.indexCategory > o.indexCategory ? 1 : -1; return this.indexCategory > o.indexCategory ? 1 : -1;
}
} }
} }

View File

@ -0,0 +1,73 @@
package org.gcube.datacatalogue.catalogue.entities;
import javax.ws.rs.core.MultivaluedMap;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.ContextUtils;
import org.gcube.datacatalogue.catalogue.utils.Delegator;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanDataset;
public class CatalogueItem {
private static final Logger logger = LoggerFactory.getLogger(CatalogueItem.class);
protected String id;
protected JSONObject item;
public CatalogueItem() {}
public CatalogueItem(String jsonString) throws ParseException {
JSONParser parser = new JSONParser();
this.item = (JSONObject) parser.parse(jsonString);
}
private void applicationChecks(String authorizationErroMessage) throws Exception {
if(ContextUtils.isApplication()) {
logger.debug("Application Token Request");
DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
CkanDataset dataset = dataCatalogue.getDataset(id, CatalogueUtils.fetchSysAPI());
String organization = CatalogueUtilMethods.getCKANOrganization();
if(organization.equalsIgnoreCase(dataset.getOrganization().getName())
&& ContextUtils.getUsername().equals(dataset.getAuthor())) {
return;
}
throw new Exception(authorizationErroMessage);
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String create() {
return null;
}
public String read() {
return Delegator.delegateGet(Constants.ITEM_SHOW, (MultivaluedMap<String,String>) null);
}
public String update() {
return null;
}
public String delete() {
return null;
}
}

View File

@ -1,33 +1,66 @@
package org.gcube.datacatalogue.catalogue.utils; package org.gcube.datacatalogue.catalogue.utils;
import net.sf.ehcache.Cache; import javax.cache.Cache;
import net.sf.ehcache.CacheManager; import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.spi.CachingProvider;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
import org.json.simple.JSONObject;
/** /**
* Handle caches via Ehcache stuff. * Handle caches via Ehcache stuff.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class CachesManager { public class CachesManager {
private static CacheManager cacheManager;
public static final CachesManager singleton = new CachesManager();
// the following caches are declared within the ehcache.xml (no default is available, so pay attention) // the following caches are declared within the ehcache.xml (no default is available, so pay attention)
public static final String PROFILES_READERS_CACHE = "profile_readers"; public static final String PROFILES_READERS_CACHE = "profile_readers";
public static final String PROFILES_USERS_CACHE = "profile_users"; public static final String PROFILES_USERS_CACHE = "profile_users";
private CachesManager(){ private static final CacheManager cacheManager;
cacheManager = CacheManager.newInstance(); private static final Cache<String, DataCalogueMetadataFormatReader> readerCache;
private static final Cache<String, JSONObject> userCache;
static {
CachingProvider provider = Caching.getCachingProvider();
cacheManager = provider.getCacheManager();
MutableConfiguration<String, DataCalogueMetadataFormatReader> readerConfiguration =
new MutableConfiguration<String, DataCalogueMetadataFormatReader>()
.setTypes(String.class, DataCalogueMetadataFormatReader.class)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
readerCache = cacheManager.createCache(PROFILES_READERS_CACHE, readerConfiguration);
MutableConfiguration<String, JSONObject> userConfiguration =
new MutableConfiguration<String, JSONObject>()
.setTypes(String.class, JSONObject.class)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
userCache = cacheManager.createCache(PROFILES_USERS_CACHE, userConfiguration);
} }
public static Cache getCache(String name){ private CachesManager() {}
return cacheManager.getCache(name);
public static Cache<String, DataCalogueMetadataFormatReader> getReaderCache() {
return readerCache;
} }
public static Cache<String, JSONObject> getUserCache() {
return userCache;
}
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
super.finalize(); super.finalize();
cacheManager.shutdown(); cacheManager.close();
} }
} }

View File

@ -4,17 +4,18 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.sf.ehcache.Cache; import javax.cache.Cache;
import net.sf.ehcache.Element; import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.gcube.common.authorization.library.ClientType; import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory;
@ -26,6 +27,7 @@ import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanGroup; import eu.trentorise.opendata.jackan.model.CkanGroup;
@ -33,35 +35,25 @@ import eu.trentorise.opendata.jackan.model.CkanOrganization;
/** /**
* Utils methods. * Utils methods.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class CatalogueUtils { public class CatalogueUtils {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CatalogueUtils.class); private static final Logger logger = LoggerFactory.getLogger(CatalogueUtils.class);
/** /**
* Retrieve an instance of the library for the current scope * Retrieve an instance of the library for the current scope
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static DataCatalogue getCatalogue() throws Exception{ public static DataCatalogue getCatalogue() throws Exception {
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
logger.debug("Discovering ckan instance into scope " + context); logger.debug("Discovering ckan instance in context {}", context);
return DataCatalogueFactory.getFactory().getUtilsPerScope(context); return DataCatalogueFactory.getFactory().getUtilsPerScope(context);
} }
/**
* Retrieve an instance of the library for the scope
* @param scope if it is null it is evaluated from the session
* @return
* @throws Exception
*/
public static CkanGroup createGroupAsSysAdmin(String title, String groupName, String description) throws Exception{
return getCatalogue().createGroup(groupName, title, description);
}
/** /**
* Get the group hierarchy * Get the group hierarchy
* @param groupName * @param groupName
@ -69,238 +61,228 @@ public class CatalogueUtils {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static List<String> getGroupHierarchyNames(String groupName, String username, boolean isApplication) throws Exception{ public static List<String> getGroupHierarchyNames(String groupName) throws Exception {
List<String> toReturn = new ArrayList<String>(); List<String> toReturn = new ArrayList<String>();
String apiKey = isApplication ? fetchSysAPI(ScopeProvider.instance.get()): getCatalogue().getApiKeyFromUsername(username); String apiKey = CatalogueUtils.getApiKey();
List<CkanGroup> ckanGroups = getCatalogue().getParentGroups(groupName, apiKey); List<CkanGroup> ckanGroups = getCatalogue().getParentGroups(groupName, apiKey);
if(ckanGroups != null && !ckanGroups.isEmpty()){ if(ckanGroups != null && !ckanGroups.isEmpty()) {
for (CkanGroup ckanGroup : ckanGroups) { for(CkanGroup ckanGroup : ckanGroups) {
toReturn.add(ckanGroup.getName()); toReturn.add(ckanGroup.getName());
} }
} }
return toReturn; return toReturn;
} }
private static DataCalogueMetadataFormatReader getDataCalogueMetadataFormatReader() throws Exception {
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 reader;
}
/** /**
* Returns the names of the metadata profiles in a given context * Returns the names of the metadata profiles in a given context
* @param context * @param context
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static List<String> getProfilesNames() throws Exception{ public static List<String> getProfilesNames() throws Exception {
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); DataCalogueMetadataFormatReader reader = getDataCalogueMetadataFormatReader();
String context = ScopeProvider.instance.get();
List<String> toReturn = new ArrayList<String>(); List<String> toReturn = new ArrayList<String>();
DataCalogueMetadataFormatReader reader;
if(profilesCache.isKeyInCache(context))
reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue();
else{
reader = new DataCalogueMetadataFormatReader();
profilesCache.put(new Element(context, reader));
}
List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles(); List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles();
if(listProfiles != null && !listProfiles.isEmpty()){ if(listProfiles != null && !listProfiles.isEmpty()) {
for (MetadataProfile profile : listProfiles) { for(MetadataProfile profile : listProfiles) {
toReturn.add(profile.getName()); toReturn.add(profile.getName());
} }
} }
return toReturn; return toReturn;
} }
/** /**
* Returns the source xml of the metadata profile (specified via name) in a given context * Returns the source xml of the metadata profile (specified via name) in a given context
* @param context * @param context
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static String getProfileSource(String profileName) throws Exception{ public static String getProfileSource(String profileName) throws Exception {
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); DataCalogueMetadataFormatReader reader = getDataCalogueMetadataFormatReader();
String context = ScopeProvider.instance.get();
DataCalogueMetadataFormatReader reader;
if(profilesCache.isKeyInCache(context))
reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue();
else{
reader = new DataCalogueMetadataFormatReader();
profilesCache.put(new Element(context, reader));
}
List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles(); List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles();
String xmlToReturn = null; String xmlToReturn = null;
if(listProfiles != null && !listProfiles.isEmpty()){ if(listProfiles != null && !listProfiles.isEmpty()) {
for (MetadataProfile profile : listProfiles) { for(MetadataProfile profile : listProfiles) {
if(profile.getName().equals(profileName)){ if(profile.getName().equals(profileName)) {
xmlToReturn = reader.getMetadataFormatForMetadataProfile(profile).getMetadataSource(); xmlToReturn = reader.getMetadataFormatForMetadataProfile(profile).getMetadataSource();
break; break;
} }
} }
} }
return xmlToReturn; return xmlToReturn;
} }
/** /**
* Returns the categories. * Returns the categories.
* @param context * @param context
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static List<NamespaceCategory> getNamespaceCategories() throws Exception{ public static List<NamespaceCategory> getNamespaceCategories() throws Exception {
DataCalogueMetadataFormatReader reader = getDataCalogueMetadataFormatReader();
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE);
String context = ScopeProvider.instance.get();
DataCalogueMetadataFormatReader reader;
if(profilesCache.isKeyInCache(context))
reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue();
else{
reader = new DataCalogueMetadataFormatReader();
profilesCache.put(new Element(context, reader));
}
return reader.getListOfNamespaceCategories(); return reader.getListOfNamespaceCategories();
} }
/** /**
* Returns the metadataform of the metadata profile (specified via name) in a given context * Returns the metadataform of the metadata profile (specified via name) in a given context
* @param context * @param context
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static MetadataFormat getMetadataProfile(String profileName) throws Exception{ public static MetadataFormat getMetadataProfile(String profileName) throws Exception {
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); DataCalogueMetadataFormatReader reader = getDataCalogueMetadataFormatReader();
String context = ScopeProvider.instance.get();
DataCalogueMetadataFormatReader reader;
if(profilesCache.isKeyInCache(context))
reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue();
else{
reader = new DataCalogueMetadataFormatReader();
profilesCache.put(new Element(context, reader));
}
List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles(); List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles();
if(listProfiles != null && !listProfiles.isEmpty()){ if(listProfiles != null && !listProfiles.isEmpty()) {
for (MetadataProfile profile : listProfiles) { for(MetadataProfile profile : listProfiles) {
if(profile.getName().equals(profileName)){ if(profile.getName().equals(profileName)) {
return reader.getMetadataFormatForMetadataProfile(profile); return reader.getMetadataFormatForMetadataProfile(profile);
} }
} }
} }
return null; return null;
} }
/** /**
* Create a string representing an error message on failure * Create a string representing an error message on failure
* @param errorMessage * @param errorMessage
* @return * @return
*/ */
public static String createJSONOnFailure(String errorMessage){ public static String createJSONOnFailure(String errorMessage) {
return createJSONObjectMin(false, errorMessage).toJSONString(); return createJSONObjectMin(false, errorMessage).toJSONString();
} }
/** /**
* JSONObject containing minimum information to be set * JSONObject containing minimum information to be set
* @return * @return
*/ */
public static JSONObject createJSONObjectMin(boolean success, String errorMessage){ public static JSONObject createJSONObjectMin(boolean success, String errorMessage) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE); obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE);
obj.put(Constants.SUCCESS_KEY, success); obj.put(Constants.SUCCESS_KEY, success);
if(errorMessage != null) if(errorMessage != null) {
obj.put(Constants.MESSAGE_ERROR_KEY, errorMessage); obj.put(Constants.MESSAGE_ERROR_KEY, errorMessage);
}
return obj; return obj;
} }
/** /**
* Check if the create-item request can be executed * Check if the create-item request can be executed
* @param username * @param username
* @param organization * @param organization
* @throws Exception * @throws Exception
*/ */
public static void checkRole(String username, String organization) throws Exception { public static void checkRole(String organization) throws Exception {
DataCatalogue dataCatalogue = getCatalogue();
DataCatalogue catalogue = getCatalogue();
// check organization exists // check organization exists
CkanOrganization org = catalogue.getOrganizationByName(organization); CkanOrganization org = dataCatalogue.getOrganizationByName(organization);
if(org == null) if(org == null) {
throw new Exception("It seems that an organization with name " + organization + " doesn't exist!"); throw new Exception("It seems that an organization with name " + organization + " doesn't exist!");
}
Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> rolesPerOrganization = catalogue.getUserRoleByOrganization(username, catalogue.getApiKeyFromUsername(username));
Map<String,Map<CkanOrganization,RolesCkanGroupOrOrg>> rolesPerOrganization = dataCatalogue
if(rolesPerOrganization.get(org.getName()).values().contains(RolesCkanGroupOrOrg.MEMBER)) .getUserRoleByOrganization(ContextUtils.getUsername(),
throw new Exception("It seems you are neither Catalogue-Admin nor Catalogue-Editor in organization " + organization + "!"); dataCatalogue.getApiKeyFromUsername(ContextUtils.getUsername()));
if(rolesPerOrganization.get(org.getName()).values().contains(RolesCkanGroupOrOrg.MEMBER)) {
throw new Exception("It seems you are neither Catalogue-Admin nor Catalogue-Editor in organization "
+ organization + "!");
}
} }
/** /**
* Execute the GET http request at this url, and return the result as string * Execute the GET http request at this url, and return the result as string
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static JSONObject getUserProfile(String userId) throws Exception{ public static JSONObject getUserProfile() throws Exception {
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_USERS_CACHE); ClientInfo clientInfo = AuthorizationProvider.instance.get().getClient();
String username = clientInfo.getId();
if(profilesCache.isKeyInCache(userId))
return (JSONObject) profilesCache.get(userId).getObjectValue(); Cache<String,JSONObject> userCache = CachesManager.getUserCache();
else{
if(userCache.containsKey(username))
return userCache.get(username);
else {
GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL(); GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL();
String socialServiceUrl = socialService.getServiceBasePath(); String socialServiceUrl = socialService.getServiceBasePath();
String url = socialServiceUrl + "2/users/get-profile"; String url = socialServiceUrl + "2/users/get-profile";
try(CloseableHttpClient client = HttpClientBuilder.create().build();){ try(CloseableHttpClient client = HttpClientBuilder.create().build();) {
HttpGet getRequest = new HttpGet(url + "?gcube-token=" + SecurityTokenProvider.instance.get()); HttpGet getRequest = new HttpGet(url + "?gcube-token=" + SecurityTokenProvider.instance.get());
HttpResponse response = client.execute(getRequest); HttpResponse response = client.execute(getRequest);
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject profile = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity())); JSONObject profile = (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
profilesCache.put(new Element(userId, profile)); userCache.put(username, profile);
return profile; return profile;
}catch(Exception e){ } catch(Exception e) {
logger.error("error while performing get method " + e.toString()); logger.error("error while performing get method " + e.toString());
throw e; throw e;
} }
} }
} }
/** /**
* Fetch the sysadmin key from the IS * Fetch the sysadmin key from the IS
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static String fetchSysAPI(String context) throws Exception{ public static String fetchSysAPI() throws Exception {
DataCatalogueRunningCluster catalogueRunningInstance = new DataCatalogueRunningCluster(
DataCatalogueRunningCluster catalogueRunningInstance = new DataCatalogueRunningCluster(context); ContextUtils.getContext());
return catalogueRunningInstance.getSysAdminToken(); return catalogueRunningInstance.getSysAdminToken();
} }
/** public static String getApiKey() throws Exception {
* Check if the token belongs to an application token return ContextUtils.isApplication() ? fetchSysAPI()
* @param caller : getCatalogue().getApiKeyFromUsername(ContextUtils.getUsername());
* @return
*/
public static boolean isApplicationToken(Caller caller){
return caller.getClient().getType().equals(ClientType.EXTERNALSERVICE);
} }
public static String getIdFromUriInfo(String idName, UriInfo uriInfo) throws Exception {
MultivaluedMap<String,String> queryParams = uriInfo.getQueryParameters(false);
List<String> ids = queryParams.get(idName);
if(ids == null || ids.isEmpty()) {
throw new Exception("'" + idName + "' field is missing!");
}else if(ids.size()>1) {
throw new Exception("More than one '\" + idName + \"' has been provided!");
}
return ids.get(0);
}
public static String getIdFromJSONString(String idName, String jsonString) throws Exception {
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
String id = (String) jsonObject.get(idName);
if(id == null || id.isEmpty()) {
throw new Exception("'" + idName + "' field is missing!");
}
return id;
}
} }

View File

@ -2,7 +2,8 @@ package org.gcube.datacatalogue.catalogue.utils;
/** /**
* Constants used within this service. * Constants used within this service.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class Constants { public class Constants {

View File

@ -0,0 +1,39 @@
package org.gcube.datacatalogue.catalogue.utils;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextUtils {
private ContextUtils(){}
public static String getContext() throws ObjectNotFound, Exception {
String context = ScopeProvider.instance.get();
if (context == null) {
String token = SecurityTokenProvider.instance.get();
try {
return org.gcube.common.authorization.client.Constants.authorizationService().get(token).getContext();
}catch (Exception e) {
new RuntimeException(e);
}
}
return context;
}
public static String getUsername() {
return AuthorizationProvider.instance.get().getClient().getId();
}
public static boolean isApplication() {
return AuthorizationProvider.instance.get().getClient().getType().equals(ClientType.EXTERNALSERVICE);
}
}

View File

@ -22,8 +22,6 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart;
@ -32,77 +30,79 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* As the name says, it just delegates GET/POST operations to CKAN * As the name says, it just delegates GET/POST operations to CKAN
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class Delegator { public class Delegator {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Delegator.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Delegator.class);
public static String delegateGet(String method, UriInfo uriInfo) {
return delegateGet(method, uriInfo.getQueryParameters(false));
}
/** /**
* Execute the get
* @param caller
* @param context
* @param method * @param method
* @param uriInfo * @param uriInfo
* @return * @return
* @throws Exception
*/ */
public static String delegateGet(Caller caller, String context, String method, UriInfo uriInfo, boolean isAppRequest){ public static String delegateGet(String method, MultivaluedMap<String,String> undecodedParams) {
String username = caller.getClient().getId();
try(CloseableHttpClient client = HttpClientBuilder.create().build();) {
try(CloseableHttpClient client = HttpClientBuilder.create().build();){
DataCatalogue catalogue = CatalogueUtils.getCatalogue(); DataCatalogue catalogue = CatalogueUtils.getCatalogue();
String authorization = isAppRequest ? CatalogueUtils.fetchSysAPI(ScopeProvider.instance.get()) : catalogue.getApiKeyFromUsername(username); String authorization = CatalogueUtils.getApiKey();
String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl() : catalogue.getCatalogueUrl() + "/";
String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl()
: catalogue.getCatalogueUrl() + "/";
requestPath += method; requestPath += method;
MultivaluedMap<String, String> undecodedParams = uriInfo.getQueryParameters(false);
Iterator<Entry<String, List<String>>> iterator = undecodedParams.entrySet().iterator(); if(undecodedParams !=null) {
Iterator<Entry<String,List<String>>> iterator = undecodedParams.entrySet().iterator();
while (iterator.hasNext()) { while(iterator.hasNext()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iterator Map.Entry<java.lang.String,java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String,java.util.List<java.lang.String>>) iterator
.next(); .next();
if(entry.getKey().equals(Constants.GCUBE_TOKEN_PARAMETER)) if(entry.getKey().equals(Constants.GCUBE_TOKEN_PARAMETER)) {
continue; continue;
else{ } else {
List<String> values = entry.getValue();
List<String> values = entry.getValue(); for(String value : values) {
for (String value : values) { requestPath += entry.getKey() + "=" + value + "&";
requestPath += entry.getKey() + "=" + value + "&"; }
} }
} }
} }
if(requestPath.endsWith("&")) if(requestPath.endsWith("&")) {
requestPath = requestPath.substring(0, requestPath.length() - 1); requestPath = requestPath.substring(0, requestPath.length() - 1);
}
HttpGet request = new HttpGet(requestPath); HttpGet request = new HttpGet(requestPath);
if(authorization != null) if(authorization != null) {
request.addHeader(Constants.AUTH_CKAN_HEADER, authorization); request.addHeader(Constants.AUTH_CKAN_HEADER, authorization);
}
logger.debug("******* REQUEST URL IS " + requestPath); logger.debug("******* REQUEST URL IS " + requestPath);
HttpEntity entityRes = client.execute(request).getEntity(); HttpEntity entityRes = client.execute(request).getEntity();
String json = EntityUtils.toString(entityRes); String json = EntityUtils.toString(entityRes);
// substitute "help" field // substitute "help" field
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(json); JSONObject obj = (JSONObject) parser.parse(json);
obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE); obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE);
return obj.toJSONString(); return obj.toJSONString();
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to serve the request", e); logger.error("Failed to serve the request", e);
return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e); return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e);
} }
} }
/** /**
* Execute the post * Execute the post
* @param caller * @param caller
@ -112,42 +112,40 @@ public class Delegator {
* @param uriInfo * @param uriInfo
* @throws Exception * @throws Exception
*/ */
public static String delegatePost(Caller caller, String context, public static String delegatePost(String method, String json, UriInfo uriInfo) {
String method, String json, UriInfo uriInfo, boolean isAppRequest){ try(CloseableHttpClient client = HttpClientBuilder.create().build();) {
String username = caller.getClient().getId(); DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
try(CloseableHttpClient client = HttpClientBuilder.create().build();){ String authorization = CatalogueUtils.getApiKey();
String requestPath = dataCatalogue.getCatalogueUrl().endsWith("/") ? dataCatalogue.getCatalogueUrl()
DataCatalogue catalogue = CatalogueUtils.getCatalogue(); : dataCatalogue.getCatalogueUrl() + "/";
String authorization = isAppRequest ? CatalogueUtils.fetchSysAPI(ScopeProvider.instance.get()) : catalogue.getApiKeyFromUsername(username);
String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl() : catalogue.getCatalogueUrl() + "/";
requestPath += method + "?"; requestPath += method + "?";
MultivaluedMap<String, String> undecodedParams = uriInfo.getQueryParameters(false); MultivaluedMap<String,String> undecodedParams = uriInfo.getQueryParameters(false);
Iterator<Entry<String, List<String>>> iterator = undecodedParams.entrySet().iterator(); Iterator<Entry<String,List<String>>> iterator = undecodedParams.entrySet().iterator();
while (iterator.hasNext()) { while(iterator.hasNext()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iterator Map.Entry<java.lang.String,java.util.List<java.lang.String>> entry =
.next(); (Map.Entry<java.lang.String,java.util.List<java.lang.String>>) iterator.next();
if(entry.getKey().equals(Constants.GCUBE_TOKEN_PARAMETER)) if(entry.getKey().equals(Constants.GCUBE_TOKEN_PARAMETER))
continue; continue;
else{ else {
List<String> values = entry.getValue(); List<String> values = entry.getValue();
for (String value : values) { for(String value : values) {
requestPath += entry.getKey() + "=" + value + "&"; requestPath += entry.getKey() + "=" + value + "&";
} }
} }
} }
if(requestPath.endsWith("&")) if(requestPath.endsWith("&")) {
requestPath = requestPath.substring(0, requestPath.length() - 1); requestPath = requestPath.substring(0, requestPath.length() - 1);
}
logger.debug("POST request url is going to be " + requestPath); logger.debug("POST request url is going to be " + requestPath);
HttpPost request = new HttpPost(requestPath); HttpPost request = new HttpPost(requestPath);
request.addHeader(Constants.AUTH_CKAN_HEADER, authorization); request.addHeader(Constants.AUTH_CKAN_HEADER, authorization);
logger.debug("Sending json to CKAN is " + json); logger.debug("Sending json to CKAN is " + json);
@ -155,105 +153,96 @@ public class Delegator {
request.setEntity(params); request.setEntity(params);
HttpEntity entityRes = client.execute(request).getEntity(); HttpEntity entityRes = client.execute(request).getEntity();
String jsonRes = EntityUtils.toString(entityRes); String jsonRes = EntityUtils.toString(entityRes);
logger.debug("Result from CKAN is " + jsonRes); logger.debug("Result from CKAN is " + jsonRes);
// substitute "help" field // substitute "help" field
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(jsonRes); JSONObject obj = (JSONObject) parser.parse(jsonRes);
obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE); obj.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE);
logger.debug("replaced information " + obj); logger.debug("replaced information " + obj);
return obj.toJSONString(); return obj.toJSONString();
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to serve the request", e); logger.error("Failed to serve the request", e);
return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e); return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e);
} }
} }
/** /**
* Execute post with multipart (e.g. for resource upload) * Execute post with multipart (e.g. for resource upload)
* @param caller * @param method
* @param context
* @param resourceCreate
* @param multiPart * @param multiPart
* @param uriInfo * @param uriInfo
* @return * @return
*/ */
public static String delegatePost(Caller caller, String context, public static String delegatePost(String method, FormDataMultiPart multiPart, UriInfo uriInfo) {
String method, FormDataMultiPart multiPart, UriInfo uriInfo, boolean isAppRequest) { try {
String username = caller.getClient().getId();
try{
DataCatalogue catalogue = CatalogueUtils.getCatalogue(); DataCatalogue catalogue = CatalogueUtils.getCatalogue();
String authorization = isAppRequest ? CatalogueUtils.fetchSysAPI(ScopeProvider.instance.get()) : catalogue.getApiKeyFromUsername(username); String authorization = CatalogueUtils.getApiKey();
String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl() : catalogue.getCatalogueUrl() + "/"; String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl()
: catalogue.getCatalogueUrl() + "/";
requestPath += method + "?"; requestPath += method + "?";
MultivaluedMap<String, String> undecodedParams = uriInfo.getQueryParameters(false); MultivaluedMap<String,String> undecodedParams = uriInfo.getQueryParameters(false);
Iterator<Entry<String, List<String>>> iterator = undecodedParams.entrySet().iterator(); Iterator<Entry<String,List<String>>> iterator = undecodedParams.entrySet().iterator();
while (iterator.hasNext()) { while(iterator.hasNext()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iterator Map.Entry<java.lang.String,java.util.List<java.lang.String>> entry =
.next(); (Map.Entry<java.lang.String,java.util.List<java.lang.String>>) iterator.next();
if(entry.getKey().equals("Constants.AUTH_CKAN_HEADER")) if(entry.getKey().equals(Constants.AUTH_CKAN_HEADER)) {
continue; continue;
else{ }else {
List<String> values = entry.getValue(); List<String> values = entry.getValue();
for (String value : values) { for(String value : values) {
requestPath += entry.getKey() + "=" + value + "&"; requestPath += entry.getKey() + "=" + value + "&";
} }
} }
} }
if(requestPath.endsWith("&")) if(requestPath.endsWith("&")) {
requestPath = requestPath.substring(0, requestPath.length() - 1); requestPath = requestPath.substring(0, requestPath.length() - 1);
}
logger.debug("POST request url is going to be " + requestPath); logger.debug("POST request url is going to be " + requestPath);
// use jersey client // use jersey client
logger.debug("Sending multipart to CKAN " + multiPart); logger.debug("Sending multipart to CKAN " + multiPart);
FormDataBodyPart upload = multiPart.getField("upload"); FormDataBodyPart upload = multiPart.getField("upload");
if(upload != null){ if(upload != null) {
File file = upload.getValueAs(File.class); File file = upload.getValueAs(File.class);
long fileLenghtBytes = file.length(); long fileLenghtBytes = file.length();
long fileLenghtMegaByte = fileLenghtBytes >> 20; long fileLenghtMegaByte = fileLenghtBytes >> 20;
logger.debug("File lenght in MegaByte is " + fileLenghtMegaByte); logger.debug("File lenght in MegaByte is " + fileLenghtMegaByte);
if(fileLenghtMegaByte > Constants.MAX_UPLOADABLE_FILE_SIZE_MB) if(fileLenghtMegaByte > Constants.MAX_UPLOADABLE_FILE_SIZE_MB)
throw new Exception("Exceeding maximum uploadable file size!"); throw new Exception("Exceeding maximum uploadable file size!");
}else } else {
throw new Exception("No 'upload' field has been provided!"); throw new Exception("No 'upload' field has been provided!");
}
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
client.register(MultiPartFeature.class); client.register(MultiPartFeature.class);
WebTarget webResource = client.target(requestPath); WebTarget webResource = client.target(requestPath);
JSONObject jsonRes = JSONObject jsonRes = webResource.request(MediaType.APPLICATION_JSON)
webResource
.request(MediaType.APPLICATION_JSON)
.header(Constants.AUTH_CKAN_HEADER, authorization) .header(Constants.AUTH_CKAN_HEADER, authorization)
.post(Entity.entity(multiPart, multiPart.getMediaType()), JSONObject.class); .post(Entity.entity(multiPart, multiPart.getMediaType()), JSONObject.class);
logger.debug("Result from CKAN is " + jsonRes); logger.debug("Result from CKAN is " + jsonRes);
// substitute "help" field // substitute "help" field
jsonRes.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE); jsonRes.put(Constants.HELP_KEY, Constants.HELP_URL_GCUBE_CATALOGUE);
return jsonRes.toJSONString(); return jsonRes.toJSONString();
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to serve the request", e); logger.error("Failed to serve the request", e);
return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e); return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e);
} }
} }
} }

View File

@ -1,6 +1,3 @@
/**
*
*/
package org.gcube.datacatalogue.catalogue.utils; package org.gcube.datacatalogue.catalogue.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.client; import static org.gcube.resources.discovery.icclient.ICFactory.client;
@ -9,7 +6,6 @@ import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List; import java.util.List;
import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -17,56 +13,61 @@ import org.slf4j.LoggerFactory;
/** /**
* Discover the Social Networking Service in the Infrastructure. * Discover the Social Networking Service in the Infrastructure.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class GcoreEndpointReaderSNL { public class GcoreEndpointReaderSNL {
private static final String RESOURCE = "jersey-servlet"; private static final String RESOURCE = "jersey-servlet";
private static final String SERVICE_NAME = "SocialNetworking"; private static final String SERVICE_NAME = "SocialNetworking";
private static final String SERVICE_CLASSE = "Portal"; private static final String SERVICE_CLASSE = "Portal";
private static Logger logger = LoggerFactory.getLogger(GcoreEndpointReaderSNL.class); private static Logger logger = LoggerFactory.getLogger(GcoreEndpointReaderSNL.class);
private String serviceBasePath; private String serviceBasePath;
/** /**
* Discover the gcore endpoint for the social networking service. * Discover the gcore endpoint for the social networking service.
* @param scope the scope
* @throws Exception the exception * @throws Exception the exception
*/ */
public GcoreEndpointReaderSNL() throws Exception { public GcoreEndpointReaderSNL() throws Exception {
String currentScope = ScopeProvider.instance.get(); try {
try{
SimpleQuery query = queryFor(GCoreEndpoint.class); SimpleQuery query = queryFor(GCoreEndpoint.class);
query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",SERVICE_CLASSE)); query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'", SERVICE_CLASSE));
query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'");
query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",SERVICE_NAME)); query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'", SERVICE_NAME));
query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+RESOURCE+"\"]/text()"); query.setResult(
"$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""
+ RESOURCE + "\"]/text()");
DiscoveryClient<String> client = client(); DiscoveryClient<String> client = client();
List<String> endpoints = client.submit(query); List<String> endpoints = client.submit(query);
if (endpoints == null || endpoints.isEmpty()) if(endpoints == null || endpoints.isEmpty()) {
throw new Exception("Cannot retrieve the GCoreEndpoint SERVICE_NAME: "+SERVICE_NAME +", SERVICE_CLASSE: " +SERVICE_CLASSE +", in scope: "+currentScope); throw new Exception("Cannot retrieve the GCoreEndpoint SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtils.getContext());
}
this.serviceBasePath = endpoints.get(0); this.serviceBasePath = endpoints.get(0);
if(serviceBasePath==null) if(serviceBasePath == null)
throw new Exception("Endpoint:"+RESOURCE+", is null for SERVICE_NAME: "+SERVICE_NAME +", SERVICE_CLASSE: " +SERVICE_CLASSE +", in scope: "+currentScope); throw new Exception("Endpoint:" + RESOURCE + ", is null for SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtils.getContext());
serviceBasePath = serviceBasePath.endsWith("/") ? serviceBasePath : serviceBasePath + "/"; serviceBasePath = serviceBasePath.endsWith("/") ? serviceBasePath : serviceBasePath + "/";
}catch(Exception e){ } catch(Exception e) {
String error = "An error occurred during GCoreEndpoint discovery, SERVICE_NAME: "+SERVICE_NAME +", SERVICE_CLASSE: " +SERVICE_CLASSE +", in scope: "+currentScope +"."; String error = "An error occurred during GCoreEndpoint discovery, SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtils.getContext() + ".";
logger.error(error, e); logger.error(error, e);
throw new Exception(error); throw new Exception(error);
} }
} }
/** /**
* @return the base path of the service * @return the base path of the service
*/ */
public String getServiceBasePath() { public String getServiceBasePath() {
return serviceBasePath; return serviceBasePath;
} }
} }

View File

@ -6,32 +6,26 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Actions to performa after a package has been correctly created on ckan. * Actions to perform after a package has been correctly created on ckan.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class PackageCreatePostActions extends Thread { public class PackageCreatePostActions extends Thread {
private String packageId; private String packageId;
private String context;
private String datasetUrl; private String datasetUrl;
private String token;
private String username;
private boolean isApplication;
private JSONArray tags; private JSONArray tags;
private String title; private String title;
public static final String ITEM_URL = "Item URL"; public static final String ITEM_URL = "Item URL";
private static Logger logger = LoggerFactory.getLogger(PackageCreatePostActions.class); private static Logger logger = LoggerFactory.getLogger(PackageCreatePostActions.class);
/** /**
* @param isApplication * @param isApplication
* @param packageId * @param packageId
@ -39,80 +33,55 @@ public class PackageCreatePostActions extends Thread {
* @param tags * @param tags
* @param title * @param title
*/ */
public PackageCreatePostActions( public PackageCreatePostActions(String datasetUrl, String packageId, JSONArray tags, String title) {
String username,
boolean isApplication,
String datasetUrl,
String packageId,
String context,
String token,
JSONArray tags,
String title) {
super(); super();
this.packageId = packageId; this.packageId = packageId;
this.datasetUrl = datasetUrl; this.datasetUrl = datasetUrl;
this.isApplication = isApplication;
this.context = context;
this.token = token;
this.username = username;
this.tags = tags; this.tags = tags;
this.title = title; this.title = title;
} }
@Override @Override
public void run() { public void run() {
try{ try {
DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
ScopeProvider.instance.set(context); String apiKey = CatalogueUtils.getApiKey();
SecurityTokenProvider.instance.set(token); dataCatalogue.setSearchableField(packageId, true);
DataCatalogue utils = CatalogueUtils.getCatalogue();
String apiKey = isApplication ? CatalogueUtils.fetchSysAPI(context) : utils.getApiKeyFromUsername(username);
utils.setSearchableField(packageId, true);
// add also this information as custom field // add also this information as custom field
if(datasetUrl == null) if(datasetUrl == null)
datasetUrl = utils.getUnencryptedUrlFromDatasetIdOrName(packageId); datasetUrl = dataCatalogue.getUnencryptedUrlFromDatasetIdOrName(packageId);
Map<String, List<String>> addField = new HashMap<String, List<String>>(); Map<String,List<String>> addField = new HashMap<String,List<String>>();
addField.put(ITEM_URL, Arrays.asList(datasetUrl)); addField.put(ITEM_URL, Arrays.asList(datasetUrl));
utils.patchProductCustomFields(packageId, apiKey, addField, false); dataCatalogue.patchProductCustomFields(packageId, apiKey, addField, false);
String fullNameUser = null; String userFullName = ContextUtils.getUsername();
if(!isApplication){
JSONObject profile = CatalogueUtils.getUserProfile(username); if(!ContextUtils.isApplication()) {
JSONObject profileValues = (JSONObject)profile.get(Constants.RESULT_KEY); JSONObject profile = CatalogueUtils.getUserProfile();
fullNameUser = (String) profileValues.get(Constants.FULLNAME_IN_PROFILE_KEY); JSONObject profileValues = (JSONObject) profile.get(Constants.RESULT_KEY);
}else{ userFullName = (String) profileValues.get(Constants.FULLNAME_IN_PROFILE_KEY);
fullNameUser = username;
} }
List<String> tagsList = null; List<String> tagsList = null;
if(tags != null){ if(tags != null) {
tagsList = new ArrayList<String>(); tagsList = new ArrayList<String>();
for(int i = 0; i < tags.size(); i++){ for(int i = 0; i < tags.size(); i++) {
JSONObject obj = (JSONObject)(tags.get(i)); JSONObject obj = (JSONObject) (tags.get(i));
tagsList.add((String)(obj.get("display_name"))); tagsList.add((String) (obj.get("display_name")));
} }
} }
// write notification post // write notification post
WritePostCatalogueManagerThread threadWritePost = WritePostCatalogueManagerThread threadWritePost = new WritePostCatalogueManagerThread(title, datasetUrl,
new WritePostCatalogueManagerThread( dataCatalogue.isNotificationToUsersEnabled(), tagsList, userFullName);
context,
title,
datasetUrl,
utils.isNotificationToUsersEnabled(),
tagsList,
fullNameUser,
token
);
threadWritePost.start(); threadWritePost.start();
}catch(Exception e){ } catch(Exception e) {
logger.error("Error while executing post creation actions", e); logger.error("Error while executing post creation actions", e);
} }
} }
} }

View File

@ -12,7 +12,6 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
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;
@ -27,50 +26,52 @@ import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory;
import org.geojson.GeoJsonObject; import org.geojson.GeoJsonObject;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.trentorise.opendata.jackan.model.CkanGroup;
/** /**
* Validate creation item requests utilities. * Validate creation item requests utilities.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings({"rawtypes","unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
public class Validator { public class Validator {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Validator.class); private static final Logger logger = LoggerFactory.getLogger(Validator.class);
private static final SimpleDateFormat DATE_SIMPLE = new SimpleDateFormat("yyyy-MM-dd"); private static final SimpleDateFormat DATE_SIMPLE = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat DATE_HOUR_MINUTES = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private static final SimpleDateFormat DATE_HOUR_MINUTES = new SimpleDateFormat("yyyy-MM-dd HH:mm");
/** /**
* Check resources have at least url/name * Check resources have at least url/name
* @param json * @param json
* @param caller * @param caller
* @throws Exception * @throws Exception
*/ */
public static void checkResourcesInformation(JSONObject dataset, Caller caller) throws Exception { public static void checkResourcesInformation(JSONObject dataset) throws Exception {
JSONArray resources = (JSONArray) dataset.get(Constants.RESOURCES_KEY);
JSONArray resources = (JSONArray)dataset.get(Constants.RESOURCES_KEY); if(resources == null || resources.isEmpty()) {
if(resources == null || resources.isEmpty())
return; return;
else{ } else {
Iterator it = resources.iterator(); Iterator it = resources.iterator();
while (it.hasNext()) { while(it.hasNext()) {
JSONObject resource = (JSONObject) it.next(); JSONObject resource = (JSONObject) it.next();
String name = (String)resource.get(Constants.RESOURCE_NAME_KEY); String name = (String) resource.get(Constants.RESOURCE_NAME_KEY);
String url = (String)resource.get(Constants.RESOURCE_URL_KEY); String url = (String) resource.get(Constants.RESOURCE_URL_KEY);
if(url == null || name == null || url.isEmpty() || name.isEmpty()) if(url == null || name == null || url.isEmpty() || name.isEmpty()) {
throw new Exception("Resources must have at least a name and an url field set!"); throw new Exception("Resources must have at least a name and an url field set!");
}
} }
} }
} }
/** /**
* This method validates the incoming json, in this sense: * This method validates the incoming json, in this sense:
* <ul> * <ul>
@ -84,53 +85,52 @@ public class Validator {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static void checkBaseInformation(JSONObject dataset, Caller caller, boolean isAppRequest) throws Exception{ public static void checkBaseInformation(JSONObject dataset) throws Exception {
// check license // check license
String licenseId = (String)dataset.get(Constants.LICENSE_KEY); String licenseId = (String) dataset.get(Constants.LICENSE_KEY);
if(licenseId == null || licenseId.isEmpty()) if(licenseId == null || licenseId.isEmpty())
throw new Exception("You must specify a license identifier to be attached to the item. License list can be retrieved invoking license methods"); throw new Exception(
"You must specify a license identifier to be attached to the item. License list can be retrieved invoking license methods");
// set author and author email // set author and author email
if(!ContextUtils.isApplication()) {
if(!isAppRequest){ JSONObject profile = CatalogueUtils.getUserProfile();
JSONObject profile = CatalogueUtils.getUserProfile(caller.getClient().getId()); JSONObject profileValues = (JSONObject) profile.get(Constants.RESULT_KEY);
JSONObject profileValues = (JSONObject)profile.get(Constants.RESULT_KEY);
dataset.put(Constants.AUTHOR_KEY, profileValues.get(Constants.FULLNAME_IN_PROFILE_KEY)); dataset.put(Constants.AUTHOR_KEY, profileValues.get(Constants.FULLNAME_IN_PROFILE_KEY));
dataset.put(Constants.AUTHOR_EMAIL_KEY, profileValues.get(Constants.EMAIL_IN_PROFILE_KEY)); dataset.put(Constants.AUTHOR_EMAIL_KEY, profileValues.get(Constants.EMAIL_IN_PROFILE_KEY));
}else{ } else {
dataset.put(Constants.AUTHOR_KEY, caller.getClient().getId()); dataset.put(Constants.AUTHOR_KEY, ContextUtils.getUsername());
dataset.put(Constants.AUTHOR_EMAIL_KEY, CatalogueUtils.getCatalogue().getCatalogueEmail()); dataset.put(Constants.AUTHOR_EMAIL_KEY, CatalogueUtils.getCatalogue().getCatalogueEmail());
} }
// version // version
String version = (String)dataset.get(Constants.VERSION_KEY); String version = (String) dataset.get(Constants.VERSION_KEY);
if(version == null || version.isEmpty()){ if(version == null || version.isEmpty()) {
version = "1"; version = "1";
dataset.put(Constants.VERSION_KEY, version); dataset.put(Constants.VERSION_KEY, version);
} }
// owner organization must be specified if the token belongs to a VRE // owner organization must be specified if the token belongs to a VRE
String username = caller.getClient().getId();
ScopeBean scopeBean = new ScopeBean(ScopeProvider.instance.get()); ScopeBean scopeBean = new ScopeBean(ScopeProvider.instance.get());
String ownerOrgFromScope = scopeBean.name(); String ownerOrgFromScope = scopeBean.name();
boolean isVREToken = scopeBean.is(Type.VRE); boolean isVREToken = scopeBean.is(Type.VRE);
String ownerOrg = (String)dataset.get(Constants.OWNER_ORG_KEY); String ownerOrg = (String) dataset.get(Constants.OWNER_ORG_KEY);
String organization = isVREToken ? ownerOrgFromScope.toLowerCase().replace(" ", "_").replace("-", "_") : ownerOrg != null ? String organization = isVREToken ? ownerOrgFromScope.toLowerCase().replace(" ", "_").replace("-", "_")
ownerOrg.toLowerCase().replace(" ", "_").replace("-", "_") : null; : ownerOrg != null ? ownerOrg.toLowerCase().replace(" ", "_").replace("-", "_") : null;
if(organization != null){ if(organization != null) {
if(!isAppRequest) if(!ContextUtils.isApplication()) {
CatalogueUtils.checkRole(username, organization); CatalogueUtils.checkRole(organization);
dataset.put(Constants.OWNER_ORG_KEY, organization); }
} dataset.put(Constants.OWNER_ORG_KEY, organization);
else } else {
throw new Exception("You must specify the field 'owner_org' in which the item should be published!"); throw new Exception("You must specify the field '" + Constants.OWNER_ORG_KEY + "' in which the item should be published!");
}
} }
/** /**
* This method validate the incoming json dataset wrt a metadata profile * This method validate the incoming json dataset wrt a metadata profile
* @param json * @param json
@ -139,167 +139,184 @@ public class Validator {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static void validateAgainstProfile(JSONObject obj, Caller caller, List<String> profiles, boolean isApplication) throws Exception { public static void validateAgainstProfile(JSONObject obj, List<String> profiles) throws Exception {
JSONArray extrasArrayOriginal = (JSONArray)obj.get(Constants.EXTRAS_KEY); JSONArray extrasArrayOriginal = (JSONArray) obj.get(Constants.EXTRAS_KEY);
JSONArray groupsArrayOriginal = (JSONArray)obj.get(Constants.GROUPS_KEY); JSONArray groupsArrayOriginal = (JSONArray) obj.get(Constants.GROUPS_KEY);
JSONArray tagsArrayOriginal = (JSONArray)obj.get(Constants.TAGS_KEY); JSONArray tagsArrayOriginal = (JSONArray) obj.get(Constants.TAGS_KEY);
if(extrasArrayOriginal == null || extrasArrayOriginal.isEmpty()) if(extrasArrayOriginal == null || extrasArrayOriginal.isEmpty()) {
throw new Exception("'extras' field is missing in context where metadata profile(s) are defined!"); throw new Exception("'extras' field is missing in context where metadata profile(s) are defined!");
}
if(groupsArrayOriginal == null) {
groupsArrayOriginal = new JSONArray();
}
if(tagsArrayOriginal == null) {
tagsArrayOriginal = new JSONArray();
}
// get the metadata profile specifying the type // get the metadata profile specifying the type
CustomField metadataTypeCF = null; CustomField metadataTypeCF = null;
List<CustomField> customFields = new ArrayList<CustomField>(extrasArrayOriginal.size()); List<CustomField> customFields = new ArrayList<CustomField>(extrasArrayOriginal.size());
Iterator iterator = extrasArrayOriginal.iterator(); Iterator iterator = extrasArrayOriginal.iterator();
while (iterator.hasNext()) { while(iterator.hasNext()) {
JSONObject object = (JSONObject) iterator.next(); JSONObject object = (JSONObject) iterator.next();
CustomField cf = new CustomField(object); CustomField cf = new CustomField(object);
if(cf.getKey().equals(Constants.TYPE_KEY)) if(cf.getKey().equals(Constants.TYPE_KEY)) {
metadataTypeCF = cf; metadataTypeCF = cf;
else if(cf.getKey().equals(PackageCreatePostActions.ITEM_URL)) } else if(cf.getKey().equals(PackageCreatePostActions.ITEM_URL)) {
continue; continue;
else } else {
customFields.add(cf); customFields.add(cf);
}
} }
if(metadataTypeCF == null) if(metadataTypeCF == null) {
throw new Exception("'" + Constants.TYPE_KEY + "' extra field is missing in context where metadata profile(s) are defined!"); throw new Exception("'" + Constants.TYPE_KEY
+ "' extra field is missing in context where metadata profile(s) are defined!");
if(groupsArrayOriginal == null) }
groupsArrayOriginal = new JSONArray();
if(tagsArrayOriginal == null)
tagsArrayOriginal = new JSONArray();
// fetch the profile by metadata type specified above // fetch the profile by metadata type specified above
MetadataFormat profile = null; MetadataFormat profile = null;
for (String profileName : profiles) { for(String profileName : profiles) {
profile = CatalogueUtils.getMetadataProfile(profileName); profile = CatalogueUtils.getMetadataProfile(profileName);
if(profile.getType().equals(metadataTypeCF.getValue())) if(profile.getType().equals(metadataTypeCF.getValue())) {
break; break;
else } else {
profile = null; profile = null;
}
} }
if(profile == null) if(profile == null) {
throw new Exception("'" + Constants.TYPE_KEY + "' extra field's value specified as custom field doesn't match any of the profiles defined in this context!"); throw new Exception("'" + Constants.TYPE_KEY
else{ + "' extra field's value ('" + metadataTypeCF.getValue() +
"') specified as custom field doesn't match any of the profiles defined in this context!");
} else {
JSONArray extrasArrayUpdated = null; JSONArray extrasArrayUpdated = null;
List<MetadataField> metadataFields = profile.getMetadataFields(); List<MetadataField> metadataFields = profile.getMetadataFields();
if(metadataFields == null || metadataFields.isEmpty()) if(metadataFields == null || metadataFields.isEmpty()) {
extrasArrayUpdated = extrasArrayOriginal; extrasArrayUpdated = extrasArrayOriginal;
else{ } else {
extrasArrayUpdated = new JSONArray(); extrasArrayUpdated = new JSONArray();
List<NamespaceCategory> categories = CatalogueUtils.getNamespaceCategories();
logger.debug("Retrieved namespaces are " + categories); List<NamespaceCategory> categories = CatalogueUtils.getNamespaceCategories();
logger.debug("Retrieved namespaces are {}", categories);
List<String> categoriesIds = new ArrayList<String>(categories == null ? 0 : categories.size()); List<String> categoriesIds = new ArrayList<String>(categories == null ? 0 : categories.size());
if(categories == null || categories.isEmpty()) if(categories == null || categories.isEmpty()) {
logger.warn("No category defined in context " + ScopeProvider.instance.get()); logger.warn("No category defined in context {}", ScopeProvider.instance.get());
else } else {
for (NamespaceCategory metadataCategory : categories) for(NamespaceCategory metadataCategory : categories) {
categoriesIds.add(metadataCategory.getId()); // save them later for matching with custom fields categoriesIds.add(metadataCategory.getId()); // save them later for matching with custom fields
}
}
// the list of already validated customFields // the list of already validated customFields
List<CustomField> validatedCustomFields = new ArrayList<CustomField>(customFields.size()); List<CustomField> validatedCustomFields = new ArrayList<CustomField>(customFields.size());
// keep track of mandatory fields and their cardinality // keep track of mandatory fields and their cardinality
Map<String, Integer> fieldsMandatoryLowerBoundMap = new HashMap<String, Integer>(metadataFields.size()); Map<String,Integer> fieldsMandatoryLowerBoundMap = new HashMap<String,Integer>(metadataFields.size());
Map<String, Integer> fieldsMandatoryUpperBoundMap = new HashMap<String, Integer>(metadataFields.size()); Map<String,Integer> fieldsMandatoryUpperBoundMap = new HashMap<String,Integer>(metadataFields.size());
Map<String, Integer> numberFieldsMandatorySameKeyMap = new HashMap<String, Integer>(metadataFields.size()); Map<String,Integer> numberFieldsMandatorySameKeyMap = new HashMap<String,Integer>(
metadataFields.size());
// keep track of the groups that must be created AFTER validation but BEFORE item creation // keep track of the groups that must be created AFTER validation but BEFORE item creation
List<String> groupsToCreateAfterValidation = new ArrayList<String>(); List<String> groupsToCreateAfterValidation = new ArrayList<String>();
// now validate fields // now validate fields
int metadataIndex = 0; int metadataIndex = 0;
for (MetadataField metadataField : metadataFields) { for(MetadataField metadataField : metadataFields) {
int categoryIdIndex = categoriesIds.indexOf(metadataField.getCategoryRef()); int categoryIdIndex = categoriesIds.indexOf(metadataField.getCategoryRef());
logger.debug("Found index for category " + metadataField.getCategoryRef() + " " + categoryIdIndex); logger.debug("Found index for category " + metadataField.getCategoryRef() + " " + categoryIdIndex);
List<CustomField> validCFs = validateAgainstMetadataField( List<CustomField> validCFs = validateAgainstMetadataField(metadataIndex, categoryIdIndex,
metadataIndex, customFields, tagsArrayOriginal, groupsArrayOriginal, metadataField, categories,
categoryIdIndex, fieldsMandatoryLowerBoundMap, fieldsMandatoryUpperBoundMap, numberFieldsMandatorySameKeyMap,
customFields, groupsToCreateAfterValidation);
tagsArrayOriginal,
groupsArrayOriginal,
metadataField,
categories,
fieldsMandatoryLowerBoundMap,
fieldsMandatoryUpperBoundMap,
numberFieldsMandatorySameKeyMap,
groupsToCreateAfterValidation,
caller.getClient().getId(),
isApplication);
validatedCustomFields.addAll(validCFs); validatedCustomFields.addAll(validCFs);
metadataIndex++; metadataIndex++;
} }
// check mandatory fields // check mandatory fields
Iterator<Entry<String, Integer>> iteratorLowerBounds = fieldsMandatoryLowerBoundMap.entrySet().iterator(); Iterator<Entry<String,Integer>> iteratorLowerBounds = fieldsMandatoryLowerBoundMap.entrySet()
while (iteratorLowerBounds.hasNext()) { .iterator();
Map.Entry<java.lang.String, java.lang.Integer> entry = (Map.Entry<java.lang.String, java.lang.Integer>) iteratorLowerBounds while(iteratorLowerBounds.hasNext()) {
Map.Entry<java.lang.String,java.lang.Integer> entry = (Map.Entry<java.lang.String,java.lang.Integer>) iteratorLowerBounds
.next(); .next();
int lowerBound = entry.getValue(); int lowerBound = entry.getValue();
int upperBound = fieldsMandatoryUpperBoundMap.get(entry.getKey()); int upperBound = fieldsMandatoryUpperBoundMap.get(entry.getKey());
int inserted = numberFieldsMandatorySameKeyMap.get(entry.getKey()); int inserted = numberFieldsMandatorySameKeyMap.get(entry.getKey());
logger.info("Field with key '" + entry.getKey() + "' has been found " + inserted + " times and its lower bound is " + lowerBound + " and upper bound " + upperBound); logger.info("Field with key '" + entry.getKey() + "' has been found " + inserted
+ " times and its lower bound is " + lowerBound + " and upper bound " + upperBound);
if(inserted < lowerBound || inserted > upperBound)
throw new Exception("Field with key '" + entry.getKey() + "' is mandatory, but it's not present among the provided fields or its cardinality is not respected ([min = " + lowerBound + ", max=" + upperBound +"])."); if(inserted < lowerBound || inserted > upperBound) {
throw new Exception("Field with key '" + entry.getKey()
+ "' is mandatory, but it's not present among the provided fields or its cardinality is not respected ([min = "
+ lowerBound + ", max=" + upperBound + "]).");
}
} }
// if there are no tags, throw an exception // if there are no tags, throw an exception
if(tagsArrayOriginal.isEmpty()) if(tagsArrayOriginal.isEmpty()) {
throw new Exception("Please define at least one tag for this item!"); throw new Exception("Please define at least one tag for this item!");
}
// sort validated custom fields and add to the extrasArrayUpdated json array // sort validated custom fields and add to the extrasArrayUpdated json array
Collections.sort(validatedCustomFields); Collections.sort(validatedCustomFields);
logger.debug("Sorted list of custom fields is " + validatedCustomFields); logger.debug("Sorted list of custom fields is " + validatedCustomFields);
// add missing fields with no match (append them at the end, since no metadataIndex or categoryIndex was defined for them) // add missing fields with no match (append them at the end, since no metadataIndex or categoryIndex was defined for them)
for(CustomField cf : customFields) for(CustomField cf : customFields)
validatedCustomFields.add(cf); validatedCustomFields.add(cf);
// convert back to json // convert back to json
for (CustomField customField : validatedCustomFields) { for(CustomField customField : validatedCustomFields) {
JSONObject jsonObj = new JSONObject(); JSONObject jsonObj = new JSONObject();
jsonObj.put(Constants.EXTRA_KEY, customField.getQualifiedKey()); jsonObj.put(Constants.EXTRA_KEY, customField.getQualifiedKey());
jsonObj.put(Constants.EXTRA_VALUE, customField.getValue()); jsonObj.put(Constants.EXTRA_VALUE, customField.getValue());
extrasArrayUpdated.add(jsonObj); extrasArrayUpdated.add(jsonObj);
} }
// add metadata type field as last element // add metadata type field as last element
JSONObject metadataTypeJSON = new JSONObject(); JSONObject metadataTypeJSON = new JSONObject();
metadataTypeJSON.put(Constants.EXTRA_KEY, metadataTypeCF.getKey()); metadataTypeJSON.put(Constants.EXTRA_KEY, metadataTypeCF.getKey());
metadataTypeJSON.put(Constants.EXTRA_VALUE, metadataTypeCF.getValue()); metadataTypeJSON.put(Constants.EXTRA_VALUE, metadataTypeCF.getValue());
extrasArrayUpdated.add(metadataTypeJSON); extrasArrayUpdated.add(metadataTypeJSON);
// create groups // create groups
for (String title : groupsToCreateAfterValidation){ for(String title : groupsToCreateAfterValidation) {
try { try {
CatalogueUtils.createGroupAsSysAdmin(title, title, ""); createGroupAsSysAdmin(title, title, "");
} catch (Exception e) { } catch(Exception e) {
logger.error("Failed to create group with title " + title, e); logger.error("Failed to create group with title " + title, e);
} }
} }
} }
obj.put(Constants.TAGS_KEY, tagsArrayOriginal); obj.put(Constants.TAGS_KEY, tagsArrayOriginal);
obj.put(Constants.GROUPS_KEY, groupsArrayOriginal); obj.put(Constants.GROUPS_KEY, groupsArrayOriginal);
obj.put(Constants.EXTRAS_KEY, extrasArrayUpdated); obj.put(Constants.EXTRAS_KEY, extrasArrayUpdated);
} }
} }
/**
* Retrieve an instance of the library for the scope
* @param scope if it is null it is evaluated from the session
* @return
* @throws Exception
*/
public static CkanGroup createGroupAsSysAdmin(String title, String groupName, String description) throws Exception {
return CatalogueUtils.getCatalogue().createGroup(groupName, title, description);
}
/** /**
* Validate this field and generate a new value (or returns the same if there is nothing to update) * Validate this field and generate a new value (or returns the same if there is nothing to update)
* @param metadataIndex * @param metadataIndex
@ -315,120 +332,109 @@ public class Validator {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static List<CustomField> validateAgainstMetadataField( private static List<CustomField> validateAgainstMetadataField(int metadataIndex, int categoryIndex,
int metadataIndex, List<CustomField> customFields, JSONArray tagsArrayOriginal, JSONArray groupsArrayOriginal,
int categoryIndex, MetadataField metadataField, List<NamespaceCategory> categories,
List<CustomField> customFields, Map<String,Integer> fieldsMandatoryLowerBoundMap, Map<String,Integer> fieldsMandatoryUpperBoundMap,
JSONArray tagsArrayOriginal, Map<String,Integer> numberFieldsMandatorySameKeyMap, List<String> groupToCreate) throws Exception {
JSONArray groupsArrayOriginal,
MetadataField metadataField,
List<NamespaceCategory> categories,
Map<String, Integer> fieldsMandatoryLowerBoundMap,
Map<String, Integer> fieldsMandatoryUpperBoundMap,
Map<String, Integer> numberFieldsMandatorySameKeyMap,
List<String> groupToCreate,
String username, boolean isApplication) throws Exception {
List<CustomField> toReturn = new ArrayList<CustomField>(); List<CustomField> toReturn = new ArrayList<CustomField>();
String metadataFieldName = metadataField.getCategoryFieldQName(); // get the qualified one, if any String metadataFieldName = metadataField.getCategoryFieldQName(); // get the qualified one, if any
int fieldsFoundWithThisKey = 0; int fieldsFoundWithThisKey = 0;
Iterator<CustomField> iterator = customFields.iterator(); Iterator<CustomField> iterator = customFields.iterator();
while (iterator.hasNext()) { while(iterator.hasNext()) {
CustomField cf = (CustomField) iterator.next(); CustomField cf = (CustomField) iterator.next();
if(cf.getKey().equals(metadataFieldName)){ if(cf.getKey().equals(metadataFieldName)) {
validate(cf, metadataField); validate(cf, metadataField);
fieldsFoundWithThisKey ++; fieldsFoundWithThisKey++;
cf.setIndexCategory(categoryIndex); cf.setIndexCategory(categoryIndex);
cf.setIndexMetadataField(metadataIndex); cf.setIndexMetadataField(metadataIndex);
checkAsGroup(cf, metadataField, groupsArrayOriginal, groupToCreate, username, isApplication); checkAsGroup(cf, metadataField, groupsArrayOriginal, groupToCreate);
checkAsTag(cf, metadataField, tagsArrayOriginal); checkAsTag(cf, metadataField, tagsArrayOriginal);
toReturn.add(cf); toReturn.add(cf);
iterator.remove(); iterator.remove();
} }
} }
// in case of mandatory fields, keep track of the number of times they appear // in case of mandatory fields, keep track of the number of times they appear
if(metadataField.getMandatory()){ if(metadataField.getMandatory()) {
// lower bound // lower bound
int lowerBound = 1; int lowerBound = 1;
if(fieldsMandatoryLowerBoundMap.containsKey(metadataFieldName)) if(fieldsMandatoryLowerBoundMap.containsKey(metadataFieldName))
lowerBound = fieldsMandatoryLowerBoundMap.get(metadataFieldName) + 1; lowerBound = fieldsMandatoryLowerBoundMap.get(metadataFieldName) + 1;
fieldsMandatoryLowerBoundMap.put(metadataFieldName, lowerBound); fieldsMandatoryLowerBoundMap.put(metadataFieldName, lowerBound);
// upper bound // upper bound
boolean hasVocabulary = metadataField.getVocabulary() != null; boolean hasVocabulary = metadataField.getVocabulary() != null;
int upperBound = hasVocabulary ? int upperBound = hasVocabulary ? (metadataField.getVocabulary().isMultiSelection()
(metadataField.getVocabulary().isMultiSelection() ? metadataField.getVocabulary().getVocabularyFields().size() : 1) : 1; ? metadataField.getVocabulary().getVocabularyFields().size()
: 1) : 1;
if(fieldsMandatoryUpperBoundMap.containsKey(metadataFieldName))
upperBound += fieldsMandatoryUpperBoundMap.get(metadataFieldName); if(fieldsMandatoryUpperBoundMap.containsKey(metadataFieldName))
upperBound += fieldsMandatoryUpperBoundMap.get(metadataFieldName);
fieldsMandatoryUpperBoundMap.put(metadataFieldName, upperBound);
fieldsMandatoryUpperBoundMap.put(metadataFieldName, upperBound);
// fields with this same key
int countPerFields = fieldsFoundWithThisKey; // fields with this same key
if(numberFieldsMandatorySameKeyMap.containsKey(metadataFieldName)) int countPerFields = fieldsFoundWithThisKey;
countPerFields += numberFieldsMandatorySameKeyMap.get(metadataFieldName); if(numberFieldsMandatorySameKeyMap.containsKey(metadataFieldName))
numberFieldsMandatorySameKeyMap.put(metadataFieldName, countPerFields); countPerFields += numberFieldsMandatorySameKeyMap.get(metadataFieldName);
numberFieldsMandatorySameKeyMap.put(metadataFieldName, countPerFields);
} }
// if there was no field with this key and it was not mandatory, just add an entry of the kind {"key": "key-value", "value" : ""}. // if there was no field with this key and it was not mandatory, just add an entry of the kind {"key": "key-value", "value" : ""}.
// Sometimes it is important to view the field as empty. // Sometimes it is important to view the field as empty.
if(fieldsFoundWithThisKey == 0 && !metadataField.getMandatory()){ if(fieldsFoundWithThisKey == 0 && !metadataField.getMandatory()) {
toReturn.add(new CustomField(metadataFieldName, "", -1, -1)); toReturn.add(new CustomField(metadataFieldName, "", -1, -1));
} }
return toReturn; return toReturn;
} }
/** /**
* Check if a tag must be generated * Check if a tag must be generated
* @param fieldToValidate * @param fieldToValidate
* @param metadataField * @param metadataField
* @param tagsArrayOriginal * @param tagsArrayOriginal
*/ */
private static void checkAsTag(CustomField fieldToValidate, private static void checkAsTag(CustomField fieldToValidate, MetadataField metadataField,
MetadataField metadataField, JSONArray tagsArrayOriginal) { JSONArray tagsArrayOriginal) {
MetadataTagging tagging = metadataField.getTagging(); MetadataTagging tagging = metadataField.getTagging();
if(tagging != null){ if(tagging != null) {
String tag = ""; String tag = "";
switch(tagging.getTaggingValue()){ switch(tagging.getTaggingValue()) {
case onFieldName: case onFieldName:
tag = metadataField.getFieldName(); tag = metadataField.getFieldName();
break; break;
case onValue: case onValue:
tag = fieldToValidate.getValue(); tag = fieldToValidate.getValue();
break; break;
case onFieldName_onValue: case onFieldName_onValue:
tag = metadataField.getFieldName() + tagging.getSeparator() + fieldToValidate.getValue(); tag = metadataField.getFieldName() + tagging.getSeparator() + fieldToValidate.getValue();
break; break;
case onValue_onFieldName: case onValue_onFieldName:
tag = fieldToValidate.getValue() + tagging.getSeparator() + metadataField.getFieldName(); tag = fieldToValidate.getValue() + tagging.getSeparator() + metadataField.getFieldName();
break; break;
default: default:
return; return;
} }
tag = tag.substring(0, Constants.MAX_TAG_CHARS > tag.length() ? tag.length() : Constants.MAX_TAG_CHARS); tag = tag.substring(0, Constants.MAX_TAG_CHARS > tag.length() ? tag.length() : Constants.MAX_TAG_CHARS);
logger.debug("Tag is " + tag); logger.debug("Tag is " + tag);
JSONObject tagJSON = new JSONObject(); JSONObject tagJSON = new JSONObject();
tagJSON.put("name", tag); tagJSON.put("name", tag);
tagJSON.put("display_name", tag); tagJSON.put("display_name", tag);
tagsArrayOriginal.add(tagJSON); tagsArrayOriginal.add(tagJSON);
} }
} }
/** /**
* Check if a group must be generated * Check if a group must be generated
* @param fieldToValidate * @param fieldToValidate
@ -437,44 +443,45 @@ public class Validator {
* @param isApplication * @param isApplication
* @throws Exception * @throws Exception
*/ */
private static void checkAsGroup(CustomField fieldToValidate, private static void checkAsGroup(CustomField fieldToValidate, MetadataField metadataField,
MetadataField metadataField, JSONArray groupsArrayOriginal, List<String> groupToCreate, String username, boolean isApplication) throws Exception { JSONArray groupsArrayOriginal, List<String> groupToCreate) throws Exception {
logger.debug("Custom field is " + fieldToValidate); logger.debug("Custom field is " + fieldToValidate);
logger.debug("MetadataField field is " + metadataField); logger.debug("MetadataField field is " + metadataField);
logger.debug("JSONArray field is " + groupsArrayOriginal); logger.debug("JSONArray field is " + groupsArrayOriginal);
MetadataGrouping grouping = metadataField.getGrouping(); MetadataGrouping grouping = metadataField.getGrouping();
if(grouping != null){ if(grouping != null) {
boolean propagateUp = grouping.getPropagateUp(); boolean propagateUp = grouping.getPropagateUp();
final Set<String> groupNames = new HashSet<String>(); final Set<String> groupNames = new HashSet<String>();
switch(grouping.getGroupingValue()){ switch(grouping.getGroupingValue()) {
case onFieldName: case onFieldName:
groupNames.add(metadataField.getFieldName()); groupNames.add(metadataField.getFieldName());
break; break;
case onValue: case onValue:
if(fieldToValidate.getValue() != null && !fieldToValidate.getValue().isEmpty()) if(fieldToValidate.getValue() != null && !fieldToValidate.getValue().isEmpty())
groupNames.add(fieldToValidate.getValue()); groupNames.add(fieldToValidate.getValue());
break; break;
case onFieldName_onValue: case onFieldName_onValue:
case onValue_onFieldName: case onValue_onFieldName:
groupNames.add(metadataField.getFieldName()); groupNames.add(metadataField.getFieldName());
if(fieldToValidate.getValue() != null && !fieldToValidate.getValue().isEmpty()) if(fieldToValidate.getValue() != null && !fieldToValidate.getValue().isEmpty())
groupNames.add(fieldToValidate.getValue()); groupNames.add(fieldToValidate.getValue());
break; break;
default: default:
return; return;
} }
for (String title : groupNames) { for(String title : groupNames) {
logger.debug("Adding group to which add this item " + CatalogueUtilMethods.fromGroupTitleToName(title)); logger.debug("Adding group to which add this item " + CatalogueUtilMethods.fromGroupTitleToName(title));
JSONObject group = new JSONObject(); JSONObject group = new JSONObject();
group.put("name", CatalogueUtilMethods.fromGroupTitleToName(title)); group.put("name", CatalogueUtilMethods.fromGroupTitleToName(title));
if(propagateUp){ if(propagateUp) {
List<String> parents = CatalogueUtils.getGroupHierarchyNames(CatalogueUtilMethods.fromGroupTitleToName(title), username, isApplication); List<String> parents = CatalogueUtils
for (String parent : parents) { .getGroupHierarchyNames(CatalogueUtilMethods.fromGroupTitleToName(title));
for(String parent : parents) {
JSONObject groupP = new JSONObject(); JSONObject groupP = new JSONObject();
groupP.put("name", parent); groupP.put("name", parent);
groupsArrayOriginal.add(groupP); groupsArrayOriginal.add(groupP);
@ -482,16 +489,16 @@ public class Validator {
} }
groupsArrayOriginal.add(group); groupsArrayOriginal.add(group);
} }
// force group creation if needed // force group creation if needed
if(grouping.getCreate()){ if(grouping.getCreate()) {
for (String title : groupNames) for(String title : groupNames)
groupToCreate.add(title); groupToCreate.add(title);
} }
} }
} }
/** /**
* Validate the single field * Validate the single field
* @param fieldToValidate * @param fieldToValidate
@ -500,118 +507,121 @@ public class Validator {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static void validate(CustomField fieldToValidate, private static void validate(CustomField fieldToValidate, MetadataField metadataField) throws Exception {
MetadataField metadataField) throws Exception {
DataType dataType = metadataField.getDataType(); DataType dataType = metadataField.getDataType();
String regex = metadataField.getValidator() != null ? metadataField.getValidator().getRegularExpression() : null; String regex = metadataField.getValidator() != null ? metadataField.getValidator().getRegularExpression()
: null;
boolean hasControlledVocabulary = metadataField.getVocabulary() != null; boolean hasControlledVocabulary = metadataField.getVocabulary() != null;
String value = fieldToValidate.getValue(); String value = fieldToValidate.getValue();
String key = fieldToValidate.getKey(); String key = fieldToValidate.getKey();
String defaultValue = metadataField.getDefaultValue(); String defaultValue = metadataField.getDefaultValue();
// replace key by prepending the qualified name of the category, if needed // replace key by prepending the qualified name of the category, if needed
fieldToValidate.setQualifiedKey(metadataField.getCategoryFieldQName()); fieldToValidate.setQualifiedKey(metadataField.getCategoryFieldQName());
if((value == null || value.isEmpty())) if((value == null || value.isEmpty()))
if(metadataField.getMandatory() || hasControlledVocabulary) if(metadataField.getMandatory() || hasControlledVocabulary)
throw new Exception("Mandatory field with name '" + key + "' doesn't have a value but it is mandatory or has a controlled vocabulary!"); throw new Exception("Mandatory field with name '" + key
+ "' doesn't have a value but it is mandatory or has a controlled vocabulary!");
else { else {
if(defaultValue != null && !defaultValue.isEmpty()){ if(defaultValue != null && !defaultValue.isEmpty()) {
value = defaultValue; value = defaultValue;
fieldToValidate.setValue(defaultValue); fieldToValidate.setValue(defaultValue);
} }
return; // there is no need to check other stuff return; // there is no need to check other stuff
} }
switch(dataType){ switch(dataType) {
case String: case String:
case Text: case Text:
if(regex != null && !value.matches(regex)) if(regex != null && !value.matches(regex))
throw new Exception("Field with key '" + key + "' doesn't match the provided regular expression (" + regex + ")!"); throw new Exception("Field with key '" + key + "' doesn't match the provided regular expression ("
+ regex + ")!");
if(hasControlledVocabulary){
if(hasControlledVocabulary) {
List<String> valuesVocabulary = metadataField.getVocabulary().getVocabularyFields();
List<String> valuesVocabulary = metadataField.getVocabulary().getVocabularyFields();
if(valuesVocabulary == null || valuesVocabulary.isEmpty())
return; if(valuesVocabulary == null || valuesVocabulary.isEmpty())
return;
boolean match = false;
for (String valueVocabulary : valuesVocabulary) { boolean match = false;
match = value.equals(valueVocabulary); for(String valueVocabulary : valuesVocabulary) {
if(match) match = value.equals(valueVocabulary);
break; if(match)
break;
}
if(!match)
throw new Exception("Field with key '" + key + "' has a value '" + value
+ "' but it doesn't match any of the vocabulary's values (" + valuesVocabulary + ")!");
} }
if(!match) break;
throw new Exception("Field with key '" + key + "' has a value '" + value + "' but it doesn't match any of the vocabulary's values ("+valuesVocabulary+")!"); case Time:
} if(!isValidDate(value))
throw new Exception("Field with key '" + key + "' doesn't seem a valid time!");
break;
case Time: break;
case Time_Interval:
if(!isValidDate(value))
throw new Exception("Field with key '" + key + "' doesn't seem a valid time!"); String[] timeValues = value.split("/");
for(int i = 0; i < timeValues.length; i++) {
break; String time = timeValues[i];
case Time_Interval:
String[] timeValues = value.split("/");
for (int i = 0; i < timeValues.length; i++) {
String time = timeValues[i];
if(!isValidDate(time))
throw new Exception("Field with key '" + key + "' doesn't seem a valid time interval!");
}
break;
case Times_ListOf:
String[] timeIntervals = value.split(",");
for (int i = 0; i < timeIntervals.length; i++) {
String[] timeIntervalValues = timeIntervals[i].split("/");
if(timeIntervalValues.length > 2)
throw new Exception("Field with key '" + key + "' doesn't seem a valid list of times!");
for (i = 0; i < timeIntervalValues.length; i++) {
String time = timeIntervalValues[i];
if(!isValidDate(time)) if(!isValidDate(time))
throw new Exception("Field with key '" + key + "' doesn't seem a valid list of times!"); throw new Exception("Field with key '" + key + "' doesn't seem a valid time interval!");
} }
}
break;
break; case Times_ListOf:
case Boolean:
String[] timeIntervals = value.split(",");
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) { for(int i = 0; i < timeIntervals.length; i++) {
String[] timeIntervalValues = timeIntervals[i].split("/");
}else if(timeIntervalValues.length > 2)
throw new Exception("Field with key '" + key + "' doesn't seem a valid boolean value!"); throw new Exception("Field with key '" + key + "' doesn't seem a valid list of times!");
for(i = 0; i < timeIntervalValues.length; i++) {
break; String time = timeIntervalValues[i];
case Number: if(!isValidDate(time))
throw new Exception("Field with key '" + key + "' doesn't seem a valid list of times!");
if(!NumberUtils.isNumber(value)) }
throw new Exception("Field's value with key '" + key + "' is not a valid number!"); }
break; break;
case GeoJSON: case Boolean:
try{ if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
new ObjectMapper().readValue(fieldToValidate.getValue(), GeoJsonObject.class);
}catch(Exception e){ } else
throw new Exception("GeoJSON field with key '" + key + "' seems not valid!"); throw new Exception("Field with key '" + key + "' doesn't seem a valid boolean value!");
}
break;
break; case Number:
default:
break; if(!NumberUtils.isNumber(value))
throw new Exception("Field's value with key '" + key + "' is not a valid number!");
break;
case GeoJSON:
try {
new ObjectMapper().readValue(fieldToValidate.getValue(), GeoJsonObject.class);
} catch(Exception e) {
throw new Exception("GeoJSON field with key '" + key + "' seems not valid!");
}
break;
default:
break;
} }
} }
/** /**
* Validate a time date against a formatter * Validate a time date against a formatter
* @param value * @param value
@ -619,22 +629,21 @@ public class Validator {
* @return * @return
*/ */
private static boolean isValidDate(String value) { private static boolean isValidDate(String value) {
try{ try {
DATE_HOUR_MINUTES.parse(value); DATE_HOUR_MINUTES.parse(value);
return true; return true;
}catch(Exception e){ } catch(Exception e) {
logger.debug("failed to parse date with hours and minutes, trying the other one"); logger.debug("failed to parse date with hours and minutes, trying the other one");
try{ try {
DATE_SIMPLE.parse(value); DATE_SIMPLE.parse(value);
return true; return true;
}catch(Exception e2){ } catch(Exception e2) {
logger.warn("failed to parse date with simple format, returning false"); logger.warn("failed to parse date with simple format, returning false");
return false; return false;
} }
} }
} }
} }

View File

@ -20,80 +20,63 @@ import org.json.simple.parser.JSONParser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Let the Product Catalogue Manager write a post in a VRE and alert there is a new product. * Let the Product Catalogue Manager write a post in a VRE and alert there is a new product.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class WritePostCatalogueManagerThread extends Thread { public class WritePostCatalogueManagerThread extends Thread {
private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.ProductCatalogue"; private static Logger logger = LoggerFactory.getLogger(WritePostCatalogueManagerThread.class);
private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.ProductCatalogue";
private static final String NOTIFICATION_MESSAGE = "Dear members,<br>The item '$PRODUCT_TITLE' has been just published by $USER_FULLNAME.<br>You can find it here: $PRODUCT_URL <br>"; private static final String NOTIFICATION_MESSAGE = "Dear members,<br>The item '$PRODUCT_TITLE' has been just published by $USER_FULLNAME.<br>You can find it here: $PRODUCT_URL <br>";
private static final String SOCIAL_SERVICE_APPLICATION_TOKEN = "/2/tokens/generate-application-token"; private static final String SOCIAL_SERVICE_APPLICATION_TOKEN = "/2/tokens/generate-application-token";
private static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST = "/2/posts/write-post-app"; private static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST = "/2/posts/write-post-app";
private static final String MEDIATYPE_JSON = "application/json"; private static final String MEDIATYPE_JSON = "application/json";
private static Logger logger = LoggerFactory.getLogger(WritePostCatalogueManagerThread.class);
private String scope; private String productName;
private String productTitle;
private String productUrl; private String productUrl;
private boolean enableNotification; private boolean enableNotification;
private List<String> hashtags; private List<String> hashtags;
private String userFullName; private String userFullName;
private String token;
/** /**
* @param token
* @param scope
* @param productTitle * @param productTitle
* @param productUrl * @param productUrl
* @param enableNotification * @param enableNotification
* @param hashtags * @param hashtags
* @param userFullName * @param userFullName
*/ */
public WritePostCatalogueManagerThread( public WritePostCatalogueManagerThread(String productName, String productUrl, boolean enableNotification,
String scope, List<String> hashtags, String userFullName) {
String productTitle, String productUrl, boolean enableNotification,
List<String> hashtags, String userFullName, String token) {
super(); super();
this.scope = scope; this.productName = productName;
this.productTitle = productTitle;
this.productUrl = productUrl; this.productUrl = productUrl;
this.enableNotification = enableNotification; this.enableNotification = enableNotification;
this.hashtags = hashtags; this.hashtags = hashtags;
this.userFullName = userFullName; this.userFullName = userFullName;
this.token = token;
} }
@Override @Override
public void run() { public void run() {
try{ try {
logger.info(
// set token and scope "Started request to write application post for new product created. Scope is {} and token is {}****************",
ScopeProvider.instance.set(scope); ScopeProvider.instance.get(), SecurityTokenProvider.instance.get().substring(0, 10));
SecurityTokenProvider.instance.set(token);
logger.info("Started request to write application post "
+ "for new product created. Scope is " + scope + " and "
+ "token is " + token.substring(0, 10) + "****************");
// write // write
writeProductPost( writeProductPost();
productTitle,
productUrl, } catch(Exception e) {
userFullName,
hashtags,
enableNotification
);
}catch(Exception e){
logger.error("Failed to write the post because of the following error ", e); logger.error("Failed to write the post because of the following error ", e);
}finally{ } finally {
SecurityTokenProvider.instance.reset(); SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset(); ScopeProvider.instance.reset();
} }
} }
/** /**
* Send notification to vre members about the created product by writing a post. * Send notification to vre members about the created product by writing a post.
* @param productName the title of the product * @param productName the title of the product
@ -101,40 +84,41 @@ public class WritePostCatalogueManagerThread extends Thread {
* @param hashtags a list of product's hashtags * @param hashtags a list of product's hashtags
* @throws Exception * @throws Exception
*/ */
private static void writeProductPost(String productName, String productUrl, String userFullname, List<String> hashtags, boolean enablePostNotification) throws Exception{ private void writeProductPost() throws Exception {
// discover service endpoint for the social networking library // discover service endpoint for the social networking library
String currentScope = ScopeProvider.instance.get(); String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get(); String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************"); logger.debug("Current scope for writeProductPost is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL(); GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL();
String basePath = socialService.getServiceBasePath(); String basePath = socialService.getServiceBasePath();
if(basePath == null){ if(basePath == null) {
logger.error("Unable to write a post because there is no social networking service available"); logger.error("Unable to write a post because there is no social networking service available");
} else {
}else{
// check base path form // check base path form
basePath = basePath.endsWith("/") ? basePath : basePath + "/"; basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().build();){ try(CloseableHttpClient client = HttpClientBuilder.create().build();) {
String pathTokenApp = basePath + SOCIAL_SERVICE_APPLICATION_TOKEN + "?"
String pathTokenApp = basePath + SOCIAL_SERVICE_APPLICATION_TOKEN + "?" + Constants.GCUBE_TOKEN_PARAMETER + "=" + tokenUser; + Constants.GCUBE_TOKEN_PARAMETER + "=" + tokenUser;
String tokenApp = requireAppToken(client, pathTokenApp); String tokenApp = requireAppToken(client, pathTokenApp);
if(tokenApp != null){ if(tokenApp != null) {
String pathWritePost = basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token=" + tokenApp; String pathWritePost = basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token="
writePost(client, pathWritePost, productName, productUrl, userFullname, hashtags, enablePostNotification); + tokenApp;
writePost(client, pathWritePost, productName, productUrl, userFullName, hashtags,
enableNotification);
} }
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to create a post", e); logger.error("Failed to create a post", e);
} }
} }
} }
/** /**
* Require the application token * Require the application token
* @param tokenUser * @param tokenUser
@ -142,46 +126,48 @@ public class WritePostCatalogueManagerThread extends Thread {
* @param client * @param client
* @return * @return
*/ */
private static String requireAppToken(CloseableHttpClient client, String path){ private static String requireAppToken(CloseableHttpClient client, String path) {
String token = null; String token = null;
try{ try {
HttpResponse response = performRequest(client, path, "{\"app_id\":\"" + APPLICATION_ID_CATALOGUE_MANAGER + "\"}"); HttpResponse response = performRequest(client, path,
"{\"app_id\":\"" + APPLICATION_ID_CATALOGUE_MANAGER + "\"}");
int statusTokenGenerate = response.getStatusLine().getStatusCode(); int statusTokenGenerate = response.getStatusLine().getStatusCode();
if(statusTokenGenerate == HttpURLConnection.HTTP_CREATED){ if(statusTokenGenerate == HttpURLConnection.HTTP_CREATED) {
// extract token // extract token
JSONObject obj = getJSONObject(response); JSONObject obj = getJSONObject(response);
if(((Boolean) obj.get("success"))) if(((Boolean) obj.get("success")))
token = (String)obj.get("result"); token = (String) obj.get("result");
else else
return null; return null;
}else if(statusTokenGenerate == HttpURLConnection.HTTP_MOVED_TEMP } else if(statusTokenGenerate == HttpURLConnection.HTTP_MOVED_TEMP
|| statusTokenGenerate == HttpURLConnection.HTTP_MOVED_PERM || statusTokenGenerate == HttpURLConnection.HTTP_MOVED_PERM
|| statusTokenGenerate == HttpURLConnection.HTTP_SEE_OTHER){ || statusTokenGenerate == HttpURLConnection.HTTP_SEE_OTHER) {
// re-execute // re-execute
Header[] locations = response.getHeaders("Location"); Header[] locations = response.getHeaders("Location");
Header lastLocation = locations[locations.length - 1]; Header lastLocation = locations[locations.length - 1];
String realLocation = lastLocation.getValue(); String realLocation = lastLocation.getValue();
logger.debug("New location is " + realLocation); logger.debug("New location is " + realLocation);
token = requireAppToken(client, realLocation); token = requireAppToken(client, realLocation);
}else } else
return null; return null;
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to retrieve application token", e); logger.error("Failed to retrieve application token", e);
} }
logger.info("Returning app token " + (token != null ? token.substring(0, 10) + "*************************" : null)); logger.info(
"Returning app token " + (token != null ? token.substring(0, 10) + "*************************" : null));
return token; return token;
} }
/** /**
* Write post request * Write post request
* @param client * @param client
@ -192,95 +178,96 @@ public class WritePostCatalogueManagerThread extends Thread {
* @param hashtags * @param hashtags
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static void writePost(CloseableHttpClient client, String path, String productName, String productUrl, String userFullname, List<String> hashtags, private static void writePost(CloseableHttpClient client, String path, String productName, String productUrl,
boolean enablePostNotification) { String userFullname, List<String> hashtags, boolean enablePostNotification) {
try{ try {
// replace // replace
String message = NOTIFICATION_MESSAGE.replace("$PRODUCT_TITLE", productName).replace("$PRODUCT_URL", productUrl).replace("$USER_FULLNAME", userFullname); String message = NOTIFICATION_MESSAGE.replace("$PRODUCT_TITLE", productName)
.replace("$PRODUCT_URL", productUrl).replace("$USER_FULLNAME", userFullname);
if(hashtags != null && !hashtags.isEmpty()) if(hashtags != null && !hashtags.isEmpty())
for (String hashtag : hashtags) { for(String hashtag : hashtags) {
String modifiedHashtag = hashtag.replaceAll(" ", "_").replace("_+", "_"); String modifiedHashtag = hashtag.replaceAll(" ", "_").replace("_+", "_");
if(modifiedHashtag.endsWith("_")) if(modifiedHashtag.endsWith("_"))
modifiedHashtag = modifiedHashtag.substring(0, modifiedHashtag.length() - 1); modifiedHashtag = modifiedHashtag.substring(0, modifiedHashtag.length() - 1);
message += " #" + modifiedHashtag; // ckan accepts tag with empty spaces, we don't message += " #" + modifiedHashtag; // ckan accepts tag with empty spaces, we don't
} }
logger.info("The post that is going to be written is -> " + message); logger.info("The post that is going to be written is -> " + message);
JSONObject objRequest = new JSONObject(); JSONObject objRequest = new JSONObject();
objRequest.put("text", message); objRequest.put("text", message);
objRequest.put("enable_notification", enablePostNotification); objRequest.put("enable_notification", enablePostNotification);
HttpResponse response = performRequest(client, path, objRequest.toJSONString()); HttpResponse response = performRequest(client, path, objRequest.toJSONString());
int statusWritePost = response.getStatusLine().getStatusCode(); int statusWritePost = response.getStatusLine().getStatusCode();
if(statusWritePost == HttpURLConnection.HTTP_CREATED){ if(statusWritePost == HttpURLConnection.HTTP_CREATED) {
// extract token // extract token
JSONObject obj = getJSONObject(response); JSONObject obj = getJSONObject(response);
if(((Boolean) obj.get("success"))) if(((Boolean) obj.get("success")))
logger.info("Post written"); logger.info("Post written");
else else
logger.info("Failed to write the post " + obj.get("message")); logger.info("Failed to write the post " + obj.get("message"));
}else if(statusWritePost == HttpURLConnection.HTTP_MOVED_TEMP } else if(statusWritePost == HttpURLConnection.HTTP_MOVED_TEMP
|| statusWritePost == HttpURLConnection.HTTP_MOVED_PERM || statusWritePost == HttpURLConnection.HTTP_MOVED_PERM
|| statusWritePost == HttpURLConnection.HTTP_SEE_OTHER){ || statusWritePost == HttpURLConnection.HTTP_SEE_OTHER) {
// re-execute // re-execute
Header[] locations = response.getHeaders("Location"); Header[] locations = response.getHeaders("Location");
Header lastLocation = locations[locations.length - 1]; Header lastLocation = locations[locations.length - 1];
String realLocation = lastLocation.getValue(); String realLocation = lastLocation.getValue();
logger.debug("New location is " + realLocation); logger.debug("New location is " + realLocation);
writePost(client, realLocation, productName, productUrl, userFullname, hashtags, enablePostNotification); writePost(client, realLocation, productName, productUrl, userFullname, hashtags,
enablePostNotification);
}else
} else
throw new RuntimeException("Failed to write the post"); throw new RuntimeException("Failed to write the post");
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to retrieve application token", e); logger.error("Failed to retrieve application token", e);
} }
} }
/** /**
* Convert the json response to a map * Convert the json response to a map
* @param response * @param response
* @return * @return
*/ */
private static JSONObject getJSONObject(HttpResponse response){ private static JSONObject getJSONObject(HttpResponse response) {
JSONObject toReturn = null; JSONObject toReturn = null;
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (entity != null) { if(entity != null) {
try { try {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
return (JSONObject)parser.parse(EntityUtils.toString(response.getEntity())); return (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
}catch(Exception e){ } catch(Exception e) {
logger.error("Failed to read json object", e); logger.error("Failed to read json object", e);
} }
} }
logger.trace("Returning " + toReturn); logger.trace("Returning " + toReturn);
return toReturn; return toReturn;
} }
/** /**
* Perform an http request post request with json entity * Perform an http request post request with json entity
* @throws IOException * @throws IOException
* @throws ClientProtocolException * @throws ClientProtocolException
*/ */
private static HttpResponse performRequest(CloseableHttpClient client, String path, String entity) throws ClientProtocolException, IOException{ private static HttpResponse performRequest(CloseableHttpClient client, String path, String entity)
throws ClientProtocolException, IOException {
HttpPost request = new HttpPost(path); HttpPost request = new HttpPost(path);
StringEntity stringEntity = new StringEntity(entity); StringEntity stringEntity = new StringEntity(entity);
stringEntity.setContentType(MEDIATYPE_JSON); stringEntity.setContentType(MEDIATYPE_JSON);
request.setEntity(stringEntity); request.setEntity(stringEntity);
return client.execute(request); return client.execute(request);
} }
} }

View File

@ -10,110 +10,77 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
@Path(Constants.GROUPS) @Path(Constants.GROUPS)
/** /**
* Groups service endpoint. * Groups service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * All checks are demanded to CKAN
* @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class Group { public class Group {
@GET @GET
@Path(Constants.SHOW_METHOD) @Path(Constants.SHOW_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String show(@Context UriInfo uriInfo){ public String show(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_show // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_show
Caller caller = AuthorizationProvider.instance.get(); return Delegator.delegateGet(Constants.GROUP_SHOW, uriInfo);
String context = ScopeProvider.instance.get();
boolean isApplication = CatalogueUtils.isApplicationToken(caller);
return Delegator.delegateGet(caller, context, Constants.GROUP_SHOW, uriInfo, isApplication);
} }
@GET @GET
@Path(Constants.LIST_METHOD) @Path(Constants.LIST_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String list(@Context UriInfo uriInfo){ public String list(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_list // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_list
Caller caller = AuthorizationProvider.instance.get(); return Delegator.delegateGet(Constants.GROUP_LIST, uriInfo);
String context = ScopeProvider.instance.get();
boolean isApplication = CatalogueUtils.isApplicationToken(caller);
return Delegator.delegateGet(caller, context, Constants.GROUP_LIST, uriInfo, isApplication);
} }
@POST @POST
@Path(Constants.CREATE_METHOD) @Path(Constants.CREATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo){ public String create(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.group_create
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_create return Delegator.delegatePost(Constants.GROUP_CREATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.GROUP_CREATE, json, uriInfo, false);
} }
@DELETE @DELETE
@Path(Constants.DELETE_METHOD) @Path(Constants.DELETE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String delete(String json, @Context UriInfo uriInfo){ public String delete(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.group_delete
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_delete return Delegator.delegatePost(Constants.GROUP_DELETE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.GROUP_DELETE, json, uriInfo, false);
} }
@DELETE @DELETE
@Path(Constants.PURGE_METHOD) @Path(Constants.PURGE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String purge(String json, @Context UriInfo uriInfo){ public String purge(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.group_purge
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_purge return Delegator.delegatePost(Constants.GROUP_PURGE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.GROUP_PURGE, json, uriInfo, false);
} }
@POST @POST
@Path(Constants.UPDATE_METHOD) @Path(Constants.UPDATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String update(String json, @Context UriInfo uriInfo){ public String update(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.group_update
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_update return Delegator.delegatePost(Constants.GROUP_UPDATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.GROUP_UPDATE, json, uriInfo, false);
} }
@POST @POST
@Path(Constants.PATCH_METHOD) @Path(Constants.PATCH_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String patch(String json, @Context UriInfo uriInfo){ public String patch(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.patch.group_patch
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.group_patch return Delegator.delegatePost(Constants.GROUP_PATCH, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.GROUP_PATCH, json, uriInfo, false);
} }
} }

View File

@ -7,18 +7,16 @@ import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
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.MultivaluedMap;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.datacatalogue.catalogue.entities.CatalogueItem;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils; import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.ContextUtils;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
import org.gcube.datacatalogue.catalogue.utils.PackageCreatePostActions; import org.gcube.datacatalogue.catalogue.utils.PackageCreatePostActions;
import org.gcube.datacatalogue.catalogue.utils.Validator; import org.gcube.datacatalogue.catalogue.utils.Validator;
@ -29,6 +27,7 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanDataset; import eu.trentorise.opendata.jackan.model.CkanDataset;
@ -36,300 +35,291 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
@Path(Constants.ITEMS) @Path(Constants.ITEMS)
/** /**
* Items service endpoint. * Items service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class Item { public class Item {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Item.class); private static final Logger logger = LoggerFactory.getLogger(Item.class);
@GET private static final String ID_NAME = "id";
@Path(Constants.SHOW_METHOD)
@Produces(MediaType.APPLICATION_JSON) private static final String ID_PATH_PARAM = "id";
public String show(@Context UriInfo uriInfo){
private void applicationChecks(String datasetId, String authorizationErroMessage) throws Exception {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_show if(ContextUtils.isApplication()) {
Caller caller = AuthorizationProvider.instance.get(); logger.debug("Application Token Request");
String context = ScopeProvider.instance.get(); DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
boolean isApplication = CatalogueUtils.isApplicationToken(caller); CkanDataset dataset = dataCatalogue.getDataset(datasetId, CatalogueUtils.fetchSysAPI());
String username = caller.getClient().getId();
String organization = CatalogueUtilMethods.getCKANOrganization();
if(!isApplication) if(organization.equalsIgnoreCase(dataset.getOrganization().getName())
return Delegator.delegateGet(caller, context, Constants.ITEM_SHOW, uriInfo, false); && ContextUtils.getUsername().equals(dataset.getAuthor())) {
else{ return;
try{
DataCatalogue utils = CatalogueUtils.getCatalogue();
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(context);
String datasetId = null;
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(false);
List<String> ids = queryParams.get("id");
if(ids == null || ids.isEmpty())
throw new Exception("'id' field is missing!");
datasetId = ids.get(0);
CkanDataset item = utils.getDataset(datasetId, CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegateGet(caller, context, Constants.ITEM_SHOW, uriInfo, true);
}else
throw new Exception("You are not authorized to access this item");
}catch(Exception e){
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
throw new Exception(authorizationErroMessage);
} }
} }
@SuppressWarnings("unchecked") @GET
@Path("{" + ID_PATH_PARAM + "}")
@Produces(MediaType.APPLICATION_JSON)
public String show(@PathParam(ID_PATH_PARAM) String itemId, @Context UriInfo uriInfo) {
try {
if(itemId.compareTo(Constants.SHOW_METHOD)==0) {
return show(uriInfo);
}
CatalogueItem item = new CatalogueItem();
item.setId(itemId);
return item.read();
} catch(Exception e) {
logger.error("", e);
return CatalogueUtils.createJSONOnFailure(e.toString());
}
}
public String show(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_show
try {
String datasetId = CatalogueUtils.getIdFromUriInfo(ID_NAME, uriInfo);
applicationChecks(datasetId, "You are not authorized to access this item");
} catch(Exception e) {
logger.error("", e);
return CatalogueUtils.createJSONOnFailure(e.toString());
}
return Delegator.delegateGet(Constants.ITEM_SHOW, uriInfo);
}
@POST @POST
@Path(Constants.CREATE_METHOD) @Path(Constants.CREATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo){ @Deprecated
public String oldCreate(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_create return this.create(json, uriInfo);
Caller caller = AuthorizationProvider.instance.get(); }
String username = caller.getClient().getId(); // in case of application token is the label of the token
String context = ScopeProvider.instance.get();
boolean isApplication = CatalogueUtils.isApplicationToken(caller); @SuppressWarnings("unchecked")
@POST
try{ @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create
try {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json); JSONObject obj = (JSONObject) parser.parse(json);
// check base information (and set them if needed) // check base information (and set them if needed)
Validator.checkBaseInformation(obj, caller, isApplication); Validator.checkBaseInformation(obj);
// check resources information (name and url must be there) // check resources information (name and url must be there)
Validator.checkResourcesInformation(obj, caller); Validator.checkResourcesInformation(obj);
// Check if there are profiles here // Check if there are profiles here
List<String> profiles = CatalogueUtils.getProfilesNames(); List<String> profiles = CatalogueUtils.getProfilesNames();
if(profiles != null && !profiles.isEmpty()) if(profiles != null && !profiles.isEmpty()) {
Validator.validateAgainstProfile(obj, caller, profiles, isApplication); Validator.validateAgainstProfile(obj, profiles);
}
obj = (JSONObject)parser.parse(Delegator.delegatePost(caller, context, Constants.ITEM_CREATE, obj.toJSONString(), uriInfo, isApplication));
JSONParser resultParser = new JSONParser();
JSONObject createdJSONObject = (JSONObject) resultParser
.parse(Delegator.delegatePost(Constants.ITEM_CREATE, obj.toJSONString(), uriInfo));
// after creation, if it is ok ... // after creation, if it is ok ...
if((boolean)obj.get(Constants.SUCCESS_KEY)){ if((boolean) createdJSONObject.get(Constants.SUCCESS_KEY)) {
JSONObject result = (JSONObject)obj.get(Constants.RESULT_KEY); JSONObject result = (JSONObject) createdJSONObject.get(Constants.RESULT_KEY);
DataCatalogue utils = CatalogueUtils.getCatalogue(); DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
// add also this information as custom field // add also this information as custom field
String datasetUrl = utils.getUnencryptedUrlFromDatasetIdOrName((String)(result.get(Constants.DATASET_KEY))); String datasetUrl = dataCatalogue
if(datasetUrl != null){ .getUnencryptedUrlFromDatasetIdOrName((String) (result.get(Constants.DATASET_KEY)));
if(datasetUrl != null) {
JSONObject itemUrl = new JSONObject(); JSONObject itemUrl = new JSONObject();
itemUrl.put(Constants.EXTRA_KEY, PackageCreatePostActions.ITEM_URL); itemUrl.put(Constants.EXTRA_KEY, PackageCreatePostActions.ITEM_URL);
itemUrl.put(Constants.EXTRA_VALUE, datasetUrl); itemUrl.put(Constants.EXTRA_VALUE, datasetUrl);
((JSONArray)((JSONObject)obj.get(Constants.RESULT_KEY)).get(Constants.EXTRAS_KEY)).add(itemUrl); ((JSONArray) ((JSONObject) createdJSONObject.get(Constants.RESULT_KEY)).get(Constants.EXTRAS_KEY))
.add(itemUrl);
} }
PackageCreatePostActions packagePostActions = new PackageCreatePostActions(
username, PackageCreatePostActions packagePostActions = new PackageCreatePostActions(datasetUrl,
isApplication, (String) (result.get(Constants.DATASET_KEY)),
datasetUrl, (JSONArray) (result.get(Constants.TAGS_KEY)),
(String)(result.get(Constants.DATASET_KEY)), (String) (result.get(Constants.TITLE_KEY)));
context,
SecurityTokenProvider.instance.get(),
(JSONArray)(result.get(Constants.TAGS_KEY)),
(String)(result.get(Constants.TITLE_KEY))
);
packagePostActions.start(); packagePostActions.start();
} }
return createdJSONObject.toJSONString();
return obj.toJSONString();
} catch(Exception e) {
}catch(Exception e){
logger.error("Something went wrong... ", e); logger.error("Something went wrong... ", e);
if(e instanceof ParseException) if(e instanceof ParseException) {
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!"); return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else } else {
return CatalogueUtils.createJSONOnFailure(e.toString()); return CatalogueUtils.createJSONOnFailure(e.toString());
}
} }
} }
@DELETE @DELETE
@Path("{" + ID_PATH_PARAM + "}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String delete(@PathParam(ID_PATH_PARAM) String itemId, String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.package_delete
try {
if(itemId.compareTo(Constants.DELETE_METHOD)==0) {
itemId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
}
applicationChecks(itemId, "You cannot delete this item");
} catch(Exception e) {
logger.error("", e);
if(e instanceof ParseException) {
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
} else {
return CatalogueUtils.createJSONOnFailure(e.toString());
}
}
return Delegator.delegatePost(Constants.ITEM_DELETE, json, uriInfo);
}
/* @DELETE
@Path(Constants.DELETE_METHOD) @Path(Constants.DELETE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String delete(String json, @Context UriInfo uriInfo){ public String delete(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.package_delete
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_delete try {
Caller caller = AuthorizationProvider.instance.get(); String datasetId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
String context = ScopeProvider.instance.get(); applicationChecks(datasetId, "You cannot delete this item");
String username = caller.getClient().getId(); } catch(Exception e) {
boolean isApplication = CatalogueUtils.isApplicationToken(caller); logger.error("", e);
if(e instanceof ParseException) {
if(!isApplication) return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
return Delegator.delegatePost(caller, context, Constants.ITEM_DELETE, json, uriInfo, isApplication); } else {
else{ return CatalogueUtils.createJSONOnFailure(e.toString());
try {
DataCatalogue utils = CatalogueUtils.getCatalogue();
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(context);
String datasetId = null;
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
datasetId = (String)obj.get("id");
if(datasetId == null || datasetId.isEmpty())
throw new Exception("'id' field is missing!");
CkanDataset item = utils.getDataset(datasetId, CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.ITEM_DELETE, json, uriInfo, true);
}else
throw new Exception("You cannot delete this item");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
} }
return Delegator.delegatePost(Constants.ITEM_DELETE, json, uriInfo);
}
}*/
@DELETE @DELETE
@Path(Constants.PURGE_METHOD) @Path(Constants.PURGE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String purge(String json, @Context UriInfo uriInfo){ public String purge(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.dataset_purge // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.dataset_purge
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
String username = caller.getClient().getId();
try { try {
DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
DataCatalogue utils = CatalogueUtils.getCatalogue(); String username = ContextUtils.getUsername();
boolean isApplication = CatalogueUtils.isApplicationToken(caller); if(!dataCatalogue.isSysAdmin(username)) {
String datasetId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
if(isApplication){ if(ContextUtils.isApplication()) {
applicationChecks(datasetId, "You cannot purge this item");
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts } else {
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(context);
String datasetId = null; String userApiKey = dataCatalogue.getApiKeyFromUsername(ContextUtils.getUsername());
CkanDataset item = dataCatalogue.getDataset(datasetId, userApiKey);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json); String ownerId = item.getCreatorUserId();
String organization = item.getOrganization().getName();
datasetId = (String)obj.get("id");
if(datasetId == null || datasetId.isEmpty())
throw new Exception("'id' field is missing!");
CkanDataset item = utils.getDataset(datasetId, CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.ITEM_PURGE, json, uriInfo, true);
}else
throw new Exception("You cannot purge this item");
}
else{
// if sysadmin, just invoke ckan
if(utils.isSysAdmin(username)){
logger.debug("User " + caller.getClient().getId() + " seems a sysadmin");
return Delegator.delegatePost(caller, context, Constants.ITEM_PURGE, json, uriInfo, false);
}
else{
String datasetId = null;
String ownerId = null;
String organization = null;
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
datasetId = (String)obj.get("id");
if(datasetId == null || datasetId.isEmpty())
throw new Exception("'id' field is missing!");
String userApiKey = utils.getApiKeyFromUsername(username);
CkanDataset item = utils.getDataset(datasetId, userApiKey);
ownerId = item.getCreatorUserId();
organization = item.getOrganization().getName();
// check user role here // check user role here
RolesCkanGroupOrOrg roleInOrganization = RolesCkanGroupOrOrg.convertFromCapacity(utils.getRoleOfUserInOrganization(username, organization, userApiKey)); RolesCkanGroupOrOrg roleInOrganization = RolesCkanGroupOrOrg.convertFromCapacity(
dataCatalogue.getRoleOfUserInOrganization(username, organization, userApiKey));
boolean purged = false; boolean purged = false;
if(roleInOrganization.equals(RolesCkanGroupOrOrg.MEMBER)){ if(roleInOrganization.equals(RolesCkanGroupOrOrg.MEMBER)) {
throw new Exception("You have not enough priviliges to delete item with id " + datasetId); throw new Exception("You have not enough priviliges to delete item with id " + datasetId);
}else if(roleInOrganization.equals(RolesCkanGroupOrOrg.ADMIN)){ } else if(roleInOrganization.equals(RolesCkanGroupOrOrg.ADMIN)) {
purged = utils.deleteProduct(datasetId, userApiKey, true); purged = dataCatalogue.deleteProduct(datasetId, userApiKey, true);
}else{ } else {
// we have an editor here; just check she owns the dataset // we have an editor here; just check she owns the dataset
String userIdCkan = utils.getUserFromApiKey(userApiKey).getId(); String userIdCkan = dataCatalogue.getUserFromApiKey(userApiKey).getId();
if(ownerId.equals(userIdCkan)) if(ownerId.equals(userIdCkan)) {
purged = utils.deleteProduct(datasetId, userApiKey, true); purged = dataCatalogue.deleteProduct(datasetId, userApiKey, true);
else } else {
throw new Exception("Editors can only remove their own items!"); throw new Exception("Editors can only remove their own items!");
}
} }
return CatalogueUtils.createJSONObjectMin(purged, null).toJSONString(); return CatalogueUtils.createJSONObjectMin(purged, null).toJSONString();
} }
} }
} } catch(Exception e) {
catch (Exception e) { logger.error("", e);
logger.error("Something went wrong... ", e); if(e instanceof ParseException) {
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!"); return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else } else {
return CatalogueUtils.createJSONOnFailure(e.toString()); return CatalogueUtils.createJSONOnFailure(e.toString());
}
} }
return Delegator.delegatePost(Constants.ITEM_PURGE, json, uriInfo);
} }
@POST
@Path(Constants.UPDATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String update(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.package_update
try {
String datasetId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
applicationChecks(datasetId, "You cannot update this item");
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(json);
// check base information (and set them if needed)
Validator.checkBaseInformation(obj);
// check resources information (name and url must be there)
Validator.checkResourcesInformation(obj);
// Check if there are profiles here
List<String> profiles = CatalogueUtils.getProfilesNames();
if(profiles != null && !profiles.isEmpty()) {
Validator.validateAgainstProfile(obj, profiles);
}
// TODO PROFILE VALIDATION MUST BE PERFORMED HERE AS WELL return Delegator.delegatePost(Constants.ITEM_UPDATE, json, uriInfo);
// @POST
// @Path(Constants.UPDATE_METHOD) } catch (Exception e) {
// @Consumes(MediaType.APPLICATION_JSON) logger.error("", e);
// @Produces(MediaType.APPLICATION_JSON) if(e instanceof ParseException) {
// public String update(String json){ return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
// } else {
// return CatalogueUtils.createJSONOnFailure(e.toString());
// // 1) Check if there are profiles here }
// // 2) If there are profiles: match the record against them }
// // 3) Else submit it
// }
// // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_update
// Caller caller = AuthorizationProvider.instance.get(); /*
// String context = ScopeProvider.instance.get(); @POST
// return CatalogueUtils.delegatePost(caller, context, Constants.ITEM_UPDATE, json); @Path(Constants.PATCH_METHOD)
// @Consumes(MediaType.APPLICATION_JSON)
// } @Produces(MediaType.APPLICATION_JSON)
public String patch(String json, @Context UriInfo uriInfo) {
// @POST // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.patch.package_patch
// @Path(Constants.PATCH_METHOD) try {
// @Consumes(MediaType.APPLICATION_JSON) return Delegator.delegatePost(Constants.ITEM_PATCH, json, uriInfo);
// @Produces(MediaType.APPLICATION_JSON) } catch (Exception e) {
// public String patch(String json){ logger.error("", e);
// if(e instanceof ParseException) {
// return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
// // 1) Check if there are profiles here } else {
// // 2) If there are profiles: match the record against them return CatalogueUtils.createJSONOnFailure(e.toString());
// // 3) Else submit it }
// }
// // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_patch
// Caller caller = AuthorizationProvider.instance.get(); }
// String context = ScopeProvider.instance.get(); */
// return CatalogueUtils.delegatePost(caller, context, Constants.ITEM_PATCH, json);
//
// }
} }

View File

@ -14,79 +14,72 @@ import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@Path(Constants.PROFILES) @Path(Constants.PROFILES)
/** /**
* Items profiles service endpoint. * Items profiles service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class ItemProfile { public class ItemProfile {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ItemProfile.class); private static final Logger logger = LoggerFactory.getLogger(ItemProfile.class);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@GET @GET
@Path(Constants.PROFILES_NAMES_SHOW) @Path(Constants.PROFILES_NAMES_SHOW)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String showNames(){ public String showNames() {
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
logger.debug("Incoming request for context " + context); logger.debug("Incoming request for context " + context);
// get the names as list // get the names as list
JSONObject json = CatalogueUtils.createJSONObjectMin(true, null); JSONObject json = CatalogueUtils.createJSONObjectMin(true, null);
try {
try{
List<String> names = CatalogueUtils.getProfilesNames(); List<String> names = CatalogueUtils.getProfilesNames();
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (String elem : names) { for(String elem : names) {
try{ try {
array.add(elem); array.add(elem);
}catch(Exception e){ } catch(Exception e) {
} }
} }
json.put(Constants.RESULT_KEY, array); json.put(Constants.RESULT_KEY, array);
}catch(Exception e){ } catch(Exception e) {
json = CatalogueUtils.createJSONObjectMin(false, e.getMessage()); json = CatalogueUtils.createJSONObjectMin(false, e.getMessage());
} }
return json.toJSONString(); return json.toJSONString();
} }
@GET @GET
@Path(Constants.PROFILE_SHOW) @Path(Constants.PROFILE_SHOW)
@Produces({MediaType.APPLICATION_XML, /*MediaType.APPLICATION_JSON*/}) @Produces({MediaType.APPLICATION_XML, /*MediaType.APPLICATION_JSON*/})
public String showSource( public String showSource(
//@DefaultValue(MediaType.APPLICATION_XML) @HeaderParam("Accept") String accept, //@DefaultValue(MediaType.APPLICATION_XML) @HeaderParam("Accept") String accept,
@QueryParam("name") String profileName) throws Exception{ @QueryParam("name") String profileName) throws Exception {
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
logger.debug("Incoming request for context/name " + context+ "/" + profileName); logger.debug("Incoming request for context/name " + context + "/" + profileName);
// TODO Check how this mapping xml-> json works // TODO Check how this mapping xml-> json works
/*if(accept.equals(MediaType.APPLICATION_JSON)){ /*if(accept.equals(MediaType.APPLICATION_JSON)){
org.json.JSONObject xmlJSONObj = XML.toJSONObject(content); org.json.JSONObject xmlJSONObj = XML.toJSONObject(content);
return xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR); return xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
}*/
}*/
return CatalogueUtils.getProfileSource(profileName); return CatalogueUtils.getProfileSource(profileName);
} }
@GET @GET
@Path(Constants.NAMESPACES_SHOW) @Path(Constants.NAMESPACES_SHOW)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String showNamespaces() throws Exception{ public String showNamespaces() throws Exception {
// get the names as list // get the names as list
JSONObject json = CatalogueUtils.createJSONObjectMin(true, null); JSONObject json = CatalogueUtils.createJSONObjectMin(true, null);
JSONArray namespacesJson = new JSONArray(); JSONArray namespacesJson = new JSONArray();
try{ try {
List<NamespaceCategory> namespaces = CatalogueUtils.getNamespaceCategories(); List<NamespaceCategory> namespaces = CatalogueUtils.getNamespaceCategories();
for (NamespaceCategory namespaceCategory : namespaces) { for(NamespaceCategory namespaceCategory : namespaces) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("id", namespaceCategory.getId()); obj.put("id", namespaceCategory.getId());
obj.put("title", namespaceCategory.getTitle()); obj.put("title", namespaceCategory.getTitle());
@ -95,11 +88,10 @@ public class ItemProfile {
namespacesJson.add(obj); namespacesJson.add(obj);
} }
json.put(Constants.RESULT_KEY, namespacesJson); json.put(Constants.RESULT_KEY, namespacesJson);
}catch(Exception e){ } catch(Exception e) {
json = CatalogueUtils.createJSONObjectMin(false, e.getMessage()); json = CatalogueUtils.createJSONObjectMin(false, e.getMessage());
} }
return json.toJSONString(); return json.toJSONString();
} }
} }

View File

@ -7,10 +7,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
@ -18,7 +14,8 @@ import org.gcube.datacatalogue.catalogue.utils.Delegator;
@Path(Constants.LICENSES) @Path(Constants.LICENSES)
/** /**
* Licenses service endpoint. * Licenses service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class License { public class License {
@ -26,13 +23,8 @@ public class License {
@Path(Constants.LIST_METHOD) @Path(Constants.LIST_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String show(@Context UriInfo uriInfo){ public String show(@Context UriInfo uriInfo){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.license_list // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.license_list
Caller caller = AuthorizationProvider.instance.get(); return Delegator.delegateGet(Constants.LICENSES_SHOW, uriInfo);
String context = ScopeProvider.instance.get();
boolean isApplication = CatalogueUtils.isApplicationToken(caller);
return Delegator.delegateGet(caller, context, Constants.LICENSES_SHOW, uriInfo, isApplication);
} }
} }

View File

@ -1,7 +1,5 @@
package org.gcube.datacatalogue.catalogue.ws; package org.gcube.datacatalogue.catalogue.ws;
import java.util.List;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -10,146 +8,115 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
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.MultivaluedMap;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils; import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.ContextUtils;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods; import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanOrganization; import eu.trentorise.opendata.jackan.model.CkanOrganization;
@Path(Constants.ORGANIZATIONS) @Path(Constants.ORGANIZATIONS)
/** /**
* Organizations service endpoint. * Organizations service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class Organization { public class Organization {
private static final Logger logger = LoggerFactory.getLogger(Organization.class);
private static final String ID_NAME = "id";
private void applicationChecks(String organizationId, String authorizationErroMessage) throws Exception {
if(ContextUtils.isApplication()) {
logger.debug("Application Token Request");
DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
String organization = CatalogueUtilMethods.getCKANOrganization();
CkanOrganization fetchedOrganization = dataCatalogue.getOrganizationByName(organizationId);
if(organization.equalsIgnoreCase(fetchedOrganization.getName())) {
return;
}
throw new Exception(authorizationErroMessage);
}
}
@GET @GET
@Path(Constants.SHOW_METHOD) @Path(Constants.SHOW_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String show(@Context UriInfo uriInfo){ public String show(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_show // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_show
Caller caller = AuthorizationProvider.instance.get(); try {
String context = ScopeProvider.instance.get(); String organizationId = CatalogueUtils.getIdFromUriInfo(ID_NAME, uriInfo);
boolean isApplication = CatalogueUtils.isApplicationToken(caller); applicationChecks(organizationId, "You are not authorized to access this organization");
} catch(Exception e) {
if(!isApplication) logger.error("", e);
return Delegator.delegateGet(caller, context, Constants.ORGANIZATION_SHOW, uriInfo, false); return CatalogueUtils.createJSONOnFailure(e.toString());
else{
try{
DataCatalogue utils = CatalogueUtils.getCatalogue();
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(context);
String organizationId = null;
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(false);
List<String> ids = queryParams.get("id");
if(ids == null || ids.isEmpty())
throw new Exception("'id' field is missing!");
organizationId = ids.get(0);
CkanOrganization fetchedOrganization = utils.getOrganizationByName(organizationId);
if(organization.equalsIgnoreCase(fetchedOrganization.getName())){
return Delegator.delegateGet(caller, context, Constants.ORGANIZATION_SHOW, uriInfo, true);
}else
throw new Exception("You are not authorized to access this organization");
}catch(Exception e){
return CatalogueUtils.createJSONOnFailure(e.toString());
}
} }
return Delegator.delegateGet(Constants.ORGANIZATION_SHOW, uriInfo);
} }
@GET @GET
@Path(Constants.LIST_METHOD) @Path(Constants.LIST_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String organizationList(@Context UriInfo uriInfo){ public String organizationList(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_list // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_list
Caller caller = AuthorizationProvider.instance.get(); return Delegator.delegateGet(Constants.ORGANIZATION_LIST, uriInfo);
String context = ScopeProvider.instance.get();
boolean isApplication = CatalogueUtils.isApplicationToken(caller);
return Delegator.delegateGet(caller, context, Constants.ORGANIZATION_LIST, uriInfo, isApplication);
} }
@POST @POST
@Path(Constants.CREATE_METHOD) @Path(Constants.CREATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo){ public String create(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.organization_create
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_create return Delegator.delegatePost(Constants.ORGANIZATION_CREATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.ORGANIZATION_CREATE, json, uriInfo, false);
} }
@DELETE @DELETE
@Path(Constants.DELETE_METHOD) @Path(Constants.DELETE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String delete(String json, @Context UriInfo uriInfo){ public String delete(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.organization_delete
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_delete return Delegator.delegatePost(Constants.ORGANIZATION_DELETE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.ORGANIZATION_DELETE, json, uriInfo, false);
} }
@DELETE @DELETE
@Path(Constants.PURGE_METHOD) @Path(Constants.PURGE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String purge(String json, @Context UriInfo uriInfo){ public String purge(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.organization_purge
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_create return Delegator.delegatePost(Constants.ORGANIZATION_PURGE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.ORGANIZATION_PURGE, json, uriInfo, false);
} }
@POST @POST
@Path(Constants.UPDATE_METHOD) @Path(Constants.UPDATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String update(String json, @Context UriInfo uriInfo){ public String update(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.organization_update
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_update return Delegator.delegatePost(Constants.ORGANIZATION_UPDATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.ORGANIZATION_UPDATE, json, uriInfo, false);
} }
@POST @POST
@Path(Constants.PATCH_METHOD) @Path(Constants.PATCH_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String patch(String json, @Context UriInfo uriInfo){ public String patch(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.patch.organization_patch
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.organization_patch return Delegator.delegatePost(Constants.ORGANIZATION_PATCH, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.ORGANIZATION_PATCH, json, uriInfo, false);
} }
} }

View File

@ -1,8 +1,5 @@
package org.gcube.datacatalogue.catalogue.ws; package org.gcube.datacatalogue.catalogue.ws;
import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
@ -12,22 +9,17 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
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.MultivaluedMap;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils; import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.ContextUtils;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods; import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
import org.glassfish.jersey.media.multipart.FormDataMultiPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanDataset; import eu.trentorise.opendata.jackan.model.CkanDataset;
@ -36,288 +28,148 @@ import eu.trentorise.opendata.jackan.model.CkanResource;
@Path(Constants.RESOURCES) @Path(Constants.RESOURCES)
/** /**
* Resource service endpoint. * Resource service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class Resource { public class Resource {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Resource.class); private static final Logger logger = LoggerFactory.getLogger(Resource.class);
private static final String ID_NAME = "id";
private static final String PACKAGE_ID_NAME = "package_id";
private void applicationChecks(String datasetId, String authorizationErroMessage) throws Exception {
if(ContextUtils.isApplication()) {
logger.debug("Application Token Request");
DataCatalogue dataCatalogue = CatalogueUtils.getCatalogue();
CkanDataset dataset = dataCatalogue.getDataset(datasetId, CatalogueUtils.fetchSysAPI());
String organization = CatalogueUtilMethods.getCKANOrganization();
if(organization.equalsIgnoreCase(dataset.getOrganization().getName())
&& ContextUtils.getUsername().equals(dataset.getAuthor())) {
return;
}
throw new Exception(authorizationErroMessage);
}
}
@GET @GET
@Path(Constants.SHOW_METHOD) @Path(Constants.SHOW_METHOD)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String show(@Context UriInfo uriInfo){ public String show(@Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.resource_show // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.resource_show
Caller caller = AuthorizationProvider.instance.get(); try {
String context = ScopeProvider.instance.get(); String resourceId = CatalogueUtils.getIdFromUriInfo(ID_NAME, uriInfo);
String username = caller.getClient().getId(); CkanResource resource = CatalogueUtils.getCatalogue().getResource(resourceId, CatalogueUtils.fetchSysAPI());
boolean isApplication = CatalogueUtils.isApplicationToken(caller); applicationChecks(resource.getPackageId(), "You are not authorized to access this resource");
} catch(Exception e) {
if(isApplication){ logger.error("", e);
try{ return CatalogueUtils.createJSONOnFailure(e.toString());
DataCatalogue utils = CatalogueUtils.getCatalogue();
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(context);
String resourceId = null;
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(false);
List<String> ids = queryParams.get("id");
if(ids == null || ids.isEmpty())
throw new Exception("'id' field is missing!");
CkanResource resource = utils.getResource(resourceId, CatalogueUtils.fetchSysAPI(context));
CkanDataset item = utils.getDataset(resource.getPackageId(), CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegateGet(caller, context, Constants.RESOURCE_SHOW, uriInfo, true);
}else
throw new Exception("You are not authorized to access this resource");
} catch (Exception e) {
return CatalogueUtils.createJSONOnFailure(e.toString());
}
} }
return Delegator.delegateGet(Constants.RESOURCE_SHOW, uriInfo);
return Delegator.delegateGet(caller, context, Constants.RESOURCE_SHOW, uriInfo, isApplication);
} }
@POST @POST
@Path(Constants.CREATE_METHOD) @Path(Constants.CREATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo){ public String create(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
Caller caller = AuthorizationProvider.instance.get(); try {
String context = ScopeProvider.instance.get(); String datasetId = CatalogueUtils.getIdFromJSONString(PACKAGE_ID_NAME, json);
String username = caller.getClient().getId(); applicationChecks(datasetId, "You cannot add a resource to this item");
boolean isApplication = CatalogueUtils.isApplicationToken(caller); } catch(Exception e) {
logger.error("", e);
if(!isApplication) if(e instanceof ParseException) {
return Delegator.delegatePost(caller, context, Constants.RESOURCE_CREATE, json, uriInfo, isApplication); return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else{ } else {
try { return CatalogueUtils.createJSONOnFailure(e.toString());
DataCatalogue utils = CatalogueUtils.getCatalogue();
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
String organization = CatalogueUtilMethods.getOrganizationNameFromScope(ScopeProvider.instance.get());
String datasetId = null;
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
datasetId = (String)obj.get("package_id"); // within the resource it is defined this way
if(datasetId == null || datasetId.isEmpty())
throw new Exception("'id' field is missing!");
CkanDataset item = utils.getDataset(datasetId, CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.RESOURCE_CREATE, json, uriInfo, true);
}else
throw new Exception("You cannot add a resource to this item");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
} }
return Delegator.delegatePost(Constants.RESOURCE_CREATE, json, uriInfo);
} }
@POST @POST
@Path(Constants.CREATE_METHOD) @Path(Constants.CREATE_METHOD)
@Consumes(MediaType.MULTIPART_FORM_DATA) @Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create( public String create(FormDataMultiPart multiPart, @Context UriInfo uriInfo,
FormDataMultiPart multiPart, @Context UriInfo uriInfo, @Context final HttpServletRequest request) {
@Context final HttpServletRequest request // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
){ try {
String datasetId = (String) multiPart.getField(PACKAGE_ID_NAME).getValue(); // within the resource it is defined this way
Caller caller = AuthorizationProvider.instance.get(); if(datasetId == null || datasetId.isEmpty()) {
String context = ScopeProvider.instance.get(); throw new Exception("'" + PACKAGE_ID_NAME +"' field is missing!");
String username = caller.getClient().getId();
boolean isApplication = CatalogueUtils.isApplicationToken(caller);
if(!isApplication)
return Delegator.delegatePost(caller, context, Constants.RESOURCE_CREATE, multiPart, uriInfo, false);
else{
try {
DataCatalogue utils = CatalogueUtils.getCatalogue();
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
String organization = bean.name().toLowerCase().replace(" ", "_").replace("-", "_");
String datasetId = null;
datasetId = (String)multiPart.getField("package_id").getValue(); // within the resource it is defined this way
if(datasetId == null || datasetId.isEmpty())
throw new Exception("'id' field is missing!");
CkanDataset item = utils.getDataset(datasetId, CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.RESOURCE_CREATE, multiPart, uriInfo, true);
}else
throw new Exception("You cannot add a resource to this item");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
applicationChecks(datasetId, "You cannot add a resource to this item");
} catch(Exception e) {
logger.error("", e);
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
return Delegator.delegatePost(Constants.RESOURCE_CREATE, multiPart, uriInfo);
} }
@DELETE @DELETE
@Path(Constants.DELETE_METHOD) @Path(Constants.DELETE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String delete(String json, @Context UriInfo uriInfo){ public String delete(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.resource_delete
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_delete try {
Caller caller = AuthorizationProvider.instance.get(); String resourceId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
String context = ScopeProvider.instance.get(); CkanResource resource = CatalogueUtils.getCatalogue().getResource(resourceId, CatalogueUtils.fetchSysAPI());
String username = caller.getClient().getId(); applicationChecks(resource.getPackageId(), "You cannot delete this resource");
boolean isApplication = CatalogueUtils.isApplicationToken(caller); } catch(Exception e) {
logger.error("", e);
if(!isApplication) if(e instanceof ParseException) {
return Delegator.delegatePost(caller, context, Constants.RESOURCE_DELETE, json, uriInfo, false); return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else{ } else {
try { return CatalogueUtils.createJSONOnFailure(e.toString());
DataCatalogue utils = CatalogueUtils.getCatalogue();
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
String organization = bean.name().toLowerCase().replace(" ", "_").replace("-", "_");
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
String resourceId = (String)obj.get("id"); // within the resource it is defined this way
if(resourceId == null || resourceId.isEmpty())
throw new Exception("'id' field is missing!");
CkanResource resource = utils.getResource(resourceId, CatalogueUtils.fetchSysAPI(context));
CkanDataset item = utils.getDataset(resource.getPackageId(), CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.RESOURCE_DELETE, json, uriInfo, true);
}else
throw new Exception("You cannot delete this resource");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
} }
return Delegator.delegatePost(Constants.RESOURCE_DELETE, json, uriInfo);
} }
@POST @POST
@Path(Constants.UPDATE_METHOD) @Path(Constants.UPDATE_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String update(String json, @Context UriInfo uriInfo){ public String update(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.resource_update // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.resource_update
Caller caller = AuthorizationProvider.instance.get(); try {
String context = ScopeProvider.instance.get(); String resourceId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
String username = caller.getClient().getId(); CkanResource resource = CatalogueUtils.getCatalogue().getResource(resourceId, CatalogueUtils.fetchSysAPI());
boolean isApplication = CatalogueUtils.isApplicationToken(caller); applicationChecks(resource.getPackageId(), "You cannot update this resource");
} catch(Exception e) {
if(!isApplication) logger.error("", e);
return Delegator.delegatePost(caller, context, Constants.RESOURCE_UPDATE, json, uriInfo, false); if(e instanceof ParseException) {
else{ return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
try { } else {
DataCatalogue utils = CatalogueUtils.getCatalogue(); return CatalogueUtils.createJSONOnFailure(e.toString());
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
String organization = bean.name().toLowerCase().replace(" ", "_").replace("-", "_");
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
String resourceId = (String)obj.get("id"); // within the resource it is defined this way
if(resourceId == null || resourceId.isEmpty())
throw new Exception("'id' field is missing!");
CkanResource resource = utils.getResource(resourceId, CatalogueUtils.fetchSysAPI(context));
CkanDataset item = utils.getDataset(resource.getPackageId(), CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.RESOURCE_UPDATE, json, uriInfo, true);
}else
throw new Exception("You cannot update this resource");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
} }
return Delegator.delegatePost(Constants.RESOURCE_UPDATE, json, uriInfo);
} }
@POST @POST
@Path(Constants.PATCH_METHOD) @Path(Constants.PATCH_METHOD)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String patch(String json, @Context UriInfo uriInfo){ public String patch(String json, @Context UriInfo uriInfo) {
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.patch.resource_patch
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.resource_patch try {
Caller caller = AuthorizationProvider.instance.get(); String resourceId = CatalogueUtils.getIdFromJSONString(ID_NAME, json);
String context = ScopeProvider.instance.get(); CkanResource resource = CatalogueUtils.getCatalogue().getResource(resourceId, CatalogueUtils.fetchSysAPI());
String username = caller.getClient().getId(); applicationChecks(resource.getPackageId(), "You cannot patch this resource");
boolean isApplication = CatalogueUtils.isApplicationToken(caller); } catch(Exception e) {
logger.error("", e);
if(!isApplication) if(e instanceof ParseException) {
return Delegator.delegatePost(caller, context, Constants.RESOURCE_PATCH, json, uriInfo, false); return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else{ } else {
try { return CatalogueUtils.createJSONOnFailure(e.toString());
DataCatalogue utils = CatalogueUtils.getCatalogue();
// in this case we check the author has been filled with the same qualifier of this token: the same qualifier can be used in two different contexts
ScopeBean bean = new ScopeBean(ScopeProvider.instance.get());
String organization = bean.name().toLowerCase().replace(" ", "_").replace("-", "_");
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(json);
String resourceId = (String)obj.get("id"); // within the resource it is defined this way
if(resourceId == null || resourceId.isEmpty())
throw new Exception("'id' field is missing!");
CkanResource resource = utils.getResource(resourceId, CatalogueUtils.fetchSysAPI(context));
CkanDataset item = utils.getDataset(resource.getPackageId(), CatalogueUtils.fetchSysAPI(context));
if(organization.equalsIgnoreCase(item.getOrganization().getName()) && username.equals(item.getAuthor())){
return Delegator.delegatePost(caller, context, Constants.RESOURCE_PATCH, json, uriInfo, true);
}else
throw new Exception("You cannot patch this resource");
} catch (Exception e) {
logger.error("Something went wrong... ", e);
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.toString());
} }
} }
return Delegator.delegatePost(Constants.RESOURCE_PATCH, json, uriInfo);
} }
} }

View File

@ -10,9 +10,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.Delegator;
@ -20,7 +17,8 @@ import org.gcube.datacatalogue.catalogue.utils.Delegator;
@Path(Constants.USERS) @Path(Constants.USERS)
/** /**
* User service endpoint. * User service endpoint.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/ */
public class User { public class User {
@ -28,11 +26,8 @@ public class User {
@Path(Constants.SHOW_METHOD) @Path(Constants.SHOW_METHOD)
@Produces(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN)
public String show(@Context UriInfo uriInfo){ public String show(@Context UriInfo uriInfo){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.user_show // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.user_show
Caller caller = AuthorizationProvider.instance.get(); return Delegator.delegateGet(Constants.USER_SHOW, uriInfo);
String context = ScopeProvider.instance.get();
return Delegator.delegateGet(caller, context, Constants.USER_SHOW, uriInfo, false);
} }
@ -41,11 +36,8 @@ public class User {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String create(String json, @Context UriInfo uriInfo){ public String create(String json, @Context UriInfo uriInfo){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.user_create
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.user_create return Delegator.delegatePost(Constants.USER_CREATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.USER_CREATE, json, uriInfo, false);
} }
@ -54,11 +46,8 @@ public class User {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String delete(String json, @Context UriInfo uriInfo){ public String delete(String json, @Context UriInfo uriInfo){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.user_delete
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.user_delete return Delegator.delegatePost(Constants.USER_DELETE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.USER_DELETE, json, uriInfo, false);
} }
@ -67,11 +56,8 @@ public class User {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String update(String json, @Context UriInfo uriInfo){ public String update(String json, @Context UriInfo uriInfo){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.update.user_update
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.user_update return Delegator.delegatePost(Constants.USER_UPDATE, json, uriInfo);
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return Delegator.delegatePost(caller, context, Constants.USER_UPDATE, json, uriInfo, false);
} }

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="/home/gcube/tomcat/tmp" />
<cache name="profile_readers" maxEntriesLocalHeap="10" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" transactionalMode="off">
<persistence strategy="none" />
</cache>
<cache name="profile_users" maxEntriesLocalHeap="1000"
maxEntriesLocalDisk="1000" diskSpoolBufferSizeMB="20" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>

View File

@ -1,6 +1,4 @@
import java.util.HashMap;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
@ -14,14 +12,12 @@ import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.Closea
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
import eu.trentorise.opendata.jackan.internal.org.apache.http.util.EntityUtils; import eu.trentorise.opendata.jackan.internal.org.apache.http.util.EntityUtils;
/** /**
* An example of publishing in the data catalogue. * An example of publishing in the data catalogue.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/ */
public class CataloguePublishExample { public class CataloguePublishExample {
private static final String LICENSE_LIST = "/api/licenses/list/"; private static final String LICENSE_LIST = "/api/licenses/list/";
private static final String ORGANIZATIONS_LIST = "/api/organizations/list"; private static final String ORGANIZATIONS_LIST = "/api/organizations/list";
private static final String ORGANIZATIONS_SHOW = "/api/organizations/show?id="; private static final String ORGANIZATIONS_SHOW = "/api/organizations/show?id=";
@ -30,72 +26,72 @@ public class CataloguePublishExample {
private static final String GET_SINGLE_PROFILE_XML = "/api/profiles/profile?name="; // GET IT'S XML private static final String GET_SINGLE_PROFILE_XML = "/api/profiles/profile?name="; // GET IT'S XML
private static final String CREATE_ITEM = "/api/items/create"; private static final String CREATE_ITEM = "/api/items/create";
private static final String GET_BACK_ITEM = "/api/items/show?id="; private static final String GET_BACK_ITEM = "/api/items/show?id=";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// you should recover the address via IS... // you should recover the address via IS...
String catalogueServiceEndpoint = "http://catalogue-ws-d-d4s.d4science.org/catalogue-ws/rest"; String catalogueServiceEndpoint = "http://catalogue-ws-d-d4s.d4science.org/catalogue-ws/rest";
// token for the VRE in which the catalogue is present (and where I have at least catalogue-editor role, if // token for the VRE in which the catalogue is present (and where I have at least catalogue-editor role, if
// I'm willing to publish). Note: a catalogue may host more than one VRE/Organization. I'm taking the token I need. // I'm willing to publish). Note: a catalogue may host more than one VRE/Organization. I'm taking the token I need.
String myToken = ""; String myToken = "";
// fetch list of licenses and print it // fetch list of licenses and print it
JSONObject licenseBean = get(catalogueServiceEndpoint + LICENSE_LIST, myToken); JSONObject licenseBean = get(catalogueServiceEndpoint + LICENSE_LIST, myToken);
if(licenseBean != null){ if(licenseBean != null) {
// get "real result" and print couple license id - license name // get "real result" and print couple license id - license name
JSONArray licenses = (JSONArray)licenseBean.get("result"); JSONArray licenses = (JSONArray) licenseBean.get("result");
for(int i = 0; i < licenses.size(); i++){ for(int i = 0; i < licenses.size(); i++) {
JSONObject license = (JSONObject)licenses.get(i); JSONObject license = (JSONObject) licenses.get(i);
System.out.println("License name is " + (String)license.get("id")); System.out.println("License name is " + (String) license.get("id"));
System.out.println("License id is " + (String)license.get("title")); System.out.println("License id is " + (String) license.get("title"));
} }
} }
// do the same with organizations // do the same with organizations
JSONObject organizationsBean = get(catalogueServiceEndpoint + ORGANIZATIONS_LIST, myToken); JSONObject organizationsBean = get(catalogueServiceEndpoint + ORGANIZATIONS_LIST, myToken);
if(organizationsBean != null){ if(organizationsBean != null) {
// get list of organizations (a list of names is returned) // get list of organizations (a list of names is returned)
JSONArray organizations = (JSONArray)organizationsBean.get("result"); JSONArray organizations = (JSONArray) organizationsBean.get("result");
for(int i = 0; i < organizations.size(); i++){ for(int i = 0; i < organizations.size(); i++) {
String organization = (String)organizations.get(i); String organization = (String) organizations.get(i);
System.out.println("Organization name is " + organization); System.out.println("Organization name is " + organization);
} }
} }
// take "nextnext" and show its details // take "nextnext" and show its details
JSONObject organizationBean = get(catalogueServiceEndpoint + ORGANIZATIONS_SHOW + "nextnext", myToken); JSONObject organizationBean = get(catalogueServiceEndpoint + ORGANIZATIONS_SHOW + "nextnext", myToken);
if(organizationBean != null){ if(organizationBean != null) {
// get list of organizations (a list of names is returned) // get list of organizations (a list of names is returned)
JSONObject organization = (JSONObject)organizationBean.get("result"); JSONObject organization = (JSONObject) organizationBean.get("result");
System.out.println("Next Next looks like " + organization.toJSONString()); System.out.println("Next Next looks like " + organization.toJSONString());
} }
// list groups // list groups
JSONObject groupsBean = get(catalogueServiceEndpoint + GROUPS_LIST, myToken); JSONObject groupsBean = get(catalogueServiceEndpoint + GROUPS_LIST, myToken);
if(groupsBean != null){ if(groupsBean != null) {
// get list of organizations (a list of names is returned) // get list of organizations (a list of names is returned)
JSONArray groups = (JSONArray)groupsBean.get("result"); JSONArray groups = (JSONArray) groupsBean.get("result");
for(int i = 0; i < groups.size(); i++){ for(int i = 0; i < groups.size(); i++) {
String group = (String)groups.get(i); String group = (String) groups.get(i);
System.out.println("Group name is " + group); System.out.println("Group name is " + group);
} }
} }
// now, the interesting part for publishing: we need to check for "profiles" metadata, if any, in the context // now, the interesting part for publishing: we need to check for "profiles" metadata, if any, in the context
// Currently, in dev, we have: // Currently, in dev, we have:
// Profile name is Test profile and Categories // Profile name is Test profile and Categories
@ -105,26 +101,26 @@ public class CataloguePublishExample {
// Profile name is EOSCService // Profile name is EOSCService
// Profile name is SoBigData.eu: Application Metadata NextNext // Profile name is SoBigData.eu: Application Metadata NextNext
JSONObject profilesBean = get(catalogueServiceEndpoint + PROFILES_LIST, myToken); JSONObject profilesBean = get(catalogueServiceEndpoint + PROFILES_LIST, myToken);
if(profilesBean != null){ if(profilesBean != null) {
// get list of organizations (a list of names is returned) // get list of organizations (a list of names is returned)
JSONArray profiles = (JSONArray)profilesBean.get("result"); JSONArray profiles = (JSONArray) profilesBean.get("result");
for(int i = 0; i < profiles.size(); i++){ for(int i = 0; i < profiles.size(); i++) {
String profile = (String)profiles.get(i); String profile = (String) profiles.get(i);
System.out.println("Profile name is " + profile); System.out.println("Profile name is " + profile);
} }
} }
// let's check (because it sounds interesting) the "Empty" profile.. (NOTE: xml is returned here) // let's check (because it sounds interesting) the "Empty" profile.. (NOTE: xml is returned here)
String emptyProfile = getXML(catalogueServiceEndpoint + GET_SINGLE_PROFILE_XML + "Empty%20Profile", myToken); String emptyProfile = getXML(catalogueServiceEndpoint + GET_SINGLE_PROFILE_XML + "Empty%20Profile", myToken);
System.out.println("Empty profile looks like \n"); System.out.println("Empty profile looks like \n");
System.out.println(emptyProfile); System.out.println(emptyProfile);
// result is: // result is:
// <metadataformat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" // <metadataformat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// type="EmptyType" xsi:noNamespaceSchemaLocation="https://wiki.gcube-system.org/images_gcube/e/e8/Gcdcmetadataprofilev3.xsd"/> // type="EmptyType" xsi:noNamespaceSchemaLocation="https://wiki.gcube-system.org/images_gcube/e/e8/Gcdcmetadataprofilev3.xsd"/>
// great! Now let's create an item with such profile // great! Now let's create an item with such profile
JSONObject item = new JSONObject(); JSONObject item = new JSONObject();
item.put("name", "my_test_item"); // the name will be part of the uri.. it must be unique item.put("name", "my_test_item"); // the name will be part of the uri.. it must be unique
@ -137,21 +133,21 @@ public class CataloguePublishExample {
// Note: we have profiles, so we must set an extra with key system:type and a proper value(for instance, "EmptyType"). // Note: we have profiles, so we must set an extra with key system:type and a proper value(for instance, "EmptyType").
JSONArray extras = new JSONArray(); JSONArray extras = new JSONArray();
JSONObject sysTypeObject = new JSONObject(); JSONObject sysTypeObject = new JSONObject();
sysTypeObject.put("key","system:type"); sysTypeObject.put("key", "system:type");
sysTypeObject.put("value","EmptyType"); sysTypeObject.put("value", "EmptyType");
JSONObject otherField = new JSONObject(); JSONObject otherField = new JSONObject();
otherField.put("key","Temporal Coverage"); otherField.put("key", "Temporal Coverage");
otherField.put("value","whatever"); otherField.put("value", "whatever");
extras.add(sysTypeObject); extras.add(sysTypeObject);
extras.add(otherField); extras.add(otherField);
item.put("extras", extras); item.put("extras", extras);
item.put("tags", tags); item.put("tags", tags);
JSONObject created = post(catalogueServiceEndpoint + CREATE_ITEM, item, myToken); JSONObject created = post(catalogueServiceEndpoint + CREATE_ITEM, item, myToken);
System.out.println("Result is " + created.toJSONString()); System.out.println("Result is " + created.toJSONString());
} }
/** /**
* Executes an http get request * Executes an http get request
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
@ -159,26 +155,26 @@ public class CataloguePublishExample {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static JSONObject get(String url, String token) throws Exception{ private static JSONObject get(String url, String token) throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpGet req = new HttpGet(url); HttpGet req = new HttpGet(url);
req.setHeader("gcube-token", token); req.setHeader("gcube-token", token);
HttpResponse response = httpClient.execute(req); HttpResponse response = httpClient.execute(req);
if(response.getStatusLine().getStatusCode() != 200) if(response.getStatusLine().getStatusCode() != 200)
throw new Exception("There was an error while serving the request " + response.getStatusLine()); throw new Exception("There was an error while serving the request " + response.getStatusLine());
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject parsedObject = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity())); JSONObject parsedObject = (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
return parsedObject; return parsedObject;
}catch(Exception e){ } catch(Exception e) {
System.err.println("Error while serving request " + e); System.err.println("Error while serving request " + e);
throw e; throw e;
} }
} }
/** /**
* Executes a post request * Executes a post request
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
@ -187,32 +183,32 @@ public class CataloguePublishExample {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static JSONObject post(String url, JSONObject request, String token) throws Exception{ private static JSONObject post(String url, JSONObject request, String token) throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost post = new HttpPost(url); HttpPost post = new HttpPost(url);
post.setHeader("gcube-token", token); post.setHeader("gcube-token", token);
StringEntity params = new StringEntity(request.toJSONString(), ContentType.APPLICATION_JSON); StringEntity params = new StringEntity(request.toJSONString(), ContentType.APPLICATION_JSON);
post.setEntity(params); post.setEntity(params);
HttpResponse response = httpClient.execute(post); HttpResponse response = httpClient.execute(post);
if(response.getStatusLine().getStatusCode() != 200) if(response.getStatusLine().getStatusCode() != 200)
throw new Exception("There was an error while serving the request " + response.getStatusLine()); throw new Exception("There was an error while serving the request " + response.getStatusLine());
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject parsedObject = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity())); JSONObject parsedObject = (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
return parsedObject; return parsedObject;
}catch(Exception e){ } catch(Exception e) {
System.err.println("Error while serving request " + e); System.err.println("Error while serving request " + e);
throw e; throw e;
} }
} }
/** /**
* Executes an http get request to fetch an xml data * Executes an http get request to fetch an xml data
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
@ -220,19 +216,19 @@ public class CataloguePublishExample {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static String getXML(String url, String token) throws Exception{ private static String getXML(String url, String token) throws Exception {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpGet req = new HttpGet(url); HttpGet req = new HttpGet(url);
req.setHeader("gcube-token", token); req.setHeader("gcube-token", token);
HttpResponse response = httpClient.execute(req); HttpResponse response = httpClient.execute(req);
if(response.getStatusLine().getStatusCode() != 200) if(response.getStatusLine().getStatusCode() != 200)
throw new Exception("There was an error while serving the request " + response.getStatusLine()); throw new Exception("There was an error while serving the request " + response.getStatusLine());
return EntityUtils.toString(response.getEntity()); return EntityUtils.toString(response.getEntity());
}catch(Exception e){ } catch(Exception e) {
System.err.println("Error while serving request " + e); System.err.println("Error while serving request " + e);
throw e; throw e;
} }

View File

@ -4,13 +4,11 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import net.sf.ehcache.Cache; import javax.cache.Cache;
import net.sf.ehcache.Element;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.beans.resource.CustomField; import org.gcube.datacatalogue.catalogue.beans.resource.CustomField;
import org.gcube.datacatalogue.catalogue.utils.CachesManager; import org.gcube.datacatalogue.catalogue.utils.CachesManager;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.Constants;
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;
@ -20,22 +18,20 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.junit.Test; import org.junit.Test;
public class JavaTests { public class JavaTests {
//@Test //@Test
public void testGetMetadataNames() throws Exception { public void testGetMetadataNames() throws Exception {
ScopeProvider.instance.set("/gcube/devNext/NextNext"); ScopeProvider.instance.set("/gcube/devNext/NextNext");
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader();
List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles(); List<MetadataProfile> listProfiles = reader.getListOfMetadataProfiles();
System.out.println("Result is " + listProfiles); System.out.println("Result is " + listProfiles);
} }
//@Test //@Test
public void testJSONValueCategory(){ public void testJSONValueCategory() {
JSONObject catJsonObj = new JSONObject(); JSONObject catJsonObj = new JSONObject();
JSONObject value = new JSONObject(); JSONObject value = new JSONObject();
value.put("id", "aaaaa"); value.put("id", "aaaaa");
@ -44,102 +40,99 @@ public class JavaTests {
catJsonObj.put("qualified_name", value); // TODO check catJsonObj.put("qualified_name", value); // TODO check
System.out.println(catJsonObj); System.out.println(catJsonObj);
} }
//@Test //@Test
public void testDate(){ public void testDate() {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String value = "2005-03-01"; String value = "2005-03-01";
try{ try {
Date d = formatter2.parse(value); Date d = formatter2.parse(value);
System.out.println("F1 is " + d.toLocaleString()); System.out.println("F1 is " + d.toLocaleString());
if(d == null) if(d == null)
d = formatter2.parse(value); d = formatter2.parse(value);
}catch(Exception e){ } catch(Exception e) {
System.err.println("failed to parse date " + e); System.err.println("failed to parse date " + e);
} }
} }
//@Test //@Test
public void testSorting(){ public void testSorting() {
List<CustomField> toSort = new ArrayList<CustomField>(); List<CustomField> toSort = new ArrayList<CustomField>();
toSort.add(new CustomField("C", "value1", Integer.MAX_VALUE, 7)); toSort.add(new CustomField("C", "value1", Integer.MAX_VALUE, 7));
toSort.add(new CustomField("E", "value1", Integer.MAX_VALUE, 6)); toSort.add(new CustomField("E", "value1", Integer.MAX_VALUE, 6));
toSort.add(new CustomField("X", "value1", Integer.MAX_VALUE, 3)); toSort.add(new CustomField("X", "value1", Integer.MAX_VALUE, 3));
toSort.add(new CustomField("Z", "value1", 0, 3)); toSort.add(new CustomField("Z", "value1", 0, 3));
toSort.add(new CustomField("A", "value2", 1, 2)); toSort.add(new CustomField("A", "value2", 1, 2));
toSort.add(new CustomField("D", "value1", 2, 4)); toSort.add(new CustomField("D", "value1", 2, 4));
toSort.add(new CustomField("B", "value1", 2, 1)); toSort.add(new CustomField("B", "value1", 2, 1));
Collections.sort(toSort); Collections.sort(toSort);
System.out.println("Sorted list " + toSort); System.out.println("Sorted list " + toSort);
} }
//@Test //@Test
public void testJsonArray(){ public void testJsonArray() {
JSONArray tagsArrayOriginal = new JSONArray(); JSONArray tagsArrayOriginal = new JSONArray();
String tag = ""; String tag = "";
tagsArrayOriginal.add(tag); tagsArrayOriginal.add(tag);
System.out.println(tagsArrayOriginal); System.out.println(tagsArrayOriginal);
} }
//@Test @Test
public void testEHCache() throws Exception{ public void testEHCache() throws Exception {
ScopeProvider.instance.set("/gcube/devNext/NextNext"); ScopeProvider.instance.set("/gcube/devNext/NextNext");
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); Cache<String,DataCalogueMetadataFormatReader> readerCache = CachesManager.getReaderCache();
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
List<String> toReturn = new ArrayList<String>();
DataCalogueMetadataFormatReader reader; DataCalogueMetadataFormatReader reader;
if(profilesCache.isKeyInCache(context)) if(readerCache.containsKey(context)){
reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue(); reader = readerCache.get(context);
else{ }else {
reader = new DataCalogueMetadataFormatReader(); reader = new DataCalogueMetadataFormatReader();
profilesCache.put(new Element(context, reader)); readerCache.put(context, reader);
} }
Thread.sleep(1000 * 60); Thread.sleep(1000 * 60);
} }
//@Test //@Test
public void testJSONObj() throws Exception{ public void testJSONObj() throws Exception {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("c", "d"); obj.put("c", "d");
modifyJSON(obj); modifyJSON(obj);
System.out.println(obj); System.out.println(obj);
} }
private static void modifyJSON(JSONObject obj){ private static void modifyJSON(JSONObject obj) {
obj.put("a", "b"); obj.put("a", "b");
} }
//@Test //@Test
public void testJSONParser() throws ParseException{ public void testJSONParser() throws ParseException {
String jsonString = "{\"help\": \"https://ckan-d-d4s.d4science.org/api/3/action/help_show?name=package_create\", \"success\": true, \"result\": {\"license_title\": \"License Not Specified\", \"maintainer\": \"Giancarlo Panichi\", \"relationships_as_object\": [], \"private\": false, \"maintainer_email\": \"giancarlo.panichi@isti.cnr.it\", \"num_tags\": 2, \"id\": \"e39990ac-c644-49f4-a8a2-6774c2e69b59\", \"metadata_created\": \"2017-03-31T12:07:23.836973\", \"metadata_modified\": \"2017-03-31T12:07:23.836983\", \"author\": \"Costantino Perciante\", \"author_email\": \"costantino.perciante@isti.cnr.it\", \"state\": \"active\", \"version\": \"1\", \"creator_user_id\": \"ab8189ff-6b07-4963-b36a-c83d53d7e49b\", \"type\": \"dataset\", \"resources\": [{\"mimetype\": null, \"cache_url\": null, \"hash\": \"\", \"description\": \"\", \"name\": \"resource A\", \"format\": \"\", \"url\": \"http://www.test.it\", \"datastore_active\": false, \"cache_last_updated\": null, \"package_id\": \"e39990ac-c644-49f4-a8a2-6774c2e69b59\", \"created\": \"2017-03-31T14:07:23.943698\", \"state\": \"active\", \"mimetype_inner\": null, \"last_modified\": null, \"position\": 0, \"revision_id\": \"687a9a8e-4450-4904-9f54-603e6dc71d54\", \"url_type\": null, \"id\": \"fb0c2e90-bc5e-4028-ae61-8cc799814c4d\", \"resource_type\": null, \"size\": null}], \"num_resources\": 1, \"tags\": [{\"vocabulary_id\": null, \"state\": \"active\", \"display_name\": \"Artifact Name-catalogue-ws\", \"id\": \"5b287a15-ff52-429a-8f24-d5f9a4cc0f42\", \"name\": \"Artifact Name-catalogue-ws\"}, {\"vocabulary_id\": null, \"state\": \"active\", \"display_name\": \"Java\", \"id\": \"ad62fb1a-02f7-4ee5-96f8-50c919866552\", \"name\": \"Java\"}], \"groups\": [], \"license_id\": \"notspecified\", \"relationships_as_subject\": [], \"organization\": {\"description\": \"VRE nextnext\", \"created\": \"2016-06-21T17:29:47.479879\", \"title\": \"NextNext\", \"name\": \"nextnext\", \"is_organization\": true, \"state\": \"active\", \"image_url\": \"\", \"revision_id\": \"12264679-247a-4c40-945d-38607c006d2a\", \"type\": \"organization\", \"id\": \"131d8f52-2566-458b-8bc4-04fb57f2580d\", \"approval_status\": \"approved\"}, \"name\": \"test-web-service-3\", \"isopen\": false, \"url\": null, \"notes\": null, \"owner_org\": \"131d8f52-2566-458b-8bc4-04fb57f2580d\", \"extras\": [{\"key\": \"Field/Scope of use\", \"value\": \"Any use\"}, {\"key\": \"UsageMode\", \"value\": \"Download\"}, {\"key\": \"categoryref:artifact_information:Artifact Name\", \"value\": \"catalogue-ws\"}, {\"key\": \"categoryref:artifact_information:Maven Location\", \"value\": \"http://maven.research-infrastructures.eu/nexus/index.html\"}, {\"key\": \"categoryref:artifact_information:Programming language\", \"value\": \"Java\"}, {\"key\": \"categoryref:developer_information:Identifier\", \"value\": \"costantino.perciante\"}, {\"key\": \"categoryref:extra_information:First Release\", \"value\": \"2017-04-03\"}, {\"key\": \"categoryref:extra_information:Size MB\", \"value\": \"2\"}, {\"key\": \"metadatacategory:artifact_information\", \"value\": \"{\\\"id\\\":\\\"artifact_information\\\",\\\"title\\\":\\\"Artifact Information\\\",\\\"description\\\":\\\"Artifact's main information \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatacategory:developer_information\", \"value\": \"{\\\"id\\\":\\\"developer_information\\\",\\\"title\\\":\\\"Developer Information\\\",\\\"description\\\":\\\"This section is about developer(s) information \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatacategory:extra_information\", \"value\": \"{\\\"id\\\":\\\"extra_information\\\",\\\"title\\\":\\\"Extras\\\",\\\"description\\\":\\\"Other information about the artifact \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatatype\", \"value\": \"software_type\"}, {\"key\": \"spatial\", \"value\": \"{\\\"type\\\": \\\"Point\\\",\\\"coordinates\\\": [-3.145, 53.078]}\"}], \"title\": \"title test from web service-3\", \"revision_id\": \"687a9a8e-4450-4904-9f54-603e6dc71d54\"}}"; String jsonString = "{\"help\": \"https://ckan-d-d4s.d4science.org/api/3/action/help_show?name=package_create\", \"success\": true, \"result\": {\"license_title\": \"License Not Specified\", \"maintainer\": \"Giancarlo Panichi\", \"relationships_as_object\": [], \"private\": false, \"maintainer_email\": \"giancarlo.panichi@isti.cnr.it\", \"num_tags\": 2, \"id\": \"e39990ac-c644-49f4-a8a2-6774c2e69b59\", \"metadata_created\": \"2017-03-31T12:07:23.836973\", \"metadata_modified\": \"2017-03-31T12:07:23.836983\", \"author\": \"Costantino Perciante\", \"author_email\": \"costantino.perciante@isti.cnr.it\", \"state\": \"active\", \"version\": \"1\", \"creator_user_id\": \"ab8189ff-6b07-4963-b36a-c83d53d7e49b\", \"type\": \"dataset\", \"resources\": [{\"mimetype\": null, \"cache_url\": null, \"hash\": \"\", \"description\": \"\", \"name\": \"resource A\", \"format\": \"\", \"url\": \"http://www.test.it\", \"datastore_active\": false, \"cache_last_updated\": null, \"package_id\": \"e39990ac-c644-49f4-a8a2-6774c2e69b59\", \"created\": \"2017-03-31T14:07:23.943698\", \"state\": \"active\", \"mimetype_inner\": null, \"last_modified\": null, \"position\": 0, \"revision_id\": \"687a9a8e-4450-4904-9f54-603e6dc71d54\", \"url_type\": null, \"id\": \"fb0c2e90-bc5e-4028-ae61-8cc799814c4d\", \"resource_type\": null, \"size\": null}], \"num_resources\": 1, \"tags\": [{\"vocabulary_id\": null, \"state\": \"active\", \"display_name\": \"Artifact Name-catalogue-ws\", \"id\": \"5b287a15-ff52-429a-8f24-d5f9a4cc0f42\", \"name\": \"Artifact Name-catalogue-ws\"}, {\"vocabulary_id\": null, \"state\": \"active\", \"display_name\": \"Java\", \"id\": \"ad62fb1a-02f7-4ee5-96f8-50c919866552\", \"name\": \"Java\"}], \"groups\": [], \"license_id\": \"notspecified\", \"relationships_as_subject\": [], \"organization\": {\"description\": \"VRE nextnext\", \"created\": \"2016-06-21T17:29:47.479879\", \"title\": \"NextNext\", \"name\": \"nextnext\", \"is_organization\": true, \"state\": \"active\", \"image_url\": \"\", \"revision_id\": \"12264679-247a-4c40-945d-38607c006d2a\", \"type\": \"organization\", \"id\": \"131d8f52-2566-458b-8bc4-04fb57f2580d\", \"approval_status\": \"approved\"}, \"name\": \"test-web-service-3\", \"isopen\": false, \"url\": null, \"notes\": null, \"owner_org\": \"131d8f52-2566-458b-8bc4-04fb57f2580d\", \"extras\": [{\"key\": \"Field/Scope of use\", \"value\": \"Any use\"}, {\"key\": \"UsageMode\", \"value\": \"Download\"}, {\"key\": \"categoryref:artifact_information:Artifact Name\", \"value\": \"catalogue-ws\"}, {\"key\": \"categoryref:artifact_information:Maven Location\", \"value\": \"http://maven.research-infrastructures.eu/nexus/index.html\"}, {\"key\": \"categoryref:artifact_information:Programming language\", \"value\": \"Java\"}, {\"key\": \"categoryref:developer_information:Identifier\", \"value\": \"costantino.perciante\"}, {\"key\": \"categoryref:extra_information:First Release\", \"value\": \"2017-04-03\"}, {\"key\": \"categoryref:extra_information:Size MB\", \"value\": \"2\"}, {\"key\": \"metadatacategory:artifact_information\", \"value\": \"{\\\"id\\\":\\\"artifact_information\\\",\\\"title\\\":\\\"Artifact Information\\\",\\\"description\\\":\\\"Artifact's main information \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatacategory:developer_information\", \"value\": \"{\\\"id\\\":\\\"developer_information\\\",\\\"title\\\":\\\"Developer Information\\\",\\\"description\\\":\\\"This section is about developer(s) information \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatacategory:extra_information\", \"value\": \"{\\\"id\\\":\\\"extra_information\\\",\\\"title\\\":\\\"Extras\\\",\\\"description\\\":\\\"Other information about the artifact \\\\t\\\\t\\\"}\"}, {\"key\": \"metadatatype\", \"value\": \"software_type\"}, {\"key\": \"spatial\", \"value\": \"{\\\"type\\\": \\\"Point\\\",\\\"coordinates\\\": [-3.145, 53.078]}\"}], \"title\": \"title test from web service-3\", \"revision_id\": \"687a9a8e-4450-4904-9f54-603e6dc71d54\"}}";
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(jsonString); JSONObject obj = (JSONObject) parser.parse(jsonString);
JSONArray tags = (JSONArray)((JSONObject)obj.get(Constants.RESULT_KEY)).get(Constants.TAGS_KEY); JSONArray tags = (JSONArray) ((JSONObject) obj.get(Constants.RESULT_KEY)).get(Constants.TAGS_KEY);
System.out.println("Tags are " + tags); System.out.println("Tags are " + tags);
} }
} }

View File

@ -21,9 +21,9 @@ import org.json.simple.JSONObject;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class TestJersey extends JerseyTest { public class TestJersey extends JerseyTest {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestJersey.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestJersey.class);
//@Override //@Override
protected Application configure() { protected Application configure() {
logger.info("Configuring service..."); logger.info("Configuring service...");
@ -32,93 +32,73 @@ public class TestJersey extends JerseyTest {
resourceConfig.register(MultiPartFeature.class); resourceConfig.register(MultiPartFeature.class);
return resourceConfig; return resourceConfig;
} }
//@Override //@Override
public void configureClient(ClientConfig config) { public void configureClient(ClientConfig config) {
logger.info("Configuring client..."); logger.info("Configuring client...");
config.register(MultiPartFeature.class); config.register(MultiPartFeature.class);
} }
//@Test //@Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void test() throws ParseException, IOException { public void test() throws ParseException, IOException {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("test", "value"); obj.put("test", "value");
final JSONObject createResource = target("api/resources/create/") final JSONObject createResource = target("api/resources/create/").request().accept(MediaType.APPLICATION_JSON)
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.json(obj), JSONObject.class); .post(Entity.json(obj), JSONObject.class);
logger.info(createResource.toJSONString()); logger.info(createResource.toJSONString());
} }
//@Test //@Test
public void testFile() throws ParseException, IOException { public void testFile() throws ParseException, IOException {
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("uploadFile",
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("uploadFile",
new File("/Users/costantinoperciante/Desktop/rilascio_tess.doc")); new File("/Users/costantinoperciante/Desktop/rilascio_tess.doc"));
final MultiPart multipart = new FormDataMultiPart() final MultiPart multipart = new FormDataMultiPart().field("foo", "bar").bodyPart(fileDataBodyPart);
.field("foo", "bar")
.bodyPart(fileDataBodyPart); final Response createResource = target("api/resources/create/").request()
final Response createResource =
target("api/resources/create/")
.request()
.post(Entity.entity(multipart, multipart.getMediaType())); .post(Entity.entity(multipart, multipart.getMediaType()));
logger.info(createResource.toString()); logger.info(createResource.toString());
// //
multipart.close(); multipart.close();
} }
//@Test //@Test
public void testProfilesNames() throws ParseException, IOException { public void testProfilesNames() throws ParseException, IOException {
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("uploadFile",
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("uploadFile",
new File("/Users/costantinoperciante/Desktop/rilascio_tess.doc")); new File("/Users/costantinoperciante/Desktop/rilascio_tess.doc"));
final MultiPart multipart = new FormDataMultiPart() final MultiPart multipart = new FormDataMultiPart().field("foo", "bar").bodyPart(fileDataBodyPart);
.field("foo", "bar")
.bodyPart(fileDataBodyPart); final Response createResource = target("api/resources/create/").request()
final Response createResource =
target("api/resources/create/")
.request()
.post(Entity.entity(multipart, multipart.getMediaType())); .post(Entity.entity(multipart, multipart.getMediaType()));
logger.info(createResource.toString()); logger.info(createResource.toString());
// //
multipart.close(); multipart.close();
} }
//@Test //@Test
public void testProfileNames() throws ParseException, IOException { public void testProfileNames() throws ParseException, IOException {
final String profiles = final String profiles = target("api/profiles/profile_names/").queryParam("context", "/gcube/devNext/NextNext")
target("api/profiles/profile_names/") .request().get(String.class);
.queryParam("context", "/gcube/devNext/NextNext")
.request()
.get(String.class);
logger.info("Response is " + profiles); logger.info("Response is " + profiles);
} }
//@Test //@Test
public void testProfileByName() throws ParseException, IOException { public void testProfileByName() throws ParseException, IOException {
final String profiles = final String profiles = target("api/profiles/profile/").queryParam("context", "/gcube/devNext/NextNext")
target("api/profiles/profile/") .queryParam("name", "SoBigData.eu: Dataset Metadata NextNext").request(MediaType.APPLICATION_JSON)
.queryParam("context", "/gcube/devNext/NextNext")
.queryParam("name", "SoBigData.eu: Dataset Metadata NextNext")
.request(MediaType.APPLICATION_JSON)
.get(String.class); .get(String.class);
logger.info("Response is " + profiles); logger.info("Response is " + profiles);
} }
} }

View File

@ -0,0 +1,121 @@
package org.gcube.datacatalogue.catalogue;
/**
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ScopedTest {
private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class);
protected static final String PROPERTIES_FILENAME = "token.properties";
private static final String GCUBE_VARNAME = "GCUBE";
public static final String GCUBE;
private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT";
public static final String GCUBE_DEVNEXT;
private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT";
public static final String GCUBE_DEVNEXT_NEXTNEXT;
public static final String GCUBE_DEVSEC_VARNAME = "GCUBE_DEVSEC";
public static final String GCUBE_DEVSEC;
public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE";
public static final String GCUBE_DEVSEC_DEVVRE;
private static final String GCUBE_DEVNEXT_ANOTHER_USER_VARNAME = "GCUBE_DEVNEXT_ANOTHER_USER";
public static final String GCUBE_DEVNEXT_ANOTHER_USER;
private static final String GCUBE_DEVVRE_ANOTHER_USER_VARNAME = "GCUBE_DEVVRE_ANOTHER_USER";
public static final String GCUBE_DEVVRE_ANOTHER_USER;
public static final String DEFAULT_TEST_SCOPE;
public static final String PARENT_DEFAULT_TEST_SCOPE;
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
public static final String ALTERNATIVE_TEST_SCOPE;
static {
Properties properties = new Properties();
InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
GCUBE = properties.getProperty(GCUBE_VARNAME);
GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME);
GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME);
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
GCUBE_DEVNEXT_ANOTHER_USER = properties.getProperty(GCUBE_DEVNEXT_ANOTHER_USER_VARNAME);
GCUBE_DEVVRE_ANOTHER_USER = properties.getProperty(GCUBE_DEVVRE_ANOTHER_USER_VARNAME);
DEFAULT_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE;
PARENT_DEFAULT_TEST_SCOPE = GCUBE_DEVSEC;
DEFAULT_TEST_SCOPE_ANOTHER_USER = GCUBE_DEVVRE_ANOTHER_USER;
ALTERNATIVE_TEST_SCOPE = GCUBE;
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
ClientInfo clientInfo = authorizationEntry.getClientInfo();
String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier);
AuthorizationProvider.instance.set(caller);
ScopeProvider.instance.set(getCurrentScope(token));
}
@BeforeClass
public static void beforeClass() throws Exception{
setContext(DEFAULT_TEST_SCOPE);
}
@AfterClass
public static void afterClass() throws Exception{
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
}

View File

@ -0,0 +1,23 @@
package org.gcube.datacatalogue.catalogue.entities;
import org.gcube.datacatalogue.catalogue.ScopedTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CatalogueItemTest extends ScopedTest {
private static final Logger logger = LoggerFactory.getLogger(CatalogueItemTest.class);
@Test
public void read() {
String id = "3e7b6924-7851-49fc-a08a-bd8a86d4af62";
CatalogueItem catalogueItem = new CatalogueItem();
catalogueItem.setId(id);
String res = catalogueItem.read();
logger.debug(res);
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="INFO" />
<logger name="org.gcube.datacatalogue" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>