From 90d9cb3dec3b63edb4bdb210dad317d52c4ebd4b Mon Sep 17 00:00:00 2001 From: Ronald Haentjens Dekker Date: Wed, 6 May 2015 17:34:13 +0200 Subject: [PATCH] Added dot, GraphML and TEI message body writers. --- .../collatex/http/CollateApplication.java | 9 ++- .../io/VariantGraphDotMessageBodyWriter.java | 44 ++++++++++++++ .../VariantGraphGraphMLMessageBodyWriter.java | 57 +++++++++++++++++++ .../io/VariantGraphTEIMessageBodyWriter.java | 56 ++++++++++++++++++ 4 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphDotMessageBodyWriter.java create mode 100644 collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphGraphMLMessageBodyWriter.java create mode 100644 collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphTEIMessageBodyWriter.java diff --git a/collatex-servlet/src/main/java/eu/interedition/collatex/http/CollateApplication.java b/collatex-servlet/src/main/java/eu/interedition/collatex/http/CollateApplication.java index b8ae7c322..8640ca18c 100644 --- a/collatex-servlet/src/main/java/eu/interedition/collatex/http/CollateApplication.java +++ b/collatex-servlet/src/main/java/eu/interedition/collatex/http/CollateApplication.java @@ -1,9 +1,6 @@ package eu.interedition.collatex.http; -import eu.interedition.collatex.io.IOExceptionMapper; -import eu.interedition.collatex.io.SimpleCollationJSONMessageBodyReader; -import eu.interedition.collatex.io.VariantGraphJSONMessageBodyWriter; -import eu.interedition.collatex.io.VariantGraphSVGMessageBodyWriter; +import eu.interedition.collatex.io.*; import javax.ws.rs.core.Application; import java.io.BufferedReader; @@ -25,7 +22,6 @@ public class CollateApplication extends Application { private static String detectDotPath() { for (String detectionCommand : new String[] { "which dot", "where dot.exe" }) { try { - final Process process = Runtime.getRuntime().exec(detectionCommand); try (BufferedReader processReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset()))) { final CompletableFuture> path = CompletableFuture.supplyAsync(() -> processReader.lines() @@ -49,6 +45,9 @@ public Set> getClasses() { Set> s = new HashSet<>(); s.add(SimpleCollationJSONMessageBodyReader.class); s.add(VariantGraphJSONMessageBodyWriter.class); + s.add(VariantGraphTEIMessageBodyWriter.class); + s.add(VariantGraphGraphMLMessageBodyWriter.class); + s.add(VariantGraphDotMessageBodyWriter.class); s.add(IOExceptionMapper.class); return s; } diff --git a/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphDotMessageBodyWriter.java b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphDotMessageBodyWriter.java new file mode 100644 index 000000000..1dc864b4e --- /dev/null +++ b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphDotMessageBodyWriter.java @@ -0,0 +1,44 @@ +package eu.interedition.collatex.io; + +import eu.interedition.collatex.VariantGraph; +import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +/** + * Created by ronald on 5/6/15. + */ +@Provider +@Produces("text/plain") +public class VariantGraphDotMessageBodyWriter implements MessageBodyWriter { + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return VariantGraph.class.isAssignableFrom(type); + } + + @Override + public long getSize(VariantGraph variantGraph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(VariantGraph graph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + httpHeaders.add(HttpHeaders.CONTENT_TYPE, "text/plain"); + httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "utf-8"); + try (final Writer out = new OutputStreamWriter(entityStream)) { + new SimpleVariantGraphSerializer(graph).toDot(out); + } + } +} diff --git a/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphGraphMLMessageBodyWriter.java b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphGraphMLMessageBodyWriter.java new file mode 100644 index 000000000..8536b0594 --- /dev/null +++ b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphGraphMLMessageBodyWriter.java @@ -0,0 +1,57 @@ +package eu.interedition.collatex.io; + +import eu.interedition.collatex.VariantGraph; +import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +/** + * Created by ronald on 5/6/15. + */ +@Provider +@Produces("application/graphml+xml") +public class VariantGraphGraphMLMessageBodyWriter implements MessageBodyWriter { + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return VariantGraph.class.isAssignableFrom(type); + } + + @Override + public long getSize(VariantGraph variantGraph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(VariantGraph graph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + XMLStreamWriter xml = null; + try { + httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/graphml+xml"); + try (OutputStream responseStream = entityStream) { + xml = XMLOutputFactory.newInstance().createXMLStreamWriter(responseStream); + xml.writeStartDocument(); + new SimpleVariantGraphSerializer(graph).toGraphML(xml); + xml.writeEndDocument(); + } finally { + if (xml != null) { + xml.close(); + } + } + } catch (XMLStreamException e) { + throw new WebApplicationException(e); + } + + } +} diff --git a/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphTEIMessageBodyWriter.java b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphTEIMessageBodyWriter.java new file mode 100644 index 000000000..d0dcb506d --- /dev/null +++ b/collatex-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphTEIMessageBodyWriter.java @@ -0,0 +1,56 @@ +package eu.interedition.collatex.io; + +import eu.interedition.collatex.VariantGraph; +import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +/** + * Created by ronald on 5/6/15. + */ +@Provider +@Produces("application/tei+xml") +public class VariantGraphTEIMessageBodyWriter implements MessageBodyWriter { + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return VariantGraph.class.isAssignableFrom(type); + } + + @Override + public long getSize(VariantGraph variantGraph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(VariantGraph graph, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + XMLStreamWriter xml = null; + try { + httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/tei+xml"); + try (OutputStream responseStream = entityStream) { + xml = XMLOutputFactory.newInstance().createXMLStreamWriter(responseStream); + xml.writeStartDocument(); + new SimpleVariantGraphSerializer(graph).toTEI(xml); + xml.writeEndDocument(); + } finally { + if (xml != null) { + xml.close(); + } + } + } catch (XMLStreamException e) { + throw new WebApplicationException(e); + } + } +}