Skip to content

Commit

Permalink
Added dot, GraphML and TEI message body writers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhdekker committed May 6, 2015
1 parent c824726 commit 90d9cb3
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Optional<String>> path = CompletableFuture.supplyAsync(() -> processReader.lines()
Expand All @@ -49,6 +45,9 @@ public Set<Class<?>> getClasses() {
Set<Class<?>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VariantGraph> {
@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<String, Object> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<VariantGraph> {
@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<String, Object> 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);
}

}
}
Original file line number Diff line number Diff line change
@@ -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<VariantGraph> {
@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<String, Object> 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);
}
}
}

0 comments on commit 90d9cb3

Please sign in to comment.