Skip to content
Snippets Groups Projects
Commit b6c30bd5 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch 'formats-conversion-rest-api' of...

Merge branch 'formats-conversion-rest-api' of https://git-r3lab.uni.lu/minerva/core into formats-conversion-rest-api
parents 76457217 3b904984
No related branches found
No related tags found
2 merge requests!630WIP: Resolve "The privileges of a new user are not saved in some cases",!418Formats conversion rest api
Pipeline #6474 passed
......@@ -31,11 +31,14 @@ import lcsb.mapviewer.annotation.services.MiriamConnector;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.PubmedSearchException;
import lcsb.mapviewer.annotation.services.annotators.ElementAnnotator;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.IConverter;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.converter.model.sbgnml.SbgnmlXmlConverter;
import lcsb.mapviewer.converter.model.sbml.SbmlParser;
......@@ -318,6 +321,19 @@ public abstract class BaseRestImpl {
public void setProjectService(IProjectService projectService) {
this.projectService = projectService;
}
private String removeWhiteSpaces(String str) {
return str.replace(' ', '_');
}
protected IConverter getModelParserByName(String name) throws QueryException {
for (IConverter converter : getModelConverters()) {
if (removeWhiteSpaces(converter.getCommonName()).equals(name)) {
return converter;
}
}
throw new QueryException("Unknown parser name: " + name);
}
protected IConverter getModelParser(String handlerClass) throws QueryException {
for (IConverter converter : getModelConverters()) {
......@@ -335,6 +351,7 @@ public abstract class BaseRestImpl {
result.add(new SbmlParser());
return result;
}
protected String getFirstValue(List<Object> list) {
if (list == null) {
......
package lcsb.mapviewer.api.convert;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
......@@ -12,26 +10,21 @@ import org.sbml.jsbml.SBMLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.commands.CommandExecutionException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException;
import lcsb.mapviewer.services.SecurityException;
@RestController
......@@ -43,33 +36,41 @@ public class ConvertController extends BaseController {
private ConvertRestImpl convertController;
@RequestMapping(value = "/convert/{fromFormat}:{toFormat}",
@RequestMapping(value = "/convert/{fromFormat}:{toFormat}",
produces = {MediaType.TEXT_PLAIN_VALUE},
method = { RequestMethod.POST })
public String convertInput(//
@PathVariable(value = "fromFormat") String fromFormat, //
@PathVariable(value = "toFormat") String toFormat, //
@RequestBody String body, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token //
) throws SecurityException, IOException, QueryException, SBMLException,
InvalidInputDataExecption, InconsistentModelException, ConverterException, XMLStreamException,
DrawingException {
)
throws SecurityException, IOException, QueryException, SBMLException,
InvalidInputDataExecption, InconsistentModelException, ConverterException,
XMLStreamException, DrawingException
{
return convertController.convert(token, fromFormat, toFormat, body);
}
/*
@RequestMapping(value = "/convert/{fromFormat}:{toFormat}:img",
method = { RequestMethod.POST }, produces = {
MediaType.APPLICATION_OCTET_STREAM_VALUE })
public String convertInputToImage(//
method = { RequestMethod.POST })
public @ResponseBody ResponseEntity<byte[]> convertInputToImage(//
@PathVariable(value = "fromFormat") String fromFormat, //
@PathVariable(value = "toFormat") String toFormat, //
@RequestBody String body, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token //
) throws SecurityException, IOException, QueryException, SBMLException,
InvalidInputDataExecption, InconsistentModelException, ConverterException, XMLStreamException,
DrawingException {
return convertController.converToImage(token, fromFormat, toFormat, body);
)
throws SecurityException, IOException, QueryException, SBMLException,
InvalidInputDataExecption, InconsistentModelException, ConverterException,
XMLStreamException, DrawingException
{
ByteArrayOutputStream os = convertController.converToImage(token, fromFormat, toFormat, body);
return ResponseEntity.ok().contentLength(os.size())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=model" + toFormat )
.body(os.toByteArray());
}
*/
}
\ No newline at end of file
......@@ -2,10 +2,10 @@ package lcsb.mapviewer.api.convert;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import javax.xml.stream.XMLStreamException;
......@@ -15,21 +15,15 @@ import org.sbml.jsbml.SBMLException;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.IConverter;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.converter.graphics.PdfImageGenerator;
import lcsb.mapviewer.converter.graphics.PngImageGenerator;
import lcsb.mapviewer.converter.graphics.SvgImageGenerator;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.converter.model.sbgnml.SbgnmlXmlConverter;
import lcsb.mapviewer.converter.model.sbgnml.SbgnmlXmlParser;
import lcsb.mapviewer.converter.model.sbml.SbmlParser;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.services.SecurityException;
......@@ -42,112 +36,59 @@ public class ConvertRestImpl extends BaseRestImpl {
public String convert(String token, String fromFormat, String toFormat, String input)
throws SecurityException, InvalidInputDataExecption,
SBMLException, InconsistentModelException, IOException, ConverterException,
XMLStreamException, DrawingException, QueryException {
throws SecurityException, InvalidInputDataExecption,
SBMLException, InconsistentModelException, IOException, ConverterException,
XMLStreamException, DrawingException, QueryException
{
ConverterParams params = createConvertParams(input);
Model model = getModelParser(fromFormat).createModel(params);
Model model = getModelParserByNameOrClass(fromFormat).createModel(params);
IConverter exporter = getModelParser(toFormat);
IConverter exporter = getModelParserByNameOrClass(toFormat);
return IOUtils.toString(exporter.exportModelToInputStream(model));
}
private String exportModel(Model model, String format)
throws InvalidInputDataExecption, InconsistentModelException,
IOException, ConverterException, SBMLException, XMLStreamException,
DrawingException {
switch (format) {
case "celldesigner":
return exportCellDesigner(model);
case "sbml":
return exportSbml(model);
case "sbgn":
return exportSbgn(model);
case "pdf":
return exportPdf(model);
case "png":
return exportPng(model);
case "svg":
return exportSvg(model);
default:
return null;
}
}
private Model loadCellDesigner(String input) throws InvalidInputDataExecption {
ConverterParams params = createConvertParams(input);
CellDesignerXmlParser parser = new CellDesignerXmlParser();
return parser.createModel(params);
}
private String exportCellDesigner(Model model) throws InconsistentModelException {
CellDesignerXmlParser parser = new CellDesignerXmlParser();
return parser.toXml(model);
}
private Model loadSbml(String input) throws InvalidInputDataExecption {
ConverterParams params = createConvertParams(input);
SbmlParser parser = new SbmlParser();
return parser.createModel(params);
}
private String exportSbml(Model model) throws InconsistentModelException, IOException, ConverterException {
SbmlParser parser = new SbmlParser();
return IOUtils.toString(parser.exportModelToInputStream(model));
}
private Model loadSbgn(String input) throws InvalidInputDataExecption {
ConverterParams params = createConvertParams(input);
SbgnmlXmlParser parser = new SbgnmlXmlParser();
return parser.createModel(params);
}
private String exportSbgn(Model model) throws InconsistentModelException, SBMLException, XMLStreamException,
IOException, ConverterException {
SbgnmlXmlConverter converter = new SbgnmlXmlConverter();
return IOUtils.toString(converter.exportModelToInputStream(model));
}
private String exportPdf(Model model) throws IOException, DrawingException {
AbstractImageGenerator generator = new PdfImageGenerator(createImageParams(model));
// generator.saveToFile("test1.pdf");
public ByteArrayOutputStream converToImage(String token, String fromFormat, String toFormat, String input)
throws SecurityException, InvalidInputDataExecption,
SBMLException, InconsistentModelException, IOException,
ConverterException,XMLStreamException, DrawingException,
QueryException
{
Model model = getModelParserByNameOrClass(fromFormat).createModel(createConvertParams(input));
AbstractImageGenerator generator = getImageGenerator(toFormat, createImageParams(model));
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return new String(os.toByteArray());
return os;
}
private String exportPng(Model model) throws IOException, DrawingException {
AbstractImageGenerator generator = new PngImageGenerator(createImageParams(model));
// generator.saveToFile("test1.png");
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return new String(os.toByteArray());
private IConverter getModelParserByNameOrClass(String id) throws QueryException {
try {
return getModelParserByName(id);
} catch (QueryException e) {
return getModelParser(id);
}
}
private String exportSvg(Model model) throws IOException, DrawingException {
AbstractImageGenerator generator = new SvgImageGenerator(createImageParams(model));
// generator.saveToFile("test1.svg");
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return new String(os.toByteArray());
}
private AbstractImageGenerator getImageGenerator(String extension, AbstractImageGenerator.Params params) throws QueryException{
for (Pair<String, Class<? extends AbstractImageGenerator>> element : new ImageGenerators().getAvailableImageGenerators()) {
try {
Constructor<?> ctor = element.getRight().getConstructor(AbstractImageGenerator.Params.class);
AbstractImageGenerator generator = (AbstractImageGenerator) ctor.newInstance(params);
if (extension.equals(generator.getFileExtension())) {
return generator;
}
} catch (NoSuchMethodException | java.lang.SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.error("Creation of image generator class for '" + element.getLeft() + "' failed.");
throw new QueryException("Issue with obtaining image generator for extension " + extension + ".");
}
}
throw new QueryException("Image generator for extension " + extension + " not available.");
}
private AbstractImageGenerator.Params createImageParams(Model model){
return new AbstractImageGenerator.Params().//
......
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.api.convert;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import org.apache.commons.io.FileUtils;
......@@ -13,12 +14,8 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.IConverter;
import lcsb.mapviewer.converter.model.sbml.SbmlParser;
import lcsb.mapviewer.model.map.model.Model;
public class ConvertRestImplTest {
public class ConvertRestImplTest extends RestTestFunctions {
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(ConvertRestImplTest.class);
......@@ -42,18 +39,45 @@ public class ConvertRestImplTest {
@Test
public void testCelDesigner2Sbml() throws Exception {
try {
IConverter converter = new SbmlParser();
Model model = converter.createModel(new ConverterParams().filename("testFiles/convert/sample-sbml.xml"));
model.setName(null);
converter.exportModelToFile(model, "tmp.xml");
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
String result = convertRestImpl.convert(token, "lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser",
"lcsb.mapviewer.converter.model.sbml.SbmlParser", content);
System.out.print(result);
assertTrue(result.length() > 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCelDesigner2Sbml_2() throws Exception {
try {
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
String result = convertRestImpl.convert(token, "CellDesigner_SBML",
"SBGN-ML", content);
System.out.print(result);
assertTrue(result.length() > 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCelDesigner2Svg() throws Exception {
try {
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
ByteArrayOutputStream os = convertRestImpl.converToImage(token, "lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser",
"svg", content);
System.out.print(os.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment