2018-12-04 16:09:51 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.data.analysis.dminvocation;
|
|
|
|
|
2018-12-07 18:45:35 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
2018-12-04 16:09:51 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2018-12-07 18:45:35 +01:00
|
|
|
import java.io.IOException;
|
2018-12-04 16:09:51 +01:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
2018-12-07 18:45:35 +01:00
|
|
|
import javax.xml.XMLConstants;
|
2018-12-04 16:09:51 +01:00
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
import javax.xml.bind.Marshaller;
|
2018-12-07 18:45:35 +01:00
|
|
|
import javax.xml.bind.SchemaOutputResolver;
|
2018-12-04 16:09:51 +01:00
|
|
|
import javax.xml.bind.Unmarshaller;
|
2018-12-07 18:45:35 +01:00
|
|
|
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;
|
2018-12-04 16:09:51 +01:00
|
|
|
|
2018-12-04 18:40:45 +01:00
|
|
|
import org.eclipse.persistence.jaxb.MarshallerProperties;
|
2018-12-04 16:09:51 +01:00
|
|
|
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
2018-12-07 18:45:35 +01:00
|
|
|
import org.xml.sax.SAXException;
|
2018-12-04 16:09:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class DataMinerInvocationManager.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
* Dec 4, 2018
|
|
|
|
*/
|
|
|
|
public class DataMinerInvocationManager {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marshaling.
|
|
|
|
*
|
|
|
|
* @param dmInvocation the dm invocation
|
2018-12-04 18:40:45 +01:00
|
|
|
* @param mediaType the media type
|
2018-12-04 16:09:51 +01:00
|
|
|
* @return the byte array output stream
|
|
|
|
* @throws JAXBException the JAXB exception
|
2018-12-07 18:45:35 +01:00
|
|
|
* @throws SAXException
|
|
|
|
* @throws IOException
|
2018-12-04 16:09:51 +01:00
|
|
|
*/
|
2018-12-07 18:45:35 +01:00
|
|
|
public static ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType) throws JAXBException, IOException, SAXException
|
2018-12-04 16:09:51 +01:00
|
|
|
{
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
|
|
|
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
2018-12-07 18:45:35 +01:00
|
|
|
jaxbMarshaller = generateAndSetSchema(jaxbMarshaller);
|
|
|
|
|
2018-12-04 18:40:45 +01:00
|
|
|
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
|
2018-12-04 18:41:52 +01:00
|
|
|
if(mediaType==null)
|
|
|
|
mediaType = MediaType.ApplicationXML;
|
|
|
|
|
2018-12-04 18:40:45 +01:00
|
|
|
switch (mediaType) {
|
|
|
|
case ApplicationJSON:
|
|
|
|
jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, Boolean.TRUE);
|
|
|
|
case ApplicationXML:
|
|
|
|
default:
|
|
|
|
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.getMimeType());
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:09:51 +01:00
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
jaxbMarshaller.marshal(dmInvocation, baos);
|
|
|
|
return baos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unmarshaling.
|
|
|
|
*
|
|
|
|
* @param dmInvocationXMLStream the dm invocation xml file
|
2018-12-04 18:40:45 +01:00
|
|
|
* @param mediaType the media type
|
2018-12-04 16:09:51 +01:00
|
|
|
* @return the data miner invocation
|
|
|
|
* @throws JAXBException the JAXB exception
|
2018-12-07 18:45:35 +01:00
|
|
|
* @throws IOException Signals that an I/O exception has occurred.
|
|
|
|
* @throws SAXException the SAX exception
|
2018-12-04 16:09:51 +01:00
|
|
|
*/
|
2018-12-07 18:45:35 +01:00
|
|
|
public static DataMinerInvocation unmarshaling(InputStream dmInvocationXMLStream, MediaType mediaType) throws JAXBException, IOException, SAXException
|
2018-12-04 16:09:51 +01:00
|
|
|
{
|
|
|
|
//unMarshalingCategories();
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
|
|
|
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
|
|
|
|
2018-12-07 18:45:35 +01:00
|
|
|
jaxbUnmarshaller = generateAndSetSchema(jaxbUnmarshaller);
|
|
|
|
|
2018-12-04 18:41:52 +01:00
|
|
|
if(mediaType==null)
|
|
|
|
mediaType = MediaType.ApplicationXML;
|
2018-12-04 18:40:45 +01:00
|
|
|
|
|
|
|
switch (mediaType) {
|
|
|
|
case ApplicationJSON:
|
|
|
|
jaxbUnmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, Boolean.TRUE);
|
|
|
|
case ApplicationXML:
|
|
|
|
default:
|
|
|
|
jaxbUnmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.getMimeType());
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:09:51 +01:00
|
|
|
//We had written this file in marshalling example
|
|
|
|
return (DataMinerInvocation) jaxbUnmarshaller.unmarshal(dmInvocationXMLStream);
|
|
|
|
|
|
|
|
}
|
2018-12-07 18:45:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 16:09:51 +01:00
|
|
|
}
|