diff --git a/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties b/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties index 617aca7..a0d96d4 100644 --- a/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties +++ b/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties @@ -1,9 +1,9 @@ -serverURL=http://localhost:8000 -workingDir=C:/Users/useradm/git/json-discoverer/fr.inria.atlanmod.json.web/WebContent/workingDir +serverURL=http://atlanmod.github.io +workingDir=/opt/apache-tomee-jaxrs-1.6.0/webapps/jsonDiscoverer/workingDir IdInjector=injector IdDiscoverer=discoverer IdMultiDiscoverer=multidiscoverer -dotExePath=C:/Program Files (x86)/Graphviz 2.28/bin/dot.exe +dotExePath=/usr/bin/dot JsonParameter=json UrlParameter=url -LimitChars=16384 \ No newline at end of file +LimitChars=16384 diff --git a/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties.server b/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties.server index c938ce4..a0d96d4 100644 --- a/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties.server +++ b/fr.inria.atlanmod.json.web/WebContent/WEB-INF/config.properties.server @@ -1,4 +1,4 @@ -serverURL=https://atlanmod.github.io +serverURL=http://atlanmod.github.io workingDir=/opt/apache-tomee-jaxrs-1.6.0/webapps/jsonDiscoverer/workingDir IdInjector=injector IdDiscoverer=discoverer diff --git a/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/AbstractJsonDiscoverer.java b/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/AbstractJsonDiscoverer.java index c2dd161..2a374f0 100644 --- a/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/AbstractJsonDiscoverer.java +++ b/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/AbstractJsonDiscoverer.java @@ -14,6 +14,7 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.Properties; @@ -24,10 +25,15 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.cxf.helpers.IOUtils; import org.eclipse.core.runtime.CoreException; +import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EcorePackage; import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; import org.emftools.emf2gv.graphdesc.GraphdescPackage; @@ -113,7 +119,52 @@ String encodeToString(File imagePath) throws IOException { } return imageString; } + + /** + * Saves the EPackage into a XMI and then read it to return it as String in Base64 + * + * @param ePackage + * @param uniqueId + * @return + * @throws ServletException + */ + String encodeToString(EPackage ePackage, String uniqueId) throws ServletException { + File uniqueWorkingDir = new File(workingDir.getAbsolutePath() + File.separator + uniqueId); + if(!uniqueWorkingDir.isDirectory()) throw new ServletException("The working dir could not be set:" + uniqueWorkingDir.getAbsolutePath()); + // Getting a temp file + File resultPath; + try { + resultPath = File.createTempFile("temp", ".xmi", uniqueWorkingDir); + } catch (IOException e1) { + throw new ServletException("Not possible to access to temp dir"); + } + + // Saving into XMI + ResourceSet rSet = new ResourceSetImpl(); + Resource res = rSet.createResource(URI.createFileURI(resultPath.getAbsolutePath())); + try { + res.getContents().add(ePackage); + res.save(null); + } catch (IOException e) { + throw new ServletException("Not possible to save the Epackage", e); + } + + // Loading XMI into String + String result; + try { + FileInputStream fis = new FileInputStream(resultPath); + String content = IOUtils.readStringFromStream(fis); + BASE64Encoder encoder = new BASE64Encoder(); + result = encoder.encode(content.getBytes()); + fis.close(); + } catch (IOException e) { + throw new ServletException("Error reading XMI to String", e); + } + + return result; + } + /** * Draws a model into a picture. To avoid file access problems, an unique id has to be * provided. A new directory using such id will be created. diff --git a/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/JsonDiscovererServlet.java b/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/JsonDiscovererServlet.java index 9ed1f18..783c89e 100644 --- a/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/JsonDiscovererServlet.java +++ b/fr.inria.atlanmod.json.web/src/fr/inria/atlanmod/json/web/JsonDiscovererServlet.java @@ -24,6 +24,10 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; + +import com.google.gson.JsonObject; import fr.inria.atlanmod.discoverer.JsonDiscoverer; import fr.inria.atlanmod.discoverer.JsonSource; @@ -53,29 +57,51 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) addResponseOptions(response); String jsonCode = request.getParameter(jsonParam); if(jsonCode == null || jsonCode.equals("")) throw new ServletException("No json data in the call"); - String resultImage = discoverMetamodelBase64(jsonCode); + + EPackage resultMetamodel = discoverMetamodel(jsonCode); + + String id = properties.getProperty(DISCOVERER_ID); + String resultImage = discoverMetamodelBase64(resultMetamodel); + String resultXMI = encodeToString(resultMetamodel, id); + + // Building the response + response.setContentType("text/x-json;charset=UTF-8"); + JsonObject jsonResponse = new JsonObject(); + jsonResponse.addProperty("image", resultImage); + jsonResponse.addProperty("xmi", resultXMI); PrintWriter out = response.getWriter(); - out.print(resultImage); + out.print(jsonResponse.toString()); } /** - * Discover a metamodel and returns the File of the picture representing - * the metamode + * Discover a metamodel from JSON * * @param jsonCode - * @return - * @throws IOException + * @return The Epackage + * @throws ServletException */ - private File discoverMetamodel(String jsonCode) throws ServletException { + private EPackage discoverMetamodel(String jsonCode) throws ServletException { // Discovering JsonDiscoverer discoverer = new JsonDiscoverer(); JsonSource source = new JsonSource("Discovered"); source.addJsonDef(jsonCode); EPackage discoveredModel = discoverer.discoverMetamodel(source); - // Drawing the discovered model + return discoveredModel; + } + + + + /** + * Converts a EPackage into a picture and saves it in disk + * + * @param jsonCode + * @return + * @throws IOException + */ + private File convertToImage(EPackage ePackage) throws ServletException { List toDraw= new ArrayList(); - toDraw.add(discoveredModel); + toDraw.add(ePackage); String id = properties.getProperty(DISCOVERER_ID); if(id == null) throw new ServletException("ID for discoverer not found in properties"); @@ -85,6 +111,8 @@ private File discoverMetamodel(String jsonCode) throws ServletException { return resultPath; } + + /** * Performs the discovery and returns a picture encoded in BASE64 with the * pciture representing the discovered metamodel @@ -93,8 +121,8 @@ private File discoverMetamodel(String jsonCode) throws ServletException { * @return * @throws IOException */ - private String discoverMetamodelBase64(String jsonCode) throws ServletException { - File resultPath = discoverMetamodel(jsonCode); + private String discoverMetamodelBase64(EPackage ePackage) throws ServletException { + File resultPath = convertToImage(ePackage); String resultImage; try { resultImage = encodeToString(resultPath);