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

import java.util.Iterator;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.oreilly.rdf.tenuki.Changeset;
import com.oreilly.rdf.tenuki.ChangesetHandler;

@Path("/graphs/")
public class GraphResource {

	private Dataset dataset;

	public void setDataset(Dataset dataset) {
		this.dataset = dataset;
	}

	@Produces("text/uri-list")
	@GET
	public String getGraphsAsURIList() {
		StringBuilder urilist = new StringBuilder();
		for (Iterator<String> iterator = dataset.listNames(); iterator
				.hasNext();) {
			String name = iterator.next();
			urilist.append(name);
			urilist.append("\r\n");
		}
		return urilist.toString();
	}

	@Path("{graphUri}")
	@Produces("application/rdf+xml")
	@GET
	public Model getGraph(@PathParam("graphUri") String graphUri) {
		return dataset.getNamedModel(graphUri);
	}

	@Path("{graphUri}")
	@Consumes("application/rdf+xml")
	@PUT
	public Response setGraph(@PathParam("graphUri") String graphUri, Model model) {
		Model dsModel = dataset.getNamedModel(graphUri);
		dsModel.add(model);
		return Response.noContent().build();
	}

	@Path("{graphUri}")
	@Consumes("application/vnd.talis.changeset+xml")
	@PATCH
	public Response applyChangesetToGraph(
			@PathParam("graphUri") String graphUri, Changeset changeset) {
		if (changeset.getSubjectOfChange().toString().equals(graphUri)) {
			Model dsModel = dataset.getNamedModel(graphUri);
			ChangesetHandler handler = new ChangesetHandler(dsModel);
			handler.applyChangeset(changeset);
			return Response.ok(dsModel,
					MediaType.valueOf("application/rdf+xml")).build();
		} else {
			return Response.serverError().build();
		}
	}

}

@@ -962,24 +962,186 @@
 String();%0A%09%7D
+%0A%09%0A%09@Produces(%22application/rdf+xml%22)%0A%09@GET%0A%09public Model getGraphByQueryParam(@QueryParam(%22graph%22) String graphUri) %7B%0A%09%09return dataset.getNamedModel(graphUri);%0A%09%7D
 %0A%0A%09@Path(%22%7Bg
@@ -1174,32 +1174,70 @@
 lication/rdf+xml
+, text/turtle, text/rdf+n3, text/plain
 %22)%0A%09@GET%0A%09public
@@ -1388,24 +1388,62 @@
 tion/rdf+xml
+, text/turtle, text/rdf+n3, text/plain
 %22)%0A%09@PUT%0A%09pu

Add support for other mimetypes