package dovetaildb.dbrepository;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dovetaildb.apiservice.ApiException;
import dovetaildb.util.Util;
public class ParsedRequest {
public String db;
public String bagName;
public String id;
public String action;
public String scriptLanguage;
public String methodName;
public HttpServletRequest request;
public HttpServletResponse response;
/*
* Post Paths:
* /mydb/mybag/query <"query" as json blob>
* /mydb/mybag/insert <"entry" as json blob>
* /mydb/mybag[/id]/insert <"entry" as json blob>
* /mydb/mybag/id/update <"entry" as json blob>
* /mydb/mybag/id/remove
* /mydb/call/js/myfn <"arguments" json list of parameters>
* /mydb/execute/js <"code">
* /_metadata/execute/js <"code">
* /_metadata/call/js/repo.list_code_files <"arguments" json list of parameters>
*/
static final Pattern slashRegexp = Pattern.compile("/+");
static final Map<String,String> cannonicalActionStrings = Util.literalMap()
.p("query","query")
.p("query","insert")
.p("query","update")
.p("query","remove");
public ParsedRequest(String url, boolean insertOrUpdate, HttpServletRequest request, HttpServletResponse response) {
String[] parts = slashRegexp.split(url);
int lastPartIndex = parts.length - 1;
db = parts[0];
this.request = request;
this.response = response;
String secondElement = parts[1];
if (secondElement.equals("call")) {
action = "call";
bagName = null;
scriptLanguage = parts[2];
methodName = parts[3];
} else if (secondElement.equals("execute")) {
action = "invoke";
scriptLanguage = parts[2];
} else {
bagName = secondElement;
String actionString = parts[lastPartIndex];
action = cannonicalActionStrings.get(actionString);
if (action == null)
throw new ApiException("UnsupportedAction", "Unsupported action: \""+actionString+"\"");
if (lastPartIndex == 3) {
id = parts[2];
} else if (action.equals("update") && insertOrUpdate) {
action = "insert";
}
}
}
}