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
This commit is contained in:
Costantino Perciante 2018-02-22 15:08:22 +00:00
parent be662be2cf
commit 0ec0aee49f
3 changed files with 140 additions and 3 deletions

View File

@ -4,9 +4,6 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="grsf-common-library-1.0.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/grsf-common-library/grsf-common-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="gcube-ckan-datacatalog"/>
<property name="java-output-path" value="/gcube-ckan-datacatalog/target/gcube-ckan-datacatalog-1.0.0-SNAPSHOT/WEB-INF/classes"/>
</wb-module>

View File

@ -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<String, Boolean> checksItemCatalogueResourceUpdated = new ConcurrentHashMap<String, Boolean>();
/**
* 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();
}

View File

@ -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<String> client = client();
List<String> 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<String> 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 = "<EndPoint><Scope>"+currentScope+"</Scope><URL>"+cleanUrl+"</URL></EndPoint>";
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];
}
}