added method to validate (via xml schema) the DataMinerInvocationModel
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/dataminer-invocation-model@174710 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
50af1a5cfe
commit
11392bfa6e
|
@ -25,29 +25,70 @@ import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
|||
import org.xml.sax.SAXException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The Class DataMinerInvocationManager.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
* Dec 4, 2018
|
||||
* Dec 10, 2018
|
||||
*/
|
||||
public class DataMinerInvocationManager {
|
||||
|
||||
private static DataMinerInvocationManager singleInstance = null;
|
||||
private JAXBContext jaxbContext;
|
||||
private Schema schema;
|
||||
|
||||
/*
|
||||
*
|
||||
* JAXBContext is thread safe and should only be created once and reused to avoid the cost of initializing the metadata multiple times.
|
||||
* Marshaller and Unmarshaller are not thread safe, but are lightweight to create and could be created per operation.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Instantiates a new data miner invocation manager.
|
||||
*
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws SAXException the SAX exception
|
||||
*/
|
||||
private DataMinerInvocationManager() throws JAXBException, IOException, SAXException{
|
||||
jaxbContext= JAXBContext.newInstance(DataMinerInvocation.class);
|
||||
schema = generateSchema();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the single instance of DataMinerInvocationManager.
|
||||
*
|
||||
* @return single instance of DataMinerInvocationManager
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws SAXException the SAX exception
|
||||
*/
|
||||
public static DataMinerInvocationManager getInstance() throws JAXBException, IOException, SAXException {
|
||||
|
||||
if (singleInstance == null)
|
||||
singleInstance = new DataMinerInvocationManager();
|
||||
|
||||
return singleInstance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Marshaling.
|
||||
*
|
||||
* @param dmInvocation the dm invocation
|
||||
* @param mediaType the media type
|
||||
* @param validateModel the validate model. If true performs model validation against the xml schema generated for {@link DataMinerInvocation}
|
||||
* @return the byte array output stream
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws SAXException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType) throws JAXBException, IOException, SAXException
|
||||
public ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType, boolean validateModel) throws JAXBException
|
||||
{
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
jaxbMarshaller = generateAndSetSchema(jaxbMarshaller);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
|
||||
if(validateModel)
|
||||
jaxbMarshaller.setSchema(schema);
|
||||
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
|
||||
|
@ -71,20 +112,18 @@ public class DataMinerInvocationManager {
|
|||
/**
|
||||
* Unmarshaling.
|
||||
*
|
||||
* @param dmInvocationXMLStream the dm invocation xml file
|
||||
* @param dmInvocationXMLStream the dm invocation xml stream
|
||||
* @param mediaType the media type
|
||||
* @param validateModel the validate model. If true performs model validation against the xml schema generated for {@link DataMinerInvocation}
|
||||
* @return the data miner invocation
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws SAXException the SAX exception
|
||||
*/
|
||||
public static DataMinerInvocation unmarshaling(InputStream dmInvocationXMLStream, MediaType mediaType) throws JAXBException, IOException, SAXException
|
||||
public DataMinerInvocation unmarshaling(InputStream dmInvocationXMLStream, MediaType mediaType, boolean validateModel) throws JAXBException
|
||||
{
|
||||
//unMarshalingCategories();
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
||||
|
||||
jaxbUnmarshaller = generateAndSetSchema(jaxbUnmarshaller);
|
||||
if(validateModel)
|
||||
jaxbUnmarshaller.setSchema(schema);
|
||||
|
||||
if(mediaType==null)
|
||||
mediaType = MediaType.ApplicationXML;
|
||||
|
@ -97,62 +136,31 @@ public class DataMinerInvocationManager {
|
|||
jaxbUnmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.getMimeType());
|
||||
}
|
||||
|
||||
//We had written this file in marshalling example
|
||||
return (DataMinerInvocation) jaxbUnmarshaller.unmarshal(dmInvocationXMLStream);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate and set schema.
|
||||
* Generate schema.
|
||||
*
|
||||
* @param unmarshaller the unmarshaller
|
||||
* @return the unmarshaller
|
||||
* @return the schema
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws SAXException the SAX exception
|
||||
*/
|
||||
private static Unmarshaller generateAndSetSchema(Unmarshaller unmarshaller) throws JAXBException, IOException, SAXException {
|
||||
private Schema generateSchema() throws JAXBException, IOException, SAXException {
|
||||
|
||||
// generate schema
|
||||
ByteArrayStreamOutputResolver schemaOutput = new ByteArrayStreamOutputResolver();
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||
jaxbContext.generateSchema(schemaOutput);
|
||||
|
||||
// load schema
|
||||
ByteArrayInputStream schemaInputStream = new ByteArrayInputStream(schemaOutput.getSchemaContent());
|
||||
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Schema schema = sf.newSchema(new StreamSource(schemaInputStream));
|
||||
|
||||
// set schema on unmarshaller
|
||||
unmarshaller.setSchema(schema);
|
||||
return unmarshaller;
|
||||
return sf.newSchema(new StreamSource(schemaInputStream));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and set schema.
|
||||
*
|
||||
* @param unmarshaller the unmarshaller
|
||||
* @return the unmarshaller
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws SAXException the SAX exception
|
||||
*/
|
||||
private static Marshaller generateAndSetSchema(Marshaller marshaller) throws JAXBException, IOException, SAXException {
|
||||
|
||||
// generate schema
|
||||
ByteArrayStreamOutputResolver schemaOutput = new ByteArrayStreamOutputResolver();
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||
jaxbContext.generateSchema(schemaOutput);
|
||||
|
||||
// load schema
|
||||
ByteArrayInputStream schemaInputStream = new ByteArrayInputStream(schemaOutput.getSchemaContent());
|
||||
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Schema schema = sf.newSchema(new StreamSource(schemaInputStream));
|
||||
|
||||
// set schema on unmarshaller
|
||||
marshaller.setSchema(schema);
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,17 +31,20 @@ public class DataMinerInvocationTest {
|
|||
|
||||
static Map<String,String> parameters = new HashMap<String,String>();
|
||||
|
||||
static DataMinerInvocationManager dmMng;
|
||||
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
public void init() throws JAXBException, IOException, SAXException{
|
||||
parameters.put("[key1]", "[value1]");
|
||||
parameters.put("[key2]", "[value2]");
|
||||
dmMng = DataMinerInvocationManager.getInstance();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
//@Test
|
||||
public void marshallingTest() throws JAXBException, IOException, SAXException {
|
||||
|
||||
System.out.println(DataMinerInvocationTest.class.getMethods()[0].getName()+" called");
|
||||
//LOADING PARAMETERS
|
||||
// List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
|
||||
// for (String pm : parameters.keySet()) {
|
||||
|
@ -60,29 +63,30 @@ public class DataMinerInvocationTest {
|
|||
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
||||
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||
|
||||
ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||
ByteArrayOutputStream outStreamXML = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
System.out.println(new String(outStreamXML.toByteArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshallingXMLTest() throws JAXBException, IOException, SAXException{
|
||||
System.out.println(DataMinerInvocationTest.class.getMethods()[1].getName()+" called");
|
||||
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml"));
|
||||
|
||||
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationXML);
|
||||
DataMinerInvocation dmInvocation = dmMng.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationXML, true);
|
||||
System.out.println(dmInvocation);
|
||||
|
||||
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||
ByteArrayOutputStream outStreamJSON = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
System.out.println(new String(outStreamJSON.toByteArray()));
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void unmarshallingJSONTest() throws JAXBException, IOException, SAXException{
|
||||
FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationJSONFile, MediaType.ApplicationJSON);
|
||||
DataMinerInvocation dmInvocation = dmMng.unmarshaling(dmInvocationJSONFile, MediaType.ApplicationJSON, true);
|
||||
System.out.println(dmInvocation);
|
||||
|
||||
ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||
ByteArrayOutputStream outStreamXML = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
System.out.println(new String(outStreamXML.toByteArray()));
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
<dataminer-invocation>
|
||||
<operator-id>org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.MPA_INTERSECT</operator-id>
|
||||
<action>RUN</action>
|
||||
<parameters>
|
||||
<input>
|
||||
<param>
|
||||
<key>fileId</key>
|
||||
<value>http://publicLinkToFile</value>
|
||||
</param>
|
||||
<param>
|
||||
<key>param2</key>
|
||||
<value>value2</value>
|
||||
</param>
|
||||
</input>
|
||||
<output/>
|
||||
</parameters>
|
||||
</dataminer-invocation>
|
Loading…
Reference in New Issue