2010-01-24 22:09:50 +01:00
|
|
|
package com.metaweb.gridlock;
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.io.File;
|
2010-01-24 22:09:50 +01:00
|
|
|
import java.io.IOException;
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import com.metaweb.gridlock.commands.Command;
|
|
|
|
import com.metaweb.gridlock.commands.CreateProjectFromUploadCommand;
|
|
|
|
import com.metaweb.gridlock.commands.GetColumnModelCommand;
|
|
|
|
import com.metaweb.gridlock.commands.GetRowsCommand;
|
|
|
|
|
2010-01-24 22:09:50 +01:00
|
|
|
public class GridlockServlet extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 2386057901503517403L;
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
static protected Map<String, Command> _commands = new HashMap<String, Command>();
|
|
|
|
|
|
|
|
static {
|
|
|
|
_commands.put("create-project-from-upload", new CreateProjectFromUploadCommand());
|
|
|
|
_commands.put("get-column-model", new GetColumnModelCommand());
|
|
|
|
_commands.put("get-rows", new GetRowsCommand());
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() throws ServletException {
|
|
|
|
super.init();
|
2010-01-25 23:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroy() {
|
|
|
|
ProjectManager.singleton.saveAllProjects();
|
|
|
|
ProjectManager.singleton.save();
|
|
|
|
ProjectManager.singleton = null;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
super.destroy();
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2010-01-25 23:51:25 +01:00
|
|
|
ProjectManager.initialize(new File("./data"));
|
|
|
|
|
|
|
|
String commandName = request.getPathInfo().substring(1);
|
|
|
|
Command command = _commands.get(commandName);
|
|
|
|
if (command != null) {
|
|
|
|
command.doPost(request, response);
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2010-01-25 23:51:25 +01:00
|
|
|
ProjectManager.initialize(new File("./data"));
|
|
|
|
|
|
|
|
String commandName = request.getPathInfo().substring(1);
|
|
|
|
Command command = _commands.get(commandName);
|
|
|
|
if (command != null) {
|
|
|
|
command.doGet(request, response);
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static public JSONObject evaluateJsonStringToObject(String s) throws JSONException {
|
|
|
|
JSONTokener t = new JSONTokener(s);
|
|
|
|
JSONObject o = (JSONObject) t.nextValue();
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String encodeString(String s) {
|
|
|
|
return s.replace("\"", "\\\"");
|
|
|
|
}
|
|
|
|
}
|