Skip to content

Commit

Permalink
JsonDiscovererServlet returns now the XMI. This fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jlcanovas committed Sep 11, 2014
1 parent 0d6f1f1 commit 0294599
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
LimitChars=16384
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<EObject> toDraw= new ArrayList<EObject>();
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");
Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 0294599

Please sign in to comment.