2017-04-26 18:18:32 +02:00
/ * *
*
* /
package org.gcube.datacatalogue.metadatadiscovery.reader ;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.ByteArrayInputStream ;
import java.io.InputStream ;
import java.io.StringReader ;
import java.util.List ;
import javax.xml.bind.JAXBContext ;
import javax.xml.bind.Unmarshaller ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategories ;
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.Logger ;
import org.slf4j.LoggerFactory ;
import org.w3c.dom.Node ;
import org.xml.sax.InputSource ;
/ * *
* The Class NamespaceCategoryReader .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Apr 26 , 2017
* /
public class NamespaceCategoryReader {
private static Logger logger = LoggerFactory . getLogger ( NamespaceCategoryReader . class ) ;
2017-08-03 12:35:38 +02:00
private static final String GENERIC_RESOURCE_NAMESPACES_NAME = " Namespaces Catalogue Categories " ;
private static final String GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE = " DataCatalogueNamespace " ;
2017-04-26 18:18:32 +02:00
private ScopeBean scope ;
private NamespaceCategories namespaces ;
/ * *
* Instantiates a new medata format reader .
*
* @param scope the scope
* @throws Exception the exception
* /
2017-08-03 12:35:38 +02:00
public NamespaceCategoryReader ( ScopeBean scope ) throws Exception {
2017-04-26 18:18:32 +02:00
this . scope = scope ;
2017-08-03 12:35:38 +02:00
readNamespaceCategory ( ) ;
2017-04-26 18:18:32 +02:00
}
/ * *
* Gets the metadata format by id .
*
* @return the metadata format by id
* @throws Exception the exception
* /
2017-08-03 12:35:38 +02:00
private void readNamespaceCategory ( ) throws Exception {
logger . trace ( " Getting Namespace Category " ) ;
2017-04-26 18:18:32 +02:00
if ( this . scope = = null )
throw new Exception ( " Scope is null " ) ;
String scopeString = this . scope . toString ( ) ;
logger . trace ( " read scope: " + scopeString ) ;
namespaces = new NamespaceCategories ( ) ;
try {
logger . info ( " Using scope from ScopeProvider: " + scopeString ) ;
2017-08-03 12:35:38 +02:00
String queryString = QueryForResourceUtil . getGcubeGenericQueryStringForSecondaryTypeAndName ( GENERIC_RESOURCE_NAMESPACES_NAME , GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE ) ;
2017-04-26 18:18:32 +02:00
logger . trace ( " queryString: " + queryString ) ;
Query q = new QueryBox ( queryString ) ;
DiscoveryClient < String > client = client ( ) ;
2017-08-03 12:35:38 +02:00
List < String > appProfile = client . submit ( q ) ;
2017-04-26 18:18:32 +02:00
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
2017-08-03 12:35:38 +02:00
throw new MetadataProfileNotFoundException ( " Your resource is not registered in the infrastructure, the scope is " + scopeString ) ;
2017-04-26 18:18:32 +02:00
else {
String theResource = null ;
try {
theResource = appProfile . get ( 0 ) ;
2017-08-03 12:35:38 +02:00
logger . debug ( " Resource (Namespaces Catalogue Categories) found " ) ;
2017-04-26 18:18:32 +02:00
DocumentBuilder docBuilder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
Node node = docBuilder . parse ( new InputSource ( new StringReader ( theResource ) ) ) . getDocumentElement ( ) ;
XPathHelper helper = new XPathHelper ( node ) ;
logger . debug ( " Unmarshalling it.. " ) ;
readNamespaceCategoryFromResource ( helper ) ;
} catch ( Exception e ) {
logger . error ( " Error while parsing Resource " + theResource + " from the infrastructure, the scope is " + scopeString , e ) ;
}
}
} catch ( Exception e ) {
2017-08-03 12:35:38 +02:00
logger . error ( " Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE +
" from the infrastructure, the scope is " + scopeString , e ) ;
throw new Exception ( " Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE +
" from the infrastructure, the scope is " + scopeString , e ) ;
2017-04-26 18:18:32 +02:00
} finally {
}
}
/ * *
* Read namespace category from resource .
*
* @param helper the helper
* @throws Exception the exception
* /
private void readNamespaceCategoryFromResource ( XPathHelper helper ) throws Exception {
try {
List < String > namespaceSources = helper . evaluate ( " /Resource/Profile/Body/namespaces " ) ;
JAXBContext jaxbContext = JAXBContext . newInstance ( NamespaceCategories . class ) ;
2017-08-03 12:35:38 +02:00
Unmarshaller jaxbUnmarshaller = jaxbContext . createUnmarshaller ( ) ;
2017-04-26 18:18:32 +02:00
2017-08-03 12:35:38 +02:00
if ( namespaceSources = = null | | namespaceSources . size ( ) = = 0 ) {
throw new Exception ( " Resource does not contain <namespaces> <namespace>...</namespace> </namespaces> in the body " ) ;
}
2017-04-26 18:18:32 +02:00
2017-08-03 12:35:38 +02:00
InputStream stream = new ByteArrayInputStream ( namespaceSources . get ( 0 ) . getBytes ( ) ) ;
2017-04-26 18:18:32 +02:00
namespaces = ( NamespaceCategories ) jaxbUnmarshaller . unmarshal ( stream ) ;
} catch ( Exception e ) {
String error = " An error occurred in readNamespaceCategoryFromResource " + e . getMessage ( ) ;
logger . error ( " An error occurred in readNamespaceCategoryFromResource " , e ) ;
throw new Exception ( error ) ;
}
}
/ * *
* Gets the namespaces .
*
* @return the namespaces
* /
public NamespaceCategories getNamespaces ( ) {
return namespaces ;
}
/ * ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /
@Override
public String toString ( ) {
StringBuilder builder = new StringBuilder ( ) ;
builder . append ( " NamespaceCategoryReader [scope= " ) ;
builder . append ( scope ) ;
builder . append ( " , namespaces= " ) ;
builder . append ( namespaces ) ;
builder . append ( " ] " ) ;
return builder . toString ( ) ;
}
}