Rewrite the servlets for the various endpoints to reuse the code from the abstract rest api class.
git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_result_distribution@1160 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/DDLAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/DDLAPIServlet.java
index 0d11890..76063e5 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/DDLAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/DDLAPIServlet.java
@@ -14,78 +14,19 @@
*/
package edu.uci.ics.asterix.api.http.servlet;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringReader;
+import java.util.Arrays;
import java.util.List;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
-import edu.uci.ics.asterix.api.common.SessionConfig;
import edu.uci.ics.asterix.aql.base.Statement;
-import edu.uci.ics.asterix.aql.parser.AQLParser;
-import edu.uci.ics.asterix.aql.parser.ParseException;
-import edu.uci.ics.asterix.aql.translator.AqlTranslator;
-import edu.uci.ics.asterix.metadata.MetadataManager;
-import edu.uci.ics.asterix.result.ResultReader;
-import edu.uci.ics.hyracks.api.client.HyracksConnection;
-import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
+import edu.uci.ics.asterix.aql.base.Statement.Kind;
-public class DDLAPIServlet extends HttpServlet {
+public class DDLAPIServlet extends RESTAPIServlet {
private static final long serialVersionUID = 1L;
- private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";
-
- @Override
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- String query = request.getParameter("query");
- String strIP = request.getParameter("hyracks-ip");
- String strPort = request.getParameter("hyracks-port");
- int port = Integer.parseInt(strPort);
- PrintWriter out = response.getWriter();
- response.setContentType("text/html");
- ServletContext context = getServletContext();
- IHyracksClientConnection hcc;
- ResultReader resultReader;
- try {
- synchronized (context) {
- hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);
- if (hcc == null) {
- hcc = new HyracksConnection(strIP, port);
- context.setAttribute(HYRACKS_CONNECTION_ATTR, hcc);
- }
- resultReader = new ResultReader(hcc, out);
- new Thread(resultReader).start();
- }
- AQLParser parser = new AQLParser(new StringReader(query));
- List<Statement> aqlStatements = parser.Statement();
- SessionConfig sessionConfig = new SessionConfig(port, true, false, false, false, false, false, false, false);
-
- MetadataManager.INSTANCE.init();
-
- AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, DisplayFormat.HTML);
-
- aqlTranslator.compileAndExecute(hcc, resultReader);
-
- } catch (ParseException pe) {
- String message = pe.getMessage();
- message = message.replace("<", "<");
- message = message.replace(">", ">");
- out.println("SyntaxError:" + message);
- int pos = message.indexOf("line");
- if (pos > 0) {
- int columnPos = message.indexOf(",", pos + 1 + "line".length());
- int lineNo = Integer.parseInt(message.substring(pos + "line".length() + 1, columnPos));
- String line = query.split("\n")[lineNo - 1];
- out.println("==> " + line);
- }
- } catch (Exception e) {
- out.println(e.getMessage());
- e.printStackTrace(out);
- }
+ protected List<Statement.Kind> getAllowedStatements() {
+ Kind[] statementsArray = { Kind.DATAVERSE_DECL, Kind.DATAVERSE_DROP, Kind.FUNCTION_DECL, Kind.NODEGROUP_DECL,
+ Kind.NODEGROUP_DROP, Kind.TYPE_DECL, Kind.TYPE_DROP, Kind.CREATE_INDEX, Kind.INDEX_DECL,
+ Kind.CREATE_DATAVERSE, Kind.INDEX_DROP, Kind.CREATE_FUNCTION, Kind.FUNCTION_DROP };
+ return Arrays.asList(statementsArray);
}
}
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/QueryAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/QueryAPIServlet.java
index 4e9659e..069c607 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/QueryAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/QueryAPIServlet.java
@@ -14,95 +14,18 @@
*/
package edu.uci.ics.asterix.api.http.servlet;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringReader;
+import java.util.Arrays;
import java.util.List;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.json.JSONArray;
-
-import twitter4j.internal.org.json.JSONObject;
-
-import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
-import edu.uci.ics.asterix.api.common.SessionConfig;
import edu.uci.ics.asterix.aql.base.Statement;
import edu.uci.ics.asterix.aql.base.Statement.Kind;
-import edu.uci.ics.asterix.aql.parser.AQLParser;
-import edu.uci.ics.asterix.aql.parser.ParseException;
-import edu.uci.ics.asterix.aql.translator.AqlTranslator;
-import edu.uci.ics.asterix.metadata.MetadataManager;
-import edu.uci.ics.hyracks.api.client.HyracksConnection;
-import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
-public class QueryAPIServlet extends HttpServlet {
+public class QueryAPIServlet extends RESTAPIServlet {
private static final long serialVersionUID = 1L;
- private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";
-
- private static final Kind QUERY_STMT = Kind.QUERY;
-
- @Override
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- String query = request.getParameter("query");
- String mode = request.getParameter("mode");
- boolean asyncResults = false;
- if (mode != null && mode.equals("asynchronous")) {
- asyncResults = true;
- }
- String strIP = "127.0.0.1";
- String strPort = "1098";
- int port = Integer.parseInt(strPort);
- PrintWriter out = response.getWriter();
- response.setContentType("text/html");
- ServletContext context = getServletContext();
- IHyracksClientConnection hcc;
- try {
- synchronized (context) {
- hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);
- if (hcc == null) {
- hcc = new HyracksConnection(strIP, port);
- context.setAttribute(HYRACKS_CONNECTION_ATTR, hcc);
- }
- }
- AQLParser parser = new AQLParser(new StringReader(query));
- List<Statement> aqlStatements = parser.Statement();
- for (Statement st : aqlStatements) {
- if (st.getKind() != QUERY_STMT) {
- JSONObject errorResp = new JSONObject();
- JSONArray errorArray = new JSONArray();
- errorArray.put("1");
- errorArray.put("Invalid stament: Non-query statement to the query API." + st.getKind());
- errorResp.put("error-code", errorArray);
- }
- }
- SessionConfig sessionConfig = new SessionConfig(port, true, false, false, false, false, false, true, false);
-
- MetadataManager.INSTANCE.init();
-
- AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, DisplayFormat.HTML);
-
- aqlTranslator.compileAndExecute(hcc, asyncResults);
-
- } catch (ParseException pe) {
- String message = pe.getMessage();
- message = message.replace("<", "<");
- message = message.replace(">", ">");
- out.println("SyntaxError:" + message);
- int pos = message.indexOf("line");
- if (pos > 0) {
- int columnPos = message.indexOf(",", pos + 1 + "line".length());
- int lineNo = Integer.parseInt(message.substring(pos + "line".length() + 1, columnPos));
- String line = query.split("\n")[lineNo - 1];
- out.println("==> " + line);
- }
- } catch (Exception e) {
- out.println(e.getMessage());
- e.printStackTrace(out);
- }
+ protected List<Statement.Kind> getAllowedStatements() {
+ Kind[] statementsArray = { Kind.DATAVERSE_DECL, Kind.QUERY, Kind.SET, Kind.WRITE, Kind.BEGIN_FEED,
+ Kind.CONTROL_FEED };
+ return Arrays.asList(statementsArray);
}
}
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
new file mode 100644
index 0000000..e8660a9
--- /dev/null
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2009-2011 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package edu.uci.ics.asterix.api.http.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.json.JSONObject;
+
+import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
+import edu.uci.ics.asterix.api.common.SessionConfig;
+import edu.uci.ics.asterix.aql.base.Statement;
+import edu.uci.ics.asterix.aql.base.Statement.Kind;
+import edu.uci.ics.asterix.aql.parser.AQLParser;
+import edu.uci.ics.asterix.aql.parser.ParseException;
+import edu.uci.ics.asterix.aql.translator.AqlTranslator;
+import edu.uci.ics.asterix.metadata.MetadataManager;
+import edu.uci.ics.asterix.result.ResultUtils;
+import edu.uci.ics.hyracks.api.client.HyracksConnection;
+import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
+
+abstract class RESTAPIServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";
+
+ @Override
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ PrintWriter out = response.getWriter();
+
+ String query = request.getParameter("query");
+ String mode = request.getParameter("mode");
+ response.setContentType("text/html");
+ ServletContext context = getServletContext();
+ IHyracksClientConnection hcc;
+
+ boolean asyncResults = false;
+ if (mode != null && mode.equals("asynchronous")) {
+ asyncResults = true;
+ }
+
+ try {
+ HyracksProperties hp = new HyracksProperties();
+ String strIP = hp.getHyracksIPAddress();
+ int port = hp.getHyracksPort();
+
+ synchronized (context) {
+ hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);
+ if (hcc == null) {
+ hcc = new HyracksConnection(strIP, port);
+ context.setAttribute(HYRACKS_CONNECTION_ATTR, hcc);
+ }
+ }
+ AQLParser parser = new AQLParser(new StringReader(query));
+ List<Statement> aqlStatements = parser.Statement();
+ if (checkForbiddenStatements(aqlStatements, out)) {
+ return;
+ }
+ SessionConfig sessionConfig = new SessionConfig(port, true, false, false, false, false, false, true, false);
+
+ MetadataManager.INSTANCE.init();
+
+ AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, DisplayFormat.JSON);
+
+ aqlTranslator.compileAndExecute(hcc, asyncResults);
+
+ } catch (ParseException pe) {
+ StringBuilder errorMessage = new StringBuilder();
+ String message = pe.getMessage();
+ message = message.replace("<", "<");
+ message = message.replace(">", ">");
+ errorMessage.append("SyntaxError:" + message + "\n");
+ int pos = message.indexOf("line");
+ if (pos > 0) {
+ int columnPos = message.indexOf(",", pos + 1 + "line".length());
+ int lineNo = Integer.parseInt(message.substring(pos + "line".length() + 1, columnPos));
+ String line = query.split("\n")[lineNo - 1];
+ errorMessage.append("==> " + line + "\n");
+ }
+ JSONObject errorResp = ResultUtils.getErrorResponse(2, errorMessage.toString());
+ out.write(errorResp.toString());
+ } catch (Exception e) {
+ StringBuilder errorMessage = new StringBuilder();
+ errorMessage.append(e.getMessage());
+ JSONObject errorResp = ResultUtils.getErrorResponse(99, errorMessage.toString());
+ out.write(errorResp.toString());
+ }
+ }
+
+ private boolean checkForbiddenStatements(List<Statement> aqlStatements, PrintWriter out) {
+ for (Statement st : aqlStatements) {
+ if (!getAllowedStatements().contains(st.getKind())) {
+ JSONObject errorResp = ResultUtils.getErrorResponse(1,
+ "Invalid stament: Non-query statement " + st.getKind() + " to the query API.");
+ out.write(errorResp.toString());
+ return true;
+ }
+ }
+ return false;
+ }
+
+ protected abstract List<Kind> getAllowedStatements();
+}
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
index a9c1a10..400fe47 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
@@ -14,78 +14,18 @@
*/
package edu.uci.ics.asterix.api.http.servlet;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringReader;
+import java.util.Arrays;
import java.util.List;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
-import edu.uci.ics.asterix.api.common.SessionConfig;
import edu.uci.ics.asterix.aql.base.Statement;
-import edu.uci.ics.asterix.aql.parser.AQLParser;
-import edu.uci.ics.asterix.aql.parser.ParseException;
-import edu.uci.ics.asterix.aql.translator.AqlTranslator;
-import edu.uci.ics.asterix.metadata.MetadataManager;
-import edu.uci.ics.asterix.result.ResultReader;
-import edu.uci.ics.hyracks.api.client.HyracksConnection;
-import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
+import edu.uci.ics.asterix.aql.base.Statement.Kind;
-public class UpdateAPIServlet extends HttpServlet {
+public class UpdateAPIServlet extends RESTAPIServlet {
private static final long serialVersionUID = 1L;
- private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";
-
- @Override
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- String query = request.getParameter("query");
- String strIP = request.getParameter("hyracks-ip");
- String strPort = request.getParameter("hyracks-port");
- int port = Integer.parseInt(strPort);
- PrintWriter out = response.getWriter();
- response.setContentType("text/html");
- ServletContext context = getServletContext();
- IHyracksClientConnection hcc;
- ResultReader resultReader;
- try {
- synchronized (context) {
- hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);
- if (hcc == null) {
- hcc = new HyracksConnection(strIP, port);
- context.setAttribute(HYRACKS_CONNECTION_ATTR, hcc);
- }
- resultReader = new ResultReader(hcc, out);
- new Thread(resultReader).start();
- }
- AQLParser parser = new AQLParser(new StringReader(query));
- List<Statement> aqlStatements = parser.Statement();
- SessionConfig sessionConfig = new SessionConfig(port, true, false, false, false, false, false, false, false);
-
- MetadataManager.INSTANCE.init();
-
- AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, DisplayFormat.HTML);
-
- aqlTranslator.compileAndExecute(hcc, resultReader);
-
- } catch (ParseException pe) {
- String message = pe.getMessage();
- message = message.replace("<", "<");
- message = message.replace(">", ">");
- out.println("SyntaxError:" + message);
- int pos = message.indexOf("line");
- if (pos > 0) {
- int columnPos = message.indexOf(",", pos + 1 + "line".length());
- int lineNo = Integer.parseInt(message.substring(pos + "line".length() + 1, columnPos));
- String line = query.split("\n")[lineNo - 1];
- out.println("==> " + line);
- }
- } catch (Exception e) {
- out.println(e.getMessage());
- e.printStackTrace(out);
- }
+ protected List<Statement.Kind> getAllowedStatements() {
+ Kind[] statementsArray = { Kind.DATAVERSE_DECL, Kind.DATASET_DECL, Kind.DATASET_DROP, Kind.DELETE, Kind.INSERT,
+ Kind.UPDATE, Kind.DML_CMD_LIST, Kind.LOAD_FROM_FILE, Kind.WRITE_FROM_QUERY_RESULT };
+ return Arrays.asList(statementsArray);
}
}