package org.taverna.t2flowrender;

import static java.io.File.createTempFile;
import static java.nio.file.Files.copy;
import static java.nio.file.Files.readAllBytes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("webapp")
@Path("/")
public class T2FlowRenderer {
	public static final String T2FLOW = "application/vnd.taverna.t2flow+xml";
	public static final String SVG = "image/svg+xml";

	@Autowired
	private Invoker invoker;

	@GET
	@Produces("text/plain")
	public String describe() {
		return "This service renders a t2flow as SVG.\nPOST your t2flow as "
				+ T2FLOW + " and prepare to receive it as " + SVG
				+ " shortly after.";
	}

	@POST
	@Consumes(T2FLOW)
	@Produces(SVG)
	public byte[] renderT2flow(InputStream t2flow) {
		File src = null, dst = null;
		try {
			src = createTempFile("src", ".t2flow");
			dst = createTempFile("dst", ".svg");
			copy(t2flow, src.toPath());
			invoker.run(src, dst);
			return readAllBytes(dst.toPath());
		} catch (IOException | InterruptedException e) {
			throw new WebApplicationException(e, 500);
		} finally {
			if (src != null)
				src.delete();
			if (dst != null)
				dst.delete();
		}
	}
}

@@ -158,16 +158,81 @@
 llBytes;
+%0Aimport static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
 %0A%0Aimport
@@ -362,16 +362,57 @@
 e.Files;
+%0Aimport java.nio.file.StandardCopyOption;
 %0A%0Aimport
@@ -1374,24 +1374,42 @@
 src.toPath()
+, REPLACE_EXISTING
 );%0A%09%09%09invoke

Fix things.