2011-08-02 05:34:47 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright 2011, Google Inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
* Neither the name of Google Inc. nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.refine.importing;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2011-09-15 23:40:40 +02:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.Writer;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
2018-10-03 22:33:37 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
2018-10-07 15:59:45 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
2018-11-05 17:35:51 +01:00
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.RefineServlet;
|
|
|
|
import com.google.refine.commands.HttpUtilities;
|
|
|
|
import com.google.refine.importing.ImportingManager.Format;
|
|
|
|
import com.google.refine.util.JSONUtilities;
|
|
|
|
import com.google.refine.util.ParsingUtilities;
|
|
|
|
|
|
|
|
public class DefaultImportingController implements ImportingController {
|
|
|
|
|
|
|
|
protected RefineServlet servlet;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init(RefineServlet servlet) {
|
|
|
|
this.servlet = servlet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
2012-08-05 17:36:23 +02:00
|
|
|
throw new ServletException("GET verb not implemented");
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The uploaded file is in the POST body as a "file part". If
|
|
|
|
* we call request.getParameter() then the POST body will get
|
|
|
|
* read and we won't have a chance to parse the body ourselves.
|
|
|
|
* This is why we have to parse the URL for parameters ourselves.
|
|
|
|
*/
|
|
|
|
Properties parameters = ParsingUtilities.parseUrlParameters(request);
|
|
|
|
String subCommand = parameters.getProperty("subCommand");
|
|
|
|
if ("load-raw-data".equals(subCommand)) {
|
|
|
|
doLoadRawData(request, response, parameters);
|
|
|
|
} else if ("update-file-selection".equals(subCommand)) {
|
|
|
|
doUpdateFileSelection(request, response, parameters);
|
|
|
|
} else if ("initialize-parser-ui".equals(subCommand)) {
|
|
|
|
doInitializeParserUI(request, response, parameters);
|
|
|
|
} else if ("update-format-and-options".equals(subCommand)) {
|
|
|
|
doUpdateFormatAndOptions(request, response, parameters);
|
|
|
|
} else if ("create-project".equals(subCommand)) {
|
|
|
|
doCreateProject(request, response, parameters);
|
|
|
|
} else {
|
|
|
|
HttpUtilities.respond(response, "error", "No such sub command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doLoadRawData(HttpServletRequest request, HttpServletResponse response, Properties parameters)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
long jobID = Long.parseLong(parameters.getProperty("jobID"));
|
|
|
|
ImportingJob job = ImportingManager.getJob(jobID);
|
|
|
|
if (job == null) {
|
|
|
|
HttpUtilities.respond(response, "error", "No such import job");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-16 01:52:05 +02:00
|
|
|
|
|
|
|
job.updating = true;
|
2018-11-19 19:56:27 +01:00
|
|
|
ObjectNode config = job.getOrCreateDefaultConfig();
|
|
|
|
if (!("new".equals(JSONUtilities.getString(config, "state", null)))) {
|
|
|
|
HttpUtilities.respond(response, "error", "Job already started; cannot load more data");
|
|
|
|
return;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2018-11-19 19:56:27 +01:00
|
|
|
|
|
|
|
ImportingUtilities.loadDataAndPrepareJob(
|
|
|
|
request, response, parameters, job, config);
|
|
|
|
job.touch();
|
|
|
|
job.updating = false;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void doUpdateFileSelection(HttpServletRequest request, HttpServletResponse response, Properties parameters)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
long jobID = Long.parseLong(parameters.getProperty("jobID"));
|
|
|
|
ImportingJob job = ImportingManager.getJob(jobID);
|
|
|
|
if (job == null) {
|
|
|
|
HttpUtilities.respond(response, "error", "No such import job");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-16 01:52:05 +02:00
|
|
|
|
|
|
|
job.updating = true;
|
2018-11-19 19:56:27 +01:00
|
|
|
ObjectNode config = job.getOrCreateDefaultConfig();
|
|
|
|
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
|
|
|
|
HttpUtilities.respond(response, "error", "Job not ready");
|
|
|
|
return;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2018-11-19 19:56:27 +01:00
|
|
|
|
|
|
|
ArrayNode fileSelectionArray = ParsingUtilities.evaluateJsonStringToArrayNode(
|
|
|
|
request.getParameter("fileSelection"));
|
|
|
|
|
|
|
|
ImportingUtilities.updateJobWithNewFileSelection(job, fileSelectionArray);
|
|
|
|
|
|
|
|
replyWithJobData(request, response, job);
|
|
|
|
job.touch();
|
|
|
|
job.updating = false;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void doUpdateFormatAndOptions(HttpServletRequest request, HttpServletResponse response, Properties parameters)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
long jobID = Long.parseLong(parameters.getProperty("jobID"));
|
|
|
|
ImportingJob job = ImportingManager.getJob(jobID);
|
|
|
|
if (job == null) {
|
|
|
|
HttpUtilities.respond(response, "error", "No such import job");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-16 01:52:05 +02:00
|
|
|
|
|
|
|
job.updating = true;
|
2018-11-19 19:56:27 +01:00
|
|
|
ObjectNode config = job.getOrCreateDefaultConfig();
|
|
|
|
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
|
|
|
|
HttpUtilities.respond(response, "error", "Job not ready");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String format = request.getParameter("format");
|
|
|
|
ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(
|
|
|
|
request.getParameter("options"));
|
|
|
|
|
|
|
|
List<Exception> exceptions = new LinkedList<Exception>();
|
|
|
|
|
|
|
|
ImportingUtilities.previewParse(job, format, optionObj, exceptions);
|
|
|
|
|
|
|
|
Writer w = response.getWriter();
|
|
|
|
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
2011-08-02 05:34:47 +02:00
|
|
|
try {
|
2018-11-19 19:56:27 +01:00
|
|
|
writer.writeStartObject();
|
|
|
|
if (exceptions.size() == 0) {
|
|
|
|
job.project.update(); // update all internal models, indexes, caches, etc.
|
|
|
|
|
|
|
|
writer.writeStringField("status", "ok");
|
|
|
|
} else {
|
|
|
|
writer.writeStringField("status", "error");
|
|
|
|
writer.writeArrayFieldStart("errors");
|
|
|
|
writeErrors(writer, exceptions);
|
|
|
|
writer.writeEndArray();
|
2011-09-15 23:40:40 +02:00
|
|
|
}
|
2018-11-19 19:56:27 +01:00
|
|
|
writer.writeEndObject();
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException e) {
|
2011-08-02 05:34:47 +02:00
|
|
|
throw new ServletException(e);
|
2011-09-16 01:52:05 +02:00
|
|
|
} finally {
|
2018-11-19 19:56:27 +01:00
|
|
|
w.flush();
|
|
|
|
w.close();
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2018-11-19 19:56:27 +01:00
|
|
|
job.touch();
|
|
|
|
job.updating = false;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Properties parameters)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
long jobID = Long.parseLong(parameters.getProperty("jobID"));
|
|
|
|
ImportingJob job = ImportingManager.getJob(jobID);
|
|
|
|
if (job == null) {
|
|
|
|
HttpUtilities.respond(response, "error", "No such import job");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String format = request.getParameter("format");
|
|
|
|
Format formatRecord = ImportingManager.formatToRecord.get(format);
|
|
|
|
if (formatRecord != null && formatRecord.parser != null) {
|
2018-11-05 17:35:51 +01:00
|
|
|
ObjectNode options = formatRecord.parser.createParserUIInitializationData(
|
2013-07-25 21:41:08 +02:00
|
|
|
job, job.getSelectedFileRecords(), format);
|
2018-11-05 17:35:51 +01:00
|
|
|
ObjectNode result = ParsingUtilities.mapper.createObjectNode();
|
2011-08-02 05:34:47 +02:00
|
|
|
JSONUtilities.safePut(result, "status", "ok");
|
|
|
|
JSONUtilities.safePut(result, "options", options);
|
|
|
|
|
|
|
|
HttpUtilities.respond(response, result.toString());
|
|
|
|
} else {
|
|
|
|
HttpUtilities.respond(response, "error", "Unrecognized format or format has no parser");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doCreateProject(HttpServletRequest request, HttpServletResponse response, Properties parameters)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
long jobID = Long.parseLong(parameters.getProperty("jobID"));
|
|
|
|
ImportingJob job = ImportingManager.getJob(jobID);
|
|
|
|
if (job == null) {
|
|
|
|
HttpUtilities.respond(response, "error", "No such import job");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-16 01:52:05 +02:00
|
|
|
|
|
|
|
job.updating = true;
|
|
|
|
job.touch();
|
2018-11-19 19:56:27 +01:00
|
|
|
ObjectNode config = job.getOrCreateDefaultConfig();
|
|
|
|
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
|
|
|
|
HttpUtilities.respond(response, "error", "Job not ready");
|
|
|
|
return;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2018-11-19 19:56:27 +01:00
|
|
|
|
|
|
|
String format = request.getParameter("format");
|
|
|
|
ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(
|
|
|
|
request.getParameter("options"));
|
|
|
|
|
|
|
|
List<Exception> exceptions = new LinkedList<Exception>();
|
|
|
|
|
|
|
|
ImportingUtilities.createProject(job, format, optionObj, exceptions, false);
|
|
|
|
|
|
|
|
HttpUtilities.respond(response, "ok", "done");
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2018-02-02 14:24:19 +01:00
|
|
|
|
2018-10-03 22:33:37 +02:00
|
|
|
protected static class JobResponse {
|
|
|
|
@JsonProperty("code")
|
|
|
|
protected String code;
|
|
|
|
@JsonProperty("job")
|
|
|
|
protected ImportingJob job;
|
|
|
|
|
|
|
|
protected JobResponse(String code, ImportingJob job) {
|
|
|
|
this.code = code;
|
|
|
|
this.job = job;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:24:19 +01:00
|
|
|
/**
|
|
|
|
* return the job to the front end.
|
|
|
|
* @param request
|
|
|
|
* @param response
|
|
|
|
* @param job
|
|
|
|
* @throws ServletException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2011-08-02 05:34:47 +02:00
|
|
|
private void replyWithJobData(HttpServletRequest request, HttpServletResponse response, ImportingJob job)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
Writer w = response.getWriter();
|
2018-10-11 12:44:54 +02:00
|
|
|
ParsingUtilities.defaultWriter.writeValue(w, new JobResponse("ok",job));
|
2018-10-03 22:33:37 +02:00
|
|
|
w.flush();
|
|
|
|
w.close();
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2011-09-15 23:40:40 +02:00
|
|
|
|
2018-10-07 15:59:45 +02:00
|
|
|
static public void writeErrors(JsonGenerator writer, List<Exception> exceptions) throws IOException {
|
2011-09-15 23:40:40 +02:00
|
|
|
for (Exception e : exceptions) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
|
|
|
|
2018-10-07 15:59:45 +02:00
|
|
|
writer.writeStartObject();
|
|
|
|
writer.writeStringField("message", e.getLocalizedMessage());
|
|
|
|
writer.writeStringField("stack", sw.toString());
|
|
|
|
writer.writeEndObject();
|
2011-09-15 23:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:35:51 +01:00
|
|
|
static public ArrayNode convertErrorsToJsonArray(List<Exception> exceptions) {
|
|
|
|
ArrayNode a = ParsingUtilities.mapper.createArrayNode();
|
2011-09-15 23:40:40 +02:00
|
|
|
for (Exception e : exceptions) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
|
|
|
|
2018-11-05 17:35:51 +01:00
|
|
|
ObjectNode o = ParsingUtilities.mapper.createObjectNode();
|
2011-09-15 23:40:40 +02:00
|
|
|
JSONUtilities.safePut(o, "message", e.getLocalizedMessage());
|
|
|
|
JSONUtilities.safePut(o, "stack", sw.toString());
|
|
|
|
JSONUtilities.append(a, o);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|