use of com.google.refine.exporters.WriterExporter 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);
String preview = params.getProperty("preview");
if (!"true".equals(preview)) {
String path = request.getPathInfo();
String filename = path.substring(path.lastIndexOf('/') + 1);
PercentEscaper escaper = new PercentEscaper("", false);
filename = escaper.escape(filename);
response.setHeader("Content-Disposition", "attachment; filename=" + filename + "; filename*=utf-8' '" + filename);
}
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
logger.info("error:{}", e.getMessage());
if (e instanceof SqlExporterException) {
response.sendError(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
throw new ServletException(e);
} finally {
ProjectManager.singleton.setBusy(false);
}
}
Aggregations