package org.geneontology.minerva.server.handler;

import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.TupleQueryResultHandlerException;
import org.openrdf.query.resultio.QueryResultIO;
import org.openrdf.query.resultio.TupleQueryResultFormat;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
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.lang.annotation.Annotation;
import java.lang.reflect.Type;

@Provider
@Produces({
        "application/sparql-results+json",
        MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_XML,
        MediaType.TEXT_XML,
        "text/csv",
        "text/tab-separated-values"
})
public class SPARQLResultsMessageBodyWriter implements MessageBodyWriter<TupleQueryResult> {

    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(TupleQueryResult result, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(TupleQueryResult result, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        TupleQueryResultFormat format = QueryResultIO.getWriterFormatForMIMEType(mediaType.toString(), TupleQueryResultFormat.JSON);
        try {
            QueryResultIO.write(result, format, entityStream);
            entityStream.flush();
            result.close();
        } catch (TupleQueryResultHandlerException | QueryEvaluationException e) {
            throw new WebApplicationException(e);
        }
    }

}

@@ -757,16 +757,58 @@
 N_JSON,%0A
+        %22application/sparql-results+xml%22,%0A
         
@@ -917,16 +917,46 @@
 -values%22
+,%0A        MediaType.TEXT_PLAIN
 %0A%7D)%0Apubl

Add MIME types.