package com.oreilly.rdf.tenuki.jaxrs.io;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

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 com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sparql.resultset.ResultSetFormat;
import com.hp.hpl.jena.sparql.resultset.SPARQLResult;

@Provider
@Produces("application/sparql-results+xml, application/sparql-results+json, application/rdf+xml, text/turtle, text/rdf+n3, text/plain, text/csv")
public class SPARQLResultWriter implements MessageBodyWriter<SPARQLResult> {
	@Override
	public long getSize(SPARQLResult t, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType) {
		return -1;
	}

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

	@Override
	public void writeTo(SPARQLResult result, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream) throws IOException,
			WebApplicationException {
		if (result.isGraph()) {
			Model model = result.getModel();
			String lang = null;
			if (mediaType
					.isCompatible(MediaType.valueOf("application/rdf+xml"))) {
				lang = "RDF/XML";
				httpHeaders.putSingle("Content-Type", "application/rdf+xml");
			}
			else if (mediaType.isCompatible(MediaType.valueOf("text/turtle"))) {
				lang = "TTL";
				httpHeaders.putSingle("Content-Type", "text/turtle");
			}
			else if (mediaType.isCompatible(MediaType.valueOf("text/rdf+n3"))) {
				lang = "N3";
				httpHeaders.putSingle("Content-Type", "text/rdf+n3");
			}
			else if (mediaType.isCompatible(MediaType.valueOf("text/plain"))) {
				lang = "N-TRIPLE";
				httpHeaders.putSingle("Content-Type", "text/plain");
			} else {
				lang = "RDF/XML";
				httpHeaders.putSingle("Content-Type", "application/rdf+xml");
			}
			model.write(entityStream, lang);
		}
		if (result.isResultSet()) {
			ResultSet rs = result.getResultSet();
			ResultSetFormat fmt = null;
			if (mediaType.isCompatible(MediaType
					.valueOf("application/sparql-results+xml"))) {
				fmt = ResultSetFormat.syntaxXML;
			}
			if (mediaType.isCompatible(MediaType.valueOf("text/plain"))) {
				fmt = ResultSetFormat.syntaxText;
			}
			if (mediaType.isCompatible(MediaType.valueOf("text/csv"))) {
				fmt = ResultSetFormat.syntaxCSV;
			}
			if (mediaType.isCompatible(MediaType.valueOf("application/sparql-results+json"))) {
				fmt = ResultSetFormat.syntaxJSON;
			}
			ResultSetFormatter.output(entityStream, rs, fmt);
		}
	}

}

@@ -2545,21 +2545,98 @@
 taxXML;%0A
+%09%09%09%09httpHeaders.putSingle(%22Content-Type%22, %22application/sparql-results+xml%22);%0A
 %09%09%09%7D%0A
-
 %09%09%09if (m
@@ -2731,16 +2731,73 @@
 axText;%0A
+%09%09%09%09httpHeaders.putSingle(%22Content-Type%22, %22text/plain%22);%0A
 %09%09%09%7D%0A%09%09%09
@@ -2894,16 +2894,71 @@
 taxCSV;%0A
+%09%09%09%09httpHeaders.putSingle(%22Content-Type%22, %22text/csv%22);%0A
 %09%09%09%7D%0A%09%09%09
@@ -3033,32 +3033,32 @@
 ults+json%22))) %7B%0A
-
 %09%09%09%09fmt = Result
@@ -3079,16 +3079,94 @@
 axJSON;%0A
+%09%09%09%09httpHeaders.putSingle(%22Content-Type%22, %22application/sparql-results+json%22);%0A
 %09%09%09%7D%0A%09%09%09

Set content types correctly on sparql returns