package com.google.refine.commands.project;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.refine.ProjectManager;
import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command;
import com.google.refine.exporters.CsvExporter;
import com.google.refine.exporters.Exporter;
import com.google.refine.exporters.ExporterRegistry;
import com.google.refine.exporters.StreamExporter;
import com.google.refine.exporters.WriterExporter;
import com.google.refine.model.Project;

public class ExportRowsCommand extends Command {

    @SuppressWarnings("unchecked")
	static public Properties getRequestParameters(HttpServletRequest request) {
        Properties options = new Properties();
        
        Enumeration<String> en = request.getParameterNames();
        while (en.hasMoreElements()) {
        	String name = en.nextElement();
        	options.put(name, request.getParameter(name));
        }
    	return options;
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ProjectManager.singleton.setBusy(true);
        try {
            Project project = getProject(request);
            Engine engine = getEngine(request, project);
            String format = request.getParameter("format");
            Properties options = getRequestParameters(request);

            Exporter exporter = ExporterRegistry.getExporter(format);
            if (exporter == null) {
                exporter = new CsvExporter('\t');
            }

            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", exporter.getContentType());

            if (exporter instanceof WriterExporter) {
                PrintWriter writer = response.getWriter();
                ((WriterExporter) exporter).export(project, options, engine, writer);
            } else if (exporter instanceof StreamExporter) {
                ((StreamExporter) exporter).export(project, options, engine, response.getOutputStream());
            } else if (exporter instanceof StreamExporter) {
                ((StreamExporter) exporter).export(project, options, engine, response.getOutputStream());
            } else {
                respondException(response, new RuntimeException("Unknown exporter type"));
            }
        } catch (Exception e) {
            respondException(response, e);
        } finally {
            ProjectManager.singleton.setBusy(false);
        }
    }
}

@@ -58,32 +58,61 @@
 io.IOException;%0A
+import java.io.OutputStream;%0A
 import java.io.P
@@ -2125,16 +2125,48 @@
 riter);%0A
+                writer.flush();%0A
         
@@ -2210,32 +2210,98 @@
 reamExporter) %7B%0A
+                OutputStream stream = response.getOutputStream();%0A
                 
@@ -2365,37 +2365,51 @@
 ne, 
-response.getOutputStream
+stream);%0A                stream.flush
 ()
-)
 ;%0A
+//
     
@@ -2439,38 +2439,35 @@
 rter instanceof 
-Stream
+Url
 Exporter) %7B%0A    
@@ -2454,32 +2454,34 @@
  UrlExporter) %7B%0A
+//
                 
@@ -2474,38 +2474,35 @@
               ((
-Stream
+Url
 Exporter) export
@@ -2540,36 +2540,8 @@
 gine
-, response.getOutputStream()
 );%0A 

Make sure the stream/writer is flushed in case the exporter forgets to do it