From 11392bfa6e280acb8bca5fc47d338150b87cb8c6 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 10 Dec 2018 11:22:11 +0000 Subject: [PATCH] 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 --- .../DataMinerInvocationManager.java | 108 ++++++++++-------- src/test/java/DataMinerInvocationTest.java | 22 ++-- src/test/resources/DataMinerInvocation.xml | 14 +++ 3 files changed, 85 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java b/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java index 91ce4a4..2b167c6 100644 --- a/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java +++ b/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java @@ -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; - } /** diff --git a/src/test/java/DataMinerInvocationTest.java b/src/test/java/DataMinerInvocationTest.java index e60d3bb..c3a6506 100644 --- a/src/test/java/DataMinerInvocationTest.java +++ b/src/test/java/DataMinerInvocationTest.java @@ -31,17 +31,20 @@ public class DataMinerInvocationTest { static Map parameters = new HashMap(); + 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 inParams = new ArrayList(); // 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())); } diff --git a/src/test/resources/DataMinerInvocation.xml b/src/test/resources/DataMinerInvocation.xml index 23353aa..15d7a53 100644 --- a/src/test/resources/DataMinerInvocation.xml +++ b/src/test/resources/DataMinerInvocation.xml @@ -1,3 +1,17 @@ org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.MPA_INTERSECT + RUN + + + + fileId + http://publicLinkToFile + + + param2 + value2 + + + + \ No newline at end of file