From 0ec0aee49f755c7066ce74b21bd18abcc11740b9 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 22 Feb 2018 15:08:22 +0000 Subject: [PATCH] added automatic check of Item Catalogue resource. it is needed for writing posts git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/gcube-ckan-datacatalog@164530 82a268e6-3cf1-43bd-a215-b396298e98cf --- .settings/org.eclipse.wst.common.component | 3 - .../GcubeCkanDataCatalogServiceImpl.java | 11 ++ .../thread/UpdateItemCatalogueResource.java | 129 ++++++++++++++++++ 3 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/UpdateItemCatalogueResource.java diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 9a48316..5fe3680 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,9 +4,6 @@ - - uses - diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java index c3b468e..6fa3f42 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import javax.servlet.http.HttpSession; @@ -14,12 +15,15 @@ import org.apache.commons.codec.binary.Base64; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.provider.UserInfo; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory; import org.gcube.datacatalogue.ckanutillibrary.server.utils.SessionCatalogueAttributes; import org.gcube.datacatalogue.ckanutillibrary.server.utils.UtilMethods; import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService; +import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.thread.UpdateItemCatalogueResource; import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.BeanUserInOrgGroupRole; import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanConnectorAccessPoint; import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.OrganizationBean; @@ -51,6 +55,7 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem public static String CKANCONNECTORLOGOUT = "CkanConnectorLogout"; private static final Log logger = LogFactoryUtil.getLog(GcubeCkanDataCatalogServiceImpl.class); private static final String VIEW_PER_ORGANIZATION_LIFERAY_CUSTOM_FIELD = "ViewPerVRECatalogue"; + private static final Map checksItemCatalogueResourceUpdated = new ConcurrentHashMap(); /** * Instanciate the ckan util library. @@ -91,6 +96,12 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem logger.info("queryStringParameters detected like Base64 and decoded like: "+queryStringParameters); } + ScopeBean scopeBean = new ScopeBean(scopePerCurrentUrl); + if(scopeBean.is(Type.VRE) && !checksItemCatalogueResourceUpdated.containsKey(scopePerCurrentUrl)){ + new UpdateItemCatalogueResource(scopePerCurrentUrl, SessionUtil.getCurrentClientUrl(getThreadLocalRequest())).start(); + checksItemCatalogueResourceUpdated.put(scopePerCurrentUrl, true); + } + if(pathInfoParameter == null || pathInfoParameter.isEmpty()){ pathInfoParameter = isViewPerVREEnabled(); } diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/UpdateItemCatalogueResource.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/UpdateItemCatalogueResource.java new file mode 100644 index 0000000..1fb6a5e --- /dev/null +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/UpdateItemCatalogueResource.java @@ -0,0 +1,129 @@ +package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.thread; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; + +import java.io.StringReader; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.portlets.widgets.ckandatapublisherwidget.server.threads.WritePostCatalogueManagerThread; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + + +/** + * This thread is scheduled to update the resource related to org.gcube.datacatalogue.ProductCatalogue + * {org.gcube.portlets.widgets.ckandatapublisherwidget.server.threads} + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class UpdateItemCatalogueResource extends Thread{ + + private String currentScope; + private String cleanUrl; + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UpdateItemCatalogueResource.class); + + + /** + * @param currentScope + * @param cleanUrl + */ + public UpdateItemCatalogueResource(String currentScope, String cleanUrl) { + super(); + this.currentScope = currentScope; + this.cleanUrl = cleanUrl; + } + + @Override + public void run() { + + try{ + + if(cleanUrl == null || cleanUrl.isEmpty() || currentScope==null || currentScope.isEmpty()){ + logger.warn("One or more arguments {}{} is wrong. Exiting", currentScope, cleanUrl); + return; + } + + ScopeBean scope = new ScopeBean(currentScope); + if(!scope.is(Type.VRE)){ + logger.warn("{} is not a VRE scope", currentScope); + return; + } + + // set the scope of the root infrastructure + String rootInfrastructure = getRootScope(); + ScopeProvider.instance.set("/"+rootInfrastructure); + + // check if the resource is present + Query q = new QueryBox("for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Body/AppId/string() " + + " eq '" + WritePostCatalogueManagerThread.APPLICATION_ID_CATALOGUE_MANAGER + "'" + + "return $profile"); + + DiscoveryClient client = client(); + List appProfile = client.submit(q); + + if (appProfile == null || appProfile.size() == 0) + throw new Exception("this applicationProfile is not registered in the infrastructure"); + else{ + + String elem = appProfile.get(0); + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + + // look for the scope + List currValue = null; + currValue = helper.evaluate(String.format("/Resource/Profile/Body/EndPoint/Scope/text()[.='%s']", scope)); + logger.debug("Result is " + currValue); + + if (currValue == null || currValue.isEmpty()) { + logger.info("Adding the following url " + cleanUrl); + + String endpoint2Add = ""+currentScope+""+cleanUrl+""; + GenericResource toUpdate = clientFor(GenericResource.class).submit(q).get(0); + try { + docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Element body = toUpdate.profile().body(); + Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(endpoint2Add))).getDocumentElement(); + fragmentNode = body.getOwnerDocument().importNode(fragmentNode, true); + body.appendChild(fragmentNode); + } catch (Exception e) { + logger.error("Error while updating the generic resource", e); + } + RegistryPublisher rp = RegistryPublisherFactory.create(); + rp.update(toUpdate); + logger.info("Resource updated!"); + } + } + }catch(Exception e){ + logger.error("Failed to execute this check", e); + }finally{ + ScopeProvider.instance.reset(); + } + + } + + private String getRootScope() throws Exception{ + + if(currentScope == null || currentScope.isEmpty()) + throw new Exception("Scope was not specified"); + + return currentScope.split("/")[1]; + + } +}