generalized for marshaling/unmarshaling as XML or JSON

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/dataminer-invocation-model@174555 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Francesco Mangiacrapa 5 years ago
parent f2cbfd0290
commit 06e1259287

@ -20,6 +20,7 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
@ -15,9 +20,17 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

@ -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

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="dataminer-invocation-model">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
</wb-module>
</project-modules>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.7"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>

@ -72,6 +72,11 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.2</version>
</dependency>
<!-- LOGGER -->
<dependency>

@ -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);

@ -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;
}
}

@ -0,0 +1,3 @@
#Here we injecting the JAXBContextFactory from MOXy library.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

@ -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);
}
}

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataminer-invocation>
<operator-id>THE_OPERATOR_ID</operator-id>
<parameters>

Loading…
Cancel
Save