commented class MetadataConverter and WorkspaceEventCollector

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@115154 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2015-05-29 14:24:04 +00:00
parent 4862239803
commit 648012769b
3 changed files with 342 additions and 330 deletions

View File

@ -1,275 +1,295 @@
/** ///**
* // *
*/ // */
package org.gcube.portlets.user.workspace.server.util; //package org.gcube.portlets.user.workspace.server.util;
//
import java.io.ByteArrayInputStream; //import java.io.ByteArrayInputStream;
import java.io.IOException; //import java.io.IOException;
import java.io.InputStream; //import java.io.InputStream;
import java.io.InputStreamReader; //import java.io.InputStreamReader;
import java.io.StringReader; //import java.io.StringReader;
import java.io.StringWriter; //import java.io.StringWriter;
import java.rmi.RemoteException; //import java.rmi.RemoteException;
import java.util.LinkedHashMap; //import java.util.LinkedHashMap;
import java.util.List; //import java.util.List;
import java.util.Map; //import java.util.Map;
//
import javax.xml.transform.OutputKeys; //import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates; //import javax.xml.transform.Templates;
import javax.xml.transform.Transformer; //import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; //import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; //import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult; //import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; //import javax.xml.transform.stream.StreamSource;
//
import org.apache.commons.io.IOUtils; //import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger; //import org.apache.log4j.Logger;
import org.gcube.application.framework.core.genericresources.model.ISGenericResource; //import org.gcube.application.framework.core.genericresources.model.ISGenericResource;
import org.gcube.application.framework.core.session.ASLSession; //import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.util.GenericResource; //import org.gcube.application.framework.core.util.GenericResource;
//
///**
/** // * @author Federico De Faveri defaveri@isti.cnr.it
* @author Federico De Faveri defaveri@isti.cnr.it // *
* // */
*/ //public class MetadataConverter {
public class MetadataConverter { //
// protected Logger logger;
protected Logger logger; //
// /**
/** // * FIXME public as generic resource
* FIXME public as generic resource // */
*/ // protected static final String DEFAULT_XSLT = "/org/gcube/portlets/user/workspace/server/util/resources/xmlverbatim.xsl";
protected static final String DEFAULT_XSLT = "/org/gcube/portlets/user/workspace/server/util/resources/xmlverbatim.xsl"; //
// /**
/** // * Schema name to XSLT generic resource id map.
* Schema name to XSLT generic resource id map. // */
*/ // protected Map<String, String> schemaNameId = new LinkedHashMap<String, String>();
protected Map<String, String> schemaNameId = new LinkedHashMap<String, String>(); //
// /**
/** // * Id to XSLT map cache.
* Id to XSLT map cache. // */
*/ // protected Map<String, String> xsltCache = new LinkedHashMap<String, String>();
protected Map<String, String> xsltCache = new LinkedHashMap<String, String>(); //
// protected boolean setup = false;
protected boolean setup = false; //
// /**
/** // * Create a new MetadataConverter.
* Create a new MetadataConverter. // */
*/ // public MetadataConverter(Logger logger) {
public MetadataConverter(Logger logger) // this.logger = logger;
{ // }
this.logger = logger; //
} // /**
// * Load user xslt.
/** // *
* Load user xslt. // * @param aslSession
* @param aslSession // */
*/ // public synchronized void setup(ASLSession aslSession) {
public synchronized void setup(ASLSession aslSession) // if (!setup)
{ // retrieveXSLTIds(aslSession);
if (!setup) retrieveXSLTIds(aslSession); // }
} //
// public synchronized void isReady() {
public synchronized void isReady() //
{ // }
//
} // /**
// * Convert the given metadata XML to HTML.
// *
/** // * @param schemaName
* Convert the given metadata XML to HTML. // * @param xml
* @param schemaName // * @param aslSession
* @param xml // * @return
* @param aslSession // * @throws Exception
* @return // */
* @throws Exception // public String convert(String schemaName, String xml, ASLSession aslSession)
*/ // throws Exception {
public String convert(String schemaName, String xml, ASLSession aslSession) throws Exception // logger.trace("convert schemaName: " + schemaName + " xml.length: "
{ // + xml.length() + " session: " + aslSession);
logger.trace("convert schemaName: "+schemaName+" xml.length: "+xml.length()+" session: "+aslSession); // // we have to find from the schema name the xslt id
//we have to find from the schema name the xslt id //
// String xsltId = null;
String xsltId = null; //
// // FIXME find a rule for schema names
//FIXME find a rule for schema names // if (schemaNameId.containsKey(schemaName))
if (schemaNameId.containsKey(schemaName)) xsltId = schemaNameId.get(schemaName); // xsltId = schemaNameId.get(schemaName);
else if (schemaNameId.containsKey(schemaName.toLowerCase())) xsltId = schemaNameId.get(schemaName.toLowerCase()); // else if (schemaNameId.containsKey(schemaName.toLowerCase()))
else if (schemaNameId.containsKey(schemaName.toUpperCase())) xsltId = schemaNameId.get(schemaName.toUpperCase()); // xsltId = schemaNameId.get(schemaName.toLowerCase());
// else if (schemaNameId.containsKey(schemaName.toUpperCase()))
// xsltId = schemaNameId.get(schemaName.toUpperCase());
logger.trace("XSLT ID: "+xsltId); //
// logger.trace("XSLT ID: " + xsltId);
String xslt = null; //
// String xslt = null;
if (xsltId != null){ //
try { // if (xsltId != null) {
xslt = getXSL(xsltId, aslSession); // try {
} catch (RemoteException e) { // xslt = getXSL(xsltId, aslSession);
logger.error("Error during xslt retrieving", e); // } catch (RemoteException e) {
} // logger.error("Error during xslt retrieving", e);
} // }
// }
if (xslt == null){ //
//we have to use the default xslt schema // if (xslt == null) {
try { // // we have to use the default xslt schema
xslt = getDefaultXSLT(); // try {
} catch (IOException e) { // xslt = getDefaultXSLT();
logger.error("Error during default xslt retrieving", e); // } catch (IOException e) {
throw e; // logger.error("Error during default xslt retrieving", e);
} // throw e;
} // }
// }
if (xslt == null) throw new Exception("No xslt found"); //
// if (xslt == null)
logger.trace("XSLT: "+xslt); // throw new Exception("No xslt found");
logger.trace("XML: "+xml); //
// logger.trace("XSLT: " + xslt);
String html = transform(xml, xslt); // logger.trace("XML: " + xml);
//
return html; // String html = transform(xml, xslt);
} //
// return html;
/** // }
* Convert the given XML to HTML using the default XSLT. //
* @param xml the XML to convert. // /**
* @param aslSession the user session. // * Convert the given XML to HTML using the default XSLT.
* @return the HTML. // *
* @throws Exception // * @param xml
*/ // * the XML to convert.
public String convertUsingDefault(String xml, ASLSession aslSession) throws Exception // * @param aslSession
{ // * the user session.
logger.trace("convertUsingDefault xml.length: "+xml.length()+" session: "+aslSession); // * @return the HTML.
// * @throws Exception
String xslt = getDefaultXSLT(); // */
// public String convertUsingDefault(String xml, ASLSession aslSession)
if (xslt == null) throw new Exception("No xslt found"); // throws Exception {
// logger.trace("convertUsingDefault xml.length: " + xml.length()
// + " session: " + aslSession);
logger.trace("XSLT: "+xslt); //
logger.trace("XML: "+xml); // String xslt = getDefaultXSLT();
//
String html = transform(xml, xslt); // if (xslt == null)
// throw new Exception("No xslt found");
return html; //
} // logger.trace("XSLT: " + xslt);
// logger.trace("XML: " + xml);
/** //
* Retrieve from user profile all transformation schemas id. // String html = transform(xml, xslt);
* @param aslSession //
*/ // return html;
protected void retrieveXSLTIds(ASLSession aslSession) // }
{ //
logger.trace("retrieveXSLTIds aslSession: "+aslSession); // /**
// * Retrieve from user profile all transformation schemas id.
/* TODO RE-ENABLE // *
* // * @param aslSession
* try{ // */
// protected void retrieveXSLTIds(ASLSession aslSession) {
UserProfile userprofile = new UserProfile(aslSession); // logger.trace("retrieveXSLTIds aslSession: " + aslSession);
logger.trace("Profile loaded"); //
// /*
HashMap<String, String> idSchemaNameMap = userprofile.getMetadataXSLTs(aslSession.getUsername()); // * TODO RE-ENABLE
// *
logger.trace("found "+idSchemaNameMap.size()+" schema maps"); // * try{
// *
for (Map.Entry<String, String> idSchemaName :idSchemaNameMap.entrySet()){ // * UserProfile userprofile = new UserProfile(aslSession);
String id = idSchemaName.getValue(); // * logger.trace("Profile loaded");
String schemaName = idSchemaName.getKey(); // *
// * HashMap<String, String> idSchemaNameMap =
logger.trace("id: "+id+" schema: "+schemaName); // * userprofile.getMetadataXSLTs(aslSession.getUsername());
// *
int uIndex = schemaName.lastIndexOf("-|-"); // * logger.trace("found "+idSchemaNameMap.size()+" schema maps");
if (uIndex>=0) schemaName = schemaName.substring(0, uIndex); // *
// * for (Map.Entry<String, String> idSchemaName
logger.trace("schema: "+schemaName); // * :idSchemaNameMap.entrySet()){ String id = idSchemaName.getValue();
// * String schemaName = idSchemaName.getKey();
schemaNameId.put(schemaName, id); // *
} // * logger.trace("id: "+id+" schema: "+schemaName);
}catch(Throwable e) // *
{ // * int uIndex = schemaName.lastIndexOf("-|-"); if (uIndex>=0) schemaName
//FIXME add exception to log // * = schemaName.substring(0, uIndex);
logger.error("Something wrong retriving the user profile informations"); // *
}*/ // * logger.trace("schema: "+schemaName);
} // *
// * schemaNameId.put(schemaName, id); } }catch(Throwable e) { //FIXME add
/** // * exception to log
* Get a XSLT from is id. // * logger.error("Something wrong retriving the user profile informations"
* If the XSLT is not in the cache then is retrieved from IS. // * ); }
* @param xsltId the XSLT id. // */
* @param aslSession // }
* @return the XSLT. //
* @throws RemoteException // /**
*/ // * Get a XSLT from is id. If the XSLT is not in the cache then is retrieved
protected String getXSL(String xsltId, ASLSession aslSession) throws RemoteException // * from IS.
{ // *
if (xsltCache.containsKey(xsltId)) return xsltCache.get(xsltId); // * @param xsltId
// * the XSLT id.
String xslt = retrieveXSLT(xsltId, aslSession); // * @param aslSession
if (xslt == null) return null; // * @return the XSLT.
// * @throws RemoteException
xsltCache.put(xsltId, xslt); // */
// protected String getXSL(String xsltId, ASLSession aslSession)
return xslt; // throws RemoteException {
} // if (xsltCache.containsKey(xsltId))
// return xsltCache.get(xsltId);
/** //
* Retrieve a schema from his generic resource. // String xslt = retrieveXSLT(xsltId, aslSession);
* @param xslId the schema id. // if (xslt == null)
* @param aslSession // return null;
* @return the schema. //
* @throws RemoteException // xsltCache.put(xsltId, xslt);
*/ //
protected String retrieveXSLT(String xslId, ASLSession aslSession) throws RemoteException // return xslt;
{ // }
GenericResource resource = new GenericResource(aslSession); //
List<ISGenericResource> genericResources = resource.getGenericResourceByID(xslId); // /**
if (genericResources.size() == 0){ // * Retrieve a schema from his generic resource.
logger.error("xsltId resource not found for id "+xslId); // *
return null; // * @param xslId
} // * the schema id.
// * @param aslSession
ISGenericResource xsltResource = genericResources.get(0); // * @return the schema.
String xsltBody = xsltResource.getBody(); // * @throws RemoteException
String xslt = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+xsltBody; // */
// protected String retrieveXSLT(String xslId, ASLSession aslSession)
return xslt; // throws RemoteException {
} // GenericResource resource = new GenericResource(aslSession);
// List<ISGenericResource> genericResources = resource
/** // .getGenericResourceByID(xslId);
* Transform an XML string to HTML string using an XSLT processor. // if (genericResources.size() == 0) {
* @param xml the XML to convert. // logger.error("xsltId resource not found for id " + xslId);
* @param xslt the XML used for the conversion. // return null;
* @return the HTML. // }
* @throws TransformerException if an error occurs. //
*/ // ISGenericResource xsltResource = genericResources.get(0);
protected String transform(String xml, String xslt) throws TransformerException // String xsltBody = xsltResource.getBody();
{ // String xslt = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
TransformerFactory tf = TransformerFactory.newInstance(); // + xsltBody;
StreamSource source = new StreamSource(new ByteArrayInputStream(xslt.getBytes())); //
Templates compiledXSLT = tf.newTemplates(source); // return xslt;
Transformer t = compiledXSLT.newTransformer(); // }
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "true"); //
StringWriter w = new StringWriter(); // /**
t.transform(new StreamSource(new StringReader(xml)), new StreamResult(w)); // * Transform an XML string to HTML string using an XSLT processor.
return w.toString(); // *
} // * @param xml
// * the XML to convert.
protected String getDefaultXSLT() throws IOException // * @param xslt
{ // * the XML used for the conversion.
InputStream is = MetadataConverter.class.getResourceAsStream(DEFAULT_XSLT); // * @return the HTML.
// * @throws TransformerException
if (is == null){ // * if an error occurs.
logger.error("Default XSLT resource not found on "+DEFAULT_XSLT); // */
return null; // protected String transform(String xml, String xslt)
} // throws TransformerException {
// TransformerFactory tf = TransformerFactory.newInstance();
InputStreamReader isr = new InputStreamReader(is); // StreamSource source = new StreamSource(new ByteArrayInputStream(
// xslt.getBytes()));
StringWriter sw = new StringWriter(); // Templates compiledXSLT = tf.newTemplates(source);
// Transformer t = compiledXSLT.newTransformer();
IOUtils.copy(isr, sw); // t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "true");
// StringWriter w = new StringWriter();
return sw.toString(); // t.transform(new StreamSource(new StringReader(xml)),
} // new StreamResult(w));
} // return w.toString();
// }
//
// protected String getDefaultXSLT() throws IOException {
// InputStream is = MetadataConverter.class
// .getResourceAsStream(DEFAULT_XSLT);
//
// if (is == null) {
// logger.error("Default XSLT resource not found on " + DEFAULT_XSLT);
// return null;
// }
//
// InputStreamReader isr = new InputStreamReader(is);
//
// StringWriter sw = new StringWriter();
//
// IOUtils.copy(isr, sw);
//
// return sw.toString();
// }
//}

View File

@ -1,47 +1,47 @@
/** ///**
* // *
*/ // */
package org.gcube.portlets.user.workspace.server.util; //package org.gcube.portlets.user.workspace.server.util;
//
import java.util.concurrent.BlockingQueue; //import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; //import java.util.concurrent.LinkedBlockingQueue;
//
import org.apache.log4j.Logger; //import org.apache.log4j.Logger;
import org.gcube.common.homelibrary.home.workspace.events.WorkspaceEvent; //import org.gcube.common.homelibrary.home.workspace.events.WorkspaceEvent;
import org.gcube.common.homelibrary.home.workspace.events.WorkspaceListener; //import org.gcube.common.homelibrary.home.workspace.events.WorkspaceListener;
//
/** ///**
* @author Federico De Faveri defaveri@isti.cnr.it // * @author Federico De Faveri defaveri@isti.cnr.it
* // *
*/ // */
public class WorkspaceEventCollector implements WorkspaceListener { //public class WorkspaceEventCollector implements WorkspaceListener {
//
protected static Logger logger = Logger.getLogger(WorkspaceEventCollector.class); // protected static Logger logger = Logger.getLogger(WorkspaceEventCollector.class);
//
protected BlockingQueue<WorkspaceEvent> eventsQueue = new LinkedBlockingQueue<WorkspaceEvent>(); // protected BlockingQueue<WorkspaceEvent> eventsQueue = new LinkedBlockingQueue<WorkspaceEvent>();
//
/** // /**
* @param logger // * @param logger
*/ // */
public WorkspaceEventCollector() { // public WorkspaceEventCollector() {
} // }
//
/** // /**
* {@inheritDoc} // * {@inheritDoc}
*/ // */
public void workspaceEvent(WorkspaceEvent event) { // public void workspaceEvent(WorkspaceEvent event) {
try { // try {
logger.trace("EventCollector workspaceEvent"+event); // logger.trace("EventCollector workspaceEvent"+event);
eventsQueue.put(event); // eventsQueue.put(event);
} catch (InterruptedException e) { // } catch (InterruptedException e) {
logger.warn("Error adding a new event", e); // logger.warn("Error adding a new event", e);
} // }
} // }
//
public WorkspaceEvent getEvent() throws InterruptedException // public WorkspaceEvent getEvent() throws InterruptedException
{ // {
logger.trace("getEvent"); // logger.trace("getEvent");
return eventsQueue.take(); // return eventsQueue.take();
} // }
//
} //}

View File

@ -6,14 +6,10 @@ package org.gcube.portlets.user.workspace;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.exceptions.UserNotFoundException;
import org.gcube.common.homelibrary.home.workspace.Workspace; import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
@ -22,10 +18,6 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNot
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem; import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
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.portlets.user.workspace.client.model.InfoContactModel;
import org.gcube.portlets.user.workspace.server.GWTWorkspaceBuilder;
import org.gcube.portlets.user.workspace.server.util.MetadataConverter;
import org.gcube.portlets.user.workspace.server.util.WorkspaceEventCollector;
/** /**
* *