2010-10-20 22:45:52 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright 2010, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.commands;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
2010-10-13 06:51:01 +02:00
|
|
|
import org.apache.velocity.VelocityContext;
|
2010-05-05 01:24:48 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2010-09-22 19:46:39 +02:00
|
|
|
import com.google.refine.RefineServlet;
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.Jsonizable;
|
|
|
|
import com.google.refine.ProjectManager;
|
|
|
|
import com.google.refine.ProjectMetadata;
|
|
|
|
import com.google.refine.browsing.Engine;
|
|
|
|
import com.google.refine.history.HistoryEntry;
|
|
|
|
import com.google.refine.model.Project;
|
|
|
|
import com.google.refine.process.Process;
|
|
|
|
import com.google.refine.util.ParsingUtilities;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The super class of all calls that the client side can invoke, most of which
|
|
|
|
* are AJAX calls.
|
|
|
|
*/
|
|
|
|
public abstract class Command {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
final static protected Logger logger = LoggerFactory.getLogger("command");
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-09-22 19:46:39 +02:00
|
|
|
protected RefineServlet servlet;
|
2010-05-29 01:19:08 +02:00
|
|
|
|
2010-09-22 19:46:39 +02:00
|
|
|
public void init(RefineServlet servlet) {
|
2010-05-29 01:19:08 +02:00
|
|
|
this.servlet = servlet;
|
|
|
|
}
|
|
|
|
|
2010-05-13 02:54:02 +02:00
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
2010-05-05 01:24:48 +02:00
|
|
|
throws ServletException, IOException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-13 23:02:19 +02:00
|
|
|
throw new UnsupportedOperationException();
|
2010-05-05 01:24:48 +02:00
|
|
|
};
|
2010-05-13 02:54:02 +02:00
|
|
|
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
2010-05-05 01:24:48 +02:00
|
|
|
throws ServletException, IOException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-13 23:02:19 +02:00
|
|
|
throw new UnsupportedOperationException();
|
2010-05-05 01:24:48 +02:00
|
|
|
};
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
/**
|
|
|
|
* Utility function to get the browsing engine's configuration as a JSON object
|
|
|
|
* from the "engine" request parameter, most often in the POST body.
|
2010-05-13 02:54:02 +02:00
|
|
|
*
|
2010-05-05 01:24:48 +02:00
|
|
|
* @param request
|
|
|
|
* @return
|
2010-05-13 11:23:37 +02:00
|
|
|
* @throws JSONException
|
2010-05-05 01:24:48 +02:00
|
|
|
*/
|
2010-05-13 02:54:02 +02:00
|
|
|
static protected JSONObject getEngineConfig(HttpServletRequest request)
|
|
|
|
throws JSONException {
|
|
|
|
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
|
2010-05-13 11:23:37 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
String json = request.getParameter("engine");
|
2010-05-13 11:23:37 +02:00
|
|
|
try{
|
|
|
|
return (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
|
|
|
|
} catch (JSONException e){
|
|
|
|
logger.debug( json + " could not be parsed to JSON");
|
|
|
|
return null;
|
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
/**
|
2010-05-13 02:54:02 +02:00
|
|
|
* Utility function to reconstruct the browsing engine from the "engine" request parameter,
|
2010-05-05 01:24:48 +02:00
|
|
|
* most often in the POST body.
|
2010-05-13 02:54:02 +02:00
|
|
|
*
|
2010-05-05 01:24:48 +02:00
|
|
|
* @param request
|
|
|
|
* @param project
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2010-05-13 02:54:02 +02:00
|
|
|
static protected Engine getEngine(HttpServletRequest request, Project project)
|
|
|
|
throws Exception {
|
|
|
|
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
|
|
|
|
if (project == null) throw new IllegalArgumentException("parameter 'project' should not be null");
|
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
Engine engine = new Engine(project);
|
2010-05-13 02:54:02 +02:00
|
|
|
JSONObject o = getEngineConfig(request);
|
|
|
|
if (o != null)
|
2010-05-05 01:24:48 +02:00
|
|
|
engine.initializeFromJSON(o);
|
|
|
|
return engine;
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
/**
|
|
|
|
* Utility method for retrieving the Project object having the ID specified
|
|
|
|
* in the "project" URL parameter.
|
2010-05-13 02:54:02 +02:00
|
|
|
*
|
2010-05-05 01:24:48 +02:00
|
|
|
* @param request
|
|
|
|
* @return
|
|
|
|
* @throws ServletException
|
|
|
|
*/
|
2010-05-13 02:54:02 +02:00
|
|
|
protected Project getProject(HttpServletRequest request) throws ServletException {
|
2010-10-07 16:20:28 +02:00
|
|
|
if (request == null) {
|
|
|
|
throw new IllegalArgumentException("parameter 'request' should not be null");
|
|
|
|
}
|
|
|
|
String param = request.getParameter("project");
|
|
|
|
if (param == null || "".equals(param)) {
|
|
|
|
throw new ServletException("Can't find project: missing ID parameter");
|
|
|
|
}
|
|
|
|
Long id;
|
2010-05-05 01:24:48 +02:00
|
|
|
try {
|
2010-10-07 16:20:28 +02:00
|
|
|
id = Long.parseLong(param);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new ServletException("Can't find project: badly formatted id #", e);
|
|
|
|
}
|
|
|
|
Project p = ProjectManager.singleton.getProject(id);
|
|
|
|
if (p != null) {
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
throw new ServletException("Failed to find project id #" + param + " - may be corrupt");
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-17 21:22:58 +02:00
|
|
|
/**
|
|
|
|
* Utility method for retrieving the ProjectMetadata object having the ID specified
|
|
|
|
* in the "project" URL parameter.
|
|
|
|
*
|
|
|
|
* @param request
|
|
|
|
* @return
|
|
|
|
* @throws ServletException
|
|
|
|
*/
|
|
|
|
protected ProjectMetadata getProjectMetadata(HttpServletRequest request) throws ServletException {
|
|
|
|
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
|
|
|
|
try {
|
|
|
|
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(Long.parseLong(request.getParameter("project")));
|
|
|
|
if (pm != null) {
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw new ServletException("Can't find project metadata: missing or bad URL parameter");
|
|
|
|
}
|
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static protected int getIntegerParameter(HttpServletRequest request, String name, int def) {
|
2010-05-13 02:54:02 +02:00
|
|
|
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
|
2010-05-05 01:24:48 +02:00
|
|
|
try {
|
|
|
|
return Integer.parseInt(request.getParameter(name));
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static protected JSONObject getJsonParameter(HttpServletRequest request, String name) {
|
2010-05-13 02:54:02 +02:00
|
|
|
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
|
2010-05-05 01:24:48 +02:00
|
|
|
String value = request.getParameter(name);
|
|
|
|
if (value != null) {
|
|
|
|
try {
|
|
|
|
return ParsingUtilities.evaluateJsonStringToObject(value);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
logger.warn("error getting json parameter",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static protected void performProcessAndRespond(
|
2010-05-13 02:54:02 +02:00
|
|
|
HttpServletRequest request,
|
2010-05-05 01:24:48 +02:00
|
|
|
HttpServletResponse response,
|
|
|
|
Project project,
|
|
|
|
Process process
|
|
|
|
) throws Exception {
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
HistoryEntry historyEntry = project.processManager.queueProcess(process);
|
|
|
|
if (historyEntry != null) {
|
|
|
|
Writer w = response.getWriter();
|
|
|
|
JSONWriter writer = new JSONWriter(w);
|
|
|
|
Properties options = new Properties();
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
writer.object();
|
|
|
|
writer.key("code"); writer.value("ok");
|
|
|
|
writer.key("historyEntry"); historyEntry.write(writer, options);
|
|
|
|
writer.endObject();
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
w.flush();
|
|
|
|
w.close();
|
|
|
|
} else {
|
|
|
|
respond(response, "{ \"code\" : \"pending\" }");
|
|
|
|
}
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
|
|
|
static protected void respond(HttpServletResponse response, String content)
|
2010-05-12 10:59:05 +02:00
|
|
|
throws IOException, ServletException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
Writer w = response.getWriter();
|
2010-05-12 10:59:05 +02:00
|
|
|
if (w != null) {
|
|
|
|
w.write(content);
|
|
|
|
w.flush();
|
|
|
|
w.close();
|
|
|
|
} else {
|
|
|
|
throw new ServletException("response returned a null writer");
|
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
|
|
|
static protected void respond(HttpServletResponse response, String status, String message)
|
2010-05-05 01:24:48 +02:00
|
|
|
throws IOException, JSONException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
Writer w = response.getWriter();
|
|
|
|
JSONWriter writer = new JSONWriter(w);
|
|
|
|
writer.object();
|
|
|
|
writer.key("status"); writer.value(status);
|
|
|
|
writer.key("message"); writer.value(message);
|
|
|
|
writer.endObject();
|
|
|
|
w.flush();
|
|
|
|
w.close();
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
|
|
|
static protected void respondJSON(HttpServletResponse response, Jsonizable o)
|
2010-05-05 01:24:48 +02:00
|
|
|
throws IOException, JSONException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
respondJSON(response, o, new Properties());
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static protected void respondJSON(
|
2010-05-13 02:54:02 +02:00
|
|
|
HttpServletResponse response, Jsonizable o, Properties options)
|
2010-05-05 01:24:48 +02:00
|
|
|
throws IOException, JSONException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
Writer w = response.getWriter();
|
|
|
|
JSONWriter writer = new JSONWriter(w);
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
o.write(writer, options);
|
|
|
|
w.flush();
|
|
|
|
w.close();
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
|
|
|
static protected void respondException(HttpServletResponse response, Exception e)
|
2010-05-12 10:59:05 +02:00
|
|
|
throws IOException, ServletException {
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
logger.warn("Exception caught", e);
|
|
|
|
|
2010-05-12 10:59:05 +02:00
|
|
|
if (response == null) {
|
|
|
|
throw new ServletException("Response object can't be null");
|
|
|
|
}
|
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
try {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
o.put("code", "error");
|
|
|
|
o.put("message", e.getMessage());
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
pw.flush();
|
|
|
|
sw.flush();
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
o.put("stack", sw.toString());
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
respond(response, o.toString());
|
|
|
|
} catch (JSONException e1) {
|
|
|
|
e.printStackTrace(response.getWriter());
|
|
|
|
}
|
|
|
|
}
|
2010-10-13 06:51:01 +02:00
|
|
|
|
|
|
|
protected void respondWithErrorPage(
|
|
|
|
HttpServletRequest request,
|
|
|
|
HttpServletResponse response,
|
|
|
|
String message,
|
|
|
|
Exception e
|
|
|
|
) {
|
|
|
|
VelocityContext context = new VelocityContext();
|
|
|
|
|
|
|
|
context.put("message", message);
|
|
|
|
|
|
|
|
if (e != null) {
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
|
|
|
|
e.printStackTrace(new PrintWriter(writer));
|
|
|
|
|
|
|
|
context.put("stack", writer.toString());
|
|
|
|
} else {
|
|
|
|
context.put("stack", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
servlet.getModule("core").sendTextFromTemplate(
|
|
|
|
request, response, context, "error.vt", "UTF-8", "text/html", true);
|
|
|
|
|
|
|
|
} catch (Exception e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static protected void redirect(HttpServletResponse response, String url) throws IOException {
|
|
|
|
response.sendRedirect(url);
|
|
|
|
}
|
2010-05-13 02:54:02 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|