dataminer-invocation-model/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager....

82 lines
2.4 KiB
Java

/**
*
*/
package org.gcube.data.analysis.dminvocation;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
/**
* 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
* @param mediaType the media type
* @return the byte array output stream
* @throws JAXBException the JAXB exception
*/
public static ByteArrayOutputStream marshaling(DataMinerInvocation dmInvocation, MediaType mediaType) throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
switch (mediaType) {
case ApplicationJSON:
jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, Boolean.TRUE);
case ApplicationXML:
default:
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.getMimeType());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
jaxbMarshaller.marshal(dmInvocation, baos);
return baos;
}
/**
* Unmarshaling.
*
* @param dmInvocationXMLStream the dm invocation xml file
* @param mediaType the media type
* @return the data miner invocation
* @throws JAXBException the JAXB exception
*/
public static DataMinerInvocation unmarshaling(InputStream dmInvocationXMLStream, MediaType mediaType) throws JAXBException
{
//unMarshalingCategories();
JAXBContext jaxbContext = JAXBContext.newInstance(DataMinerInvocation.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
switch (mediaType) {
case ApplicationJSON:
jaxbUnmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, Boolean.TRUE);
case ApplicationXML:
default:
jaxbUnmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.getMimeType());
}
//We had written this file in marshalling example
return (DataMinerInvocation) jaxbUnmarshaller.unmarshal(dmInvocationXMLStream);
}
}