added marshalling/unmarshalling against xml schema
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/dataminer-invocation-model@174686 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d411cc700d
commit
50af1a5cfe
|
@ -3,16 +3,26 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.data.analysis.dminvocation;
|
package org.gcube.data.analysis.dminvocation;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import javax.xml.XMLConstants;
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
|
import javax.xml.bind.SchemaOutputResolver;
|
||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import javax.xml.transform.Result;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
import javax.xml.validation.Schema;
|
||||||
|
import javax.xml.validation.SchemaFactory;
|
||||||
|
|
||||||
import org.eclipse.persistence.jaxb.MarshallerProperties;
|
import org.eclipse.persistence.jaxb.MarshallerProperties;
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,11 +40,15 @@ public class DataMinerInvocationManager {
|
||||||
* @param mediaType the media type
|
* @param mediaType the media type
|
||||||
* @return the byte array output stream
|
* @return the byte array output stream
|
||||||
* @throws JAXBException the JAXB exception
|
* @throws JAXBException the JAXB exception
|
||||||
|
* @throws SAXException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType) throws JAXBException
|
public static ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType) throws JAXBException, IOException, SAXException
|
||||||
{
|
{
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||||
|
jaxbMarshaller = generateAndSetSchema(jaxbMarshaller);
|
||||||
|
|
||||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
|
|
||||||
if(mediaType==null)
|
if(mediaType==null)
|
||||||
|
@ -61,13 +75,17 @@ public class DataMinerInvocationManager {
|
||||||
* @param mediaType the media type
|
* @param mediaType the media type
|
||||||
* @return the data miner invocation
|
* @return the data miner invocation
|
||||||
* @throws JAXBException the JAXB exception
|
* @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
|
public static DataMinerInvocation unmarshaling(InputStream dmInvocationXMLStream, MediaType mediaType) throws JAXBException, IOException, SAXException
|
||||||
{
|
{
|
||||||
//unMarshalingCategories();
|
//unMarshalingCategories();
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
||||||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
||||||
|
|
||||||
|
jaxbUnmarshaller = generateAndSetSchema(jaxbUnmarshaller);
|
||||||
|
|
||||||
if(mediaType==null)
|
if(mediaType==null)
|
||||||
mediaType = MediaType.ApplicationXML;
|
mediaType = MediaType.ApplicationXML;
|
||||||
|
|
||||||
|
@ -83,4 +101,92 @@ public class DataMinerInvocationManager {
|
||||||
return (DataMinerInvocation) jaxbUnmarshaller.unmarshal(dmInvocationXMLStream);
|
return (DataMinerInvocation) jaxbUnmarshaller.unmarshal(dmInvocationXMLStream);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 Unmarshaller generateAndSetSchema(Unmarshaller unmarshaller) 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Class ByteArrayStreamOutputResolver.
|
||||||
|
*
|
||||||
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||||
|
* Dec 7, 2018
|
||||||
|
*/
|
||||||
|
private static class ByteArrayStreamOutputResolver extends SchemaOutputResolver {
|
||||||
|
|
||||||
|
private ByteArrayOutputStream schemaOutputStream;
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
|
||||||
|
*/
|
||||||
|
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
|
||||||
|
|
||||||
|
schemaOutputStream = new ByteArrayOutputStream();
|
||||||
|
StreamResult result = new StreamResult(schemaOutputStream);
|
||||||
|
|
||||||
|
// We generate single XSD, so generator will not use systemId property
|
||||||
|
// Nevertheless, it validates if it's not null.
|
||||||
|
result.setSystemId("");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the schema content.
|
||||||
|
*
|
||||||
|
* @return the schema content
|
||||||
|
*/
|
||||||
|
public byte[] getSchemaContent() {
|
||||||
|
return schemaOutputStream.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import javax.xml.bind.annotation.XmlAccessType;
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import javax.xml.bind.annotation.XmlTransient;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -39,11 +38,10 @@ public class DataMinerInvocation implements Serializable{
|
||||||
@XmlElement(name = "operator-id")
|
@XmlElement(name = "operator-id")
|
||||||
private String operatorId;
|
private String operatorId;
|
||||||
|
|
||||||
@XmlElement(name = "action")
|
@XmlElement(name = "action", required=true, nillable=false)
|
||||||
@XmlTransient
|
private ActionType actionType;
|
||||||
private ActionType actionType = ActionType.RUN;
|
|
||||||
|
|
||||||
@XmlTransient
|
@XmlElement(name = "parameters", required=true, nillable=false)
|
||||||
private DataMinerParameters parameters;
|
private DataMinerParameters parameters;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import javax.xml.bind.annotation.XmlAccessType;
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import javax.xml.bind.annotation.XmlTransient;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -38,10 +37,9 @@ public class DataMinerParameters implements Serializable{
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 8298755690515099551L;
|
private static final long serialVersionUID = 8298755690515099551L;
|
||||||
@XmlElement(name = "input")
|
@XmlElement(name = "input", required=true)
|
||||||
private DataMinerInputParams input;
|
private DataMinerInputParams input;
|
||||||
@XmlElement(name = "output")
|
@XmlElement(name = "output", required=false)
|
||||||
@XmlTransient
|
|
||||||
private DataMinerOutputParams output;
|
private DataMinerOutputParams output;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,23 +6,18 @@
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
||||||
import org.gcube.data.analysis.dminvocation.MediaType;
|
import org.gcube.data.analysis.dminvocation.MediaType;
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInputParams;
|
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerOutputParams;
|
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerParam;
|
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerParameters;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,47 +39,51 @@ public class DataMinerInvocationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//@Test
|
@Test
|
||||||
public void marshallingTest() throws JAXBException {
|
public void marshallingTest() throws JAXBException, IOException, SAXException {
|
||||||
|
|
||||||
//LOADING PARAMETERS
|
//LOADING PARAMETERS
|
||||||
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
|
// List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
|
||||||
for (String pm : parameters.keySet()) {
|
// for (String pm : parameters.keySet()) {
|
||||||
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
|
// inParams.add(new DataMinerParam(pm, parameters.get(pm)));
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
DataMinerInputParams inputParams = new DataMinerInputParams(inParams);
|
// DataMinerInputParams inputParams = new DataMinerInputParams(inParams);
|
||||||
DataMinerOutputParams outputParams = new DataMinerOutputParams(null);
|
// DataMinerOutputParams outputParams = new DataMinerOutputParams(null);
|
||||||
DataMinerParameters params = new DataMinerParameters(inputParams, outputParams);
|
// DataMinerParameters params = new DataMinerParameters(inputParams, outputParams);
|
||||||
|
|
||||||
DataMinerInvocation dmInvocation = new DataMinerInvocation();
|
DataMinerInvocation dmInvocation = new DataMinerInvocation();
|
||||||
dmInvocation.setOperatorId(operatorID);
|
dmInvocation.setOperatorId(operatorID);
|
||||||
dmInvocation.setParameters(params);
|
//dmInvocation.setParameters(params);
|
||||||
System.out.println(dmInvocation);
|
System.out.println(dmInvocation);
|
||||||
|
|
||||||
ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
||||||
System.out.println(new String(outStreamJSON.toByteArray()));
|
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||||
|
|
||||||
ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||||
System.out.println(new String(outStreamXML.toByteArray()));
|
System.out.println(new String(outStreamXML.toByteArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Test
|
@Test
|
||||||
public void unmarshallingXMLTest() throws JAXBException, FileNotFoundException{
|
public void unmarshallingXMLTest() throws JAXBException, IOException, SAXException{
|
||||||
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml"));
|
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml"));
|
||||||
|
|
||||||
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationXML);
|
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationXML);
|
||||||
System.out.println(dmInvocation);
|
System.out.println(dmInvocation);
|
||||||
|
|
||||||
ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||||
System.out.println(new String(outStreamJSON.toByteArray()));
|
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
//@Test
|
||||||
public void unmarshallingJSONTest() throws JAXBException, FileNotFoundException{
|
public void unmarshallingJSONTest() throws JAXBException, IOException, SAXException{
|
||||||
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||||
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationJSON);
|
DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationJSONFile, MediaType.ApplicationJSON);
|
||||||
System.out.println(dmInvocation);
|
System.out.println(dmInvocation);
|
||||||
|
|
||||||
|
ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML);
|
||||||
|
System.out.println(new String(outStreamXML.toByteArray()));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,3 @@
|
||||||
<dataminer-invocation>
|
<dataminer-invocation>
|
||||||
<operator-id>org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.MPA_INTERSECT</operator-id>
|
<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>
|
</dataminer-invocation>
|
Loading…
Reference in New Issue