Search in sources :

Example 1 with Exporter

use of com.google.refine.exporters.Exporter in project OpenRefine by OpenRefine.

the class ExportRowsCommand method doPost.

@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);
        Properties params = getRequestParameters(request);
        String format = params.getProperty("format");
        Exporter exporter = ExporterRegistry.getExporter(format);
        if (exporter == null) {
            exporter = new CsvExporter('\t');
        }
        String contentType = params.getProperty("contentType");
        if (contentType == null) {
            contentType = exporter.getContentType();
        }
        response.setHeader("Content-Type", contentType);
        if (exporter instanceof WriterExporter) {
            String encoding = params.getProperty("encoding");
            response.setCharacterEncoding(encoding != null ? encoding : "UTF-8");
            Writer writer = encoding == null ? response.getWriter() : new OutputStreamWriter(response.getOutputStream(), encoding);
            ((WriterExporter) exporter).export(project, params, engine, writer);
            writer.close();
        } else if (exporter instanceof StreamExporter) {
            response.setCharacterEncoding("UTF-8");
            OutputStream stream = response.getOutputStream();
            ((StreamExporter) exporter).export(project, params, engine, stream);
            stream.close();
        //          } else if (exporter instanceof UrlExporter) {
        //              ((UrlExporter) exporter).export(project, options, engine);
        } else {
            // TODO: Should this use ServletException instead of respondException?
            respondException(response, new RuntimeException("Unknown exporter type"));
        }
    } catch (Exception e) {
        // Use generic error handling rather than our JSON handling
        throw new ServletException(e);
    } finally {
        ProjectManager.singleton.setBusy(false);
    }
}
Also used : WriterExporter(com.google.refine.exporters.WriterExporter) StreamExporter(com.google.refine.exporters.StreamExporter) OutputStream(java.io.OutputStream) Properties(java.util.Properties) WriterExporter(com.google.refine.exporters.WriterExporter) Exporter(com.google.refine.exporters.Exporter) StreamExporter(com.google.refine.exporters.StreamExporter) CsvExporter(com.google.refine.exporters.CsvExporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Project(com.google.refine.model.Project) CsvExporter(com.google.refine.exporters.CsvExporter) OutputStreamWriter(java.io.OutputStreamWriter) Engine(com.google.refine.browsing.Engine) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Engine (com.google.refine.browsing.Engine)1 CsvExporter (com.google.refine.exporters.CsvExporter)1 Exporter (com.google.refine.exporters.Exporter)1 StreamExporter (com.google.refine.exporters.StreamExporter)1 WriterExporter (com.google.refine.exporters.WriterExporter)1 Project (com.google.refine.model.Project)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1