示例#1
0
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    PrintWriter out = response.getWriter();

    DisplayFormat format = DisplayFormat.HTML;

    String contentType = request.getContentType();

    if ((contentType == null) || (contentType.equals("text/plain"))) {
      format = DisplayFormat.TEXT;
    } else if (contentType.equals("application/json")) {
      format = DisplayFormat.JSON;
    }

    String query = getQueryParameter(request);
    boolean asyncResults = isAsync(request);

    ServletContext context = getServletContext();
    IHyracksClientConnection hcc;
    IHyracksDataset hds;

    try {
      synchronized (context) {
        hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);

        hds = (IHyracksDataset) context.getAttribute(HYRACKS_DATASET_ATTR);
        if (hds == null) {
          hds = new HyracksDataset(hcc, ResultReader.FRAME_SIZE, ResultReader.NUM_READERS);
          context.setAttribute(HYRACKS_DATASET_ATTR, hds);
        }
      }

      AQLParser parser = new AQLParser(query);
      List<Statement> aqlStatements = parser.Statement();
      if (checkForbiddenStatements(aqlStatements, out)) {
        return;
      }
      SessionConfig sessionConfig =
          new SessionConfig(true, false, false, false, false, false, true, false);

      MetadataManager.INSTANCE.init();

      AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, format);

      aqlTranslator.compileAndExecute(hcc, hds, asyncResults);

    } catch (ParseException pe) {
      StringBuilder errorMessage = new StringBuilder();
      String message = pe.getMessage();
      message = message.replace("<", "&lt");
      message = message.replace(">", "&gt");
      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());
    }
  }
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    OutputFormat format;
    boolean csv_and_header = false;
    String output = request.getParameter("output-format");
    if (output.equals("ADM")) {
      format = OutputFormat.ADM;
    } else if (output.equals("CSV")) {
      format = OutputFormat.CSV;
    } else if (output.equals("CSV-Header")) {
      format = OutputFormat.CSV;
      csv_and_header = true;
    } else {
      // Default output format
      format = OutputFormat.JSON;
    }

    String query = request.getParameter("query");
    String printExprParam = request.getParameter("print-expr-tree");
    String printRewrittenExprParam = request.getParameter("print-rewritten-expr-tree");
    String printLogicalPlanParam = request.getParameter("print-logical-plan");
    String printOptimizedLogicalPlanParam = request.getParameter("print-optimized-logical-plan");
    String printJob = request.getParameter("print-job");
    String executeQuery = request.getParameter("execute-query");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    ServletContext context = getServletContext();
    IHyracksClientConnection hcc;
    IHyracksDataset hds;

    try {
      synchronized (context) {
        hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);

        hds = (IHyracksDataset) context.getAttribute(HYRACKS_DATASET_ATTR);
        if (hds == null) {
          hds = new HyracksDataset(hcc, ResultReader.FRAME_SIZE, ResultReader.NUM_READERS);
          context.setAttribute(HYRACKS_DATASET_ATTR, hds);
        }
      }
      AQLParser parser = new AQLParser(query);
      List<Statement> aqlStatements = parser.parse();
      SessionConfig sessionConfig = new SessionConfig(out, format, true, isSet(executeQuery), true);
      sessionConfig.set(SessionConfig.FORMAT_HTML, true);
      sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, csv_and_header);
      sessionConfig.setOOBData(
          isSet(printExprParam),
          isSet(printRewrittenExprParam),
          isSet(printLogicalPlanParam),
          isSet(printOptimizedLogicalPlanParam),
          isSet(printJob));
      MetadataManager.INSTANCE.init();
      AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, sessionConfig);
      double duration = 0;
      long startTime = System.currentTimeMillis();
      aqlTranslator.compileAndExecute(hcc, hds, AqlTranslator.ResultDelivery.SYNC);
      long endTime = System.currentTimeMillis();
      duration = (endTime - startTime) / 1000.00;
      out.println("<PRE>Duration of all jobs: " + duration + " sec</PRE>");
    } catch (ParseException | TokenMgrError | edu.uci.ics.asterix.aqlplus.parser.TokenMgrError pe) {
      GlobalConfig.ASTERIX_LOGGER.log(Level.INFO, pe.toString(), pe);
      ResultUtils.webUIParseExceptionHandler(out, pe, query);
    } catch (Exception e) {
      GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
      ResultUtils.webUIErrorHandler(out, e);
    }
  }