diff --git a/.classpath b/.classpath index 8233959..bac9cef 100644 --- a/.classpath +++ b/.classpath @@ -20,6 +20,7 @@ + diff --git a/.project b/.project index db26b4c..fdb8ee4 100644 --- a/.project +++ b/.project @@ -5,6 +5,11 @@ + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder @@ -15,9 +20,17 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index e52a276..443e085 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,8 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..fe579c5 --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,5 @@ + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..1b22d70 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/pom.xml b/pom.xml index 7cbc7a6..5827a79 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,11 @@ provided + + org.eclipse.persistence + org.eclipse.persistence.moxy + 2.5.2 + 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 2eb5847..51a20bb 100644 --- a/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java +++ b/src/main/java/org/gcube/data/analysis/dminvocation/DataMinerInvocationManager.java @@ -11,6 +11,7 @@ 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; @@ -22,19 +23,28 @@ import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; */ 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) throws JAXBException + 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, true); + 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; @@ -45,15 +55,25 @@ public class DataMinerInvocationManager { * 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) throws JAXBException + 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); diff --git a/src/main/java/org/gcube/data/analysis/dminvocation/MediaType.java b/src/main/java/org/gcube/data/analysis/dminvocation/MediaType.java new file mode 100644 index 0000000..56ed8a5 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/dminvocation/MediaType.java @@ -0,0 +1,38 @@ +/** + * + */ +package org.gcube.data.analysis.dminvocation; + + +/** + * The Enum MediaType. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Dec 4, 2018 + */ +public enum MediaType { + + ApplicationJSON("application/json"), + ApplicationXML("application/xml"); + + private String mimeType; + + /** + * Instantiates a new media type. + * + * @param mimeType the mime type + */ + MediaType(String mimeType){ + this.mimeType = mimeType; + } + + /** + * Gets the mime type. + * + * @return the mimeType + */ + public String getMimeType() { + + return mimeType; + } +} diff --git a/src/main/java/org/gcube/data/analysis/dminvocation/model/jaxb.properties b/src/main/java/org/gcube/data/analysis/dminvocation/model/jaxb.properties new file mode 100644 index 0000000..623030d --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/dminvocation/model/jaxb.properties @@ -0,0 +1,3 @@ +#Here we injecting the JAXBContextFactory from MOXy library. + +javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory \ No newline at end of file diff --git a/src/test/java/DataMinerInvocationTest.java b/src/test/java/DataMinerInvocationTest.java index 7d3e4c3..afb05bb 100644 --- a/src/test/java/DataMinerInvocationTest.java +++ b/src/test/java/DataMinerInvocationTest.java @@ -15,6 +15,7 @@ import java.util.Map; import javax.xml.bind.JAXBException; import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager; +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.DataMinerOutputParams; @@ -61,16 +62,17 @@ public class DataMinerInvocationTest { dmInvocation.setParameters(params); System.out.println(dmInvocation); - ByteArrayOutputStream outStream = DataMinerInvocationManager.marshaling(dmInvocation); - - System.out.println(new String(outStream.toByteArray())); + ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON); + System.out.println(new String(outStreamJSON.toByteArray())); + ByteArrayOutputStream outStreamXML = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationXML); + System.out.println(new String(outStreamXML.toByteArray())); } @Test - public void unmarshallingTest() throws JAXBException, FileNotFoundException{ + public void unmarshallingXMLTest() throws JAXBException, FileNotFoundException{ FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml")); - DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile); + DataMinerInvocation dmInvocation = DataMinerInvocationManager.unmarshaling(dmInvocationXMLFile, MediaType.ApplicationXML); System.out.println(dmInvocation); } } diff --git a/src/test/resources/DataMinerInvocation.xml b/src/test/resources/DataMinerInvocation.xml index b177e59..fc061c3 100644 --- a/src/test/resources/DataMinerInvocation.xml +++ b/src/test/resources/DataMinerInvocation.xml @@ -1,4 +1,3 @@ - THE_OPERATOR_ID