
package org.fcrepo.responses;

import static org.apache.jena.riot.WebContent.contentTypeToLang;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.StreamingOutput;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import org.slf4j.Logger;

public class GraphStoreStreamingOutput implements StreamingOutput {

    private static final Logger LOGGER =
            getLogger(GraphStoreStreamingOutput.class);

    private final Dataset dataset;

    private final String format;


    public GraphStoreStreamingOutput(final GraphStore graphStore,
                                     final MediaType mediaType) {
        this(graphStore.toDataset(), mediaType);
    }

    public GraphStoreStreamingOutput(final Dataset dataset,
            final MediaType mediaType) {
        this.dataset = dataset;
        format =
                contentTypeToLang(mediaType.toString()).getName().toUpperCase();
    }

    @Override
    public void write(final OutputStream out) throws IOException,
            WebApplicationException {
        LOGGER.debug("Serializing graph model as {}", format);
        final Iterator<String> iterator = dataset.listNames();

        while (iterator.hasNext()) {
            final Model model = dataset.getNamedModel(iterator.next());
            model.write(out, format);
        }

        dataset.getDefaultModel().write(out, format);

    }

}

@@ -1314,21 +1314,16 @@
 g graph 
-model
  as %7B%7D%22,
@@ -1429,24 +1429,139 @@
 asNext()) %7B%0A
+            final String modelName = iterator.next();%0A            LOGGER.debug(%22Serializing model %7B%7D%22, modelName);%0A
             
@@ -1602,31 +1602,25 @@
 edModel(
-iterator.next()
+modelName
 );%0A     
@@ -1659,24 +1659,75 @@
 %0A        %7D%0A%0A
+        LOGGER.debug(%22Serializing default model%22);%0A
         data

add better logging to the graph streaming output