<commit_before>package edu.uci.ics.asterix.api.http.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

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;
import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
import edu.uci.ics.asterix.api.common.Job;
import edu.uci.ics.asterix.api.common.SessionConfig;
import edu.uci.ics.asterix.aql.expression.Query;
import edu.uci.ics.asterix.aql.parser.AQLParser;
import edu.uci.ics.asterix.aql.parser.ParseException;
import edu.uci.ics.asterix.common.config.GlobalConfig;
import edu.uci.ics.asterix.metadata.MetadataManager;
import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
import edu.uci.ics.hyracks.api.client.HyracksConnection;
import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
import edu.uci.ics.hyracks.api.job.JobSpecification;

public class APIServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        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 strIP = request.getParameter("hyracks-ip");
        String strPort = request.getParameter("hyracks-port");
        String strDisplayResult = request.getParameter("display-result");
        int port = Integer.parseInt(strPort);
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        out.println("<H1>Input statements:</H1>");
        printInHtml(out, query);
        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));
            Query q = (Query) parser.Statement();
            SessionConfig pc = new SessionConfig(port, true, isSet(printExprParam), isSet(printRewrittenExprParam),
                    isSet(printLogicalPlanParam), isSet(printOptimizedLogicalPlanParam), false, isSet(printJob));
            pc.setGenerateJobSpec(true);

            MetadataManager.INSTANCE.init();
            String dataverseName = null;

            if (q != null) {
                dataverseName = postDmlStatement(hcc, q, out, pc);
            }

            if (q.isDummyQuery()) {
                return;
            }

            Pair<AqlCompiledMetadataDeclarations, JobSpecification> metadataAndSpec = APIFramework.compileQuery(
                    dataverseName, q, parser.getVarCounter(), null, null, pc, out, DisplayFormat.HTML, null);
            JobSpecification spec = metadataAndSpec.second;
            GlobalConfig.ASTERIX_LOGGER.info(spec.toJSON().toString(1));
            AqlCompiledMetadataDeclarations metadata = metadataAndSpec.first;
            long startTime = System.currentTimeMillis();
            APIFramework.executeJobArray(hcc, new JobSpecification[] { spec }, out, DisplayFormat.HTML);
            long endTime = System.currentTimeMillis();
            double duration = (endTime - startTime) / 1000.00;
            out.println("<H1>Result:</H1>");

            out.println("<PRE>");
            out.println(metadata.getOutputFile().getNodeName() + ":"
                    + metadata.getOutputFile().getLocalFile().getFile().getPath());
            out.println("Duration: " + duration);
            out.println("</PRE>");

            if (isSet(strDisplayResult)) {
                out.println("<PRE>");
                displayFile(metadata.getOutputFile().getLocalFile().getFile(), out);
                out.println("</PRE>");
            }
        } catch (ParseException pe) {
            String message = pe.getMessage();
            message = message.replace("<", "&lt");
            message = message.replace(">", "&gt");
            int pos = message.indexOf("line");
            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("SyntaxError:" + message);
            out.println("==> " + line);

        } catch (Exception e) {
            out.println(e.getMessage());
       }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        final String form = "<form method=\"post\">"
                + "<center><textarea cols=\"80\" rows=\"25\" name=\"query\" ></textarea><br/>"<commit_msg>Fixed issue 142.<commit_after>package edu.uci.ics.asterix.api.http.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

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;
import edu.uci.ics.asterix.api.common.APIFramework.DisplayFormat;
import edu.uci.ics.asterix.api.common.Job;
import edu.uci.ics.asterix.api.common.SessionConfig;
import edu.uci.ics.asterix.aql.expression.Query;
import edu.uci.ics.asterix.aql.parser.AQLParser;
import edu.uci.ics.asterix.aql.parser.ParseException;
import edu.uci.ics.asterix.common.config.GlobalConfig;
import edu.uci.ics.asterix.metadata.MetadataManager;
import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
import edu.uci.ics.hyracks.api.client.HyracksConnection;
import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
import edu.uci.ics.hyracks.api.job.JobSpecification;

public class APIServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String HYRACKS_CONNECTION_ATTR = "edu.uci.ics.asterix.HYRACKS_CONNECTION";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        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 strIP = request.getParameter("hyracks-ip");
        String strPort = request.getParameter("hyracks-port");
        String strDisplayResult = request.getParameter("display-result");
        int port = Integer.parseInt(strPort);
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        out.println("<H1>Input statements:</H1>");
        printInHtml(out, query);
        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));
            Query q = (Query) parser.Statement();
            SessionConfig pc = new SessionConfig(port, true, isSet(printExprParam), isSet(printRewrittenExprParam),
                    isSet(printLogicalPlanParam), isSet(printOptimizedLogicalPlanParam), false, isSet(printJob));
            pc.setGenerateJobSpec(true);

            MetadataManager.INSTANCE.init();
            String dataverseName = null;

            if (q != null) {
                dataverseName = postDmlStatement(hcc, q, out, pc);
            }

            if (q.isDummyQuery()) {
                return;
            }

            Pair<AqlCompiledMetadataDeclarations, JobSpecification> metadataAndSpec = APIFramework.compileQuery(
                    dataverseName, q, parser.getVarCounter(), null, null, pc, out, DisplayFormat.HTML, null);
            JobSpecification spec = metadataAndSpec.second;
            GlobalConfig.ASTERIX_LOGGER.info(spec.toJSON().toString(1));
            AqlCompiledMetadataDeclarations metadata = metadataAndSpec.first;
            long startTime = System.currentTimeMillis();
            APIFramework.executeJobArray(hcc, new JobSpecification[] { spec }, out, DisplayFormat.HTML);
            long endTime = System.currentTimeMillis();
            double duration = (endTime - startTime) / 1000.00;
            out.println("<H1>Result:</H1>");

            out.println("<PRE>");
            out.println(metadata.getOutputFile().getNodeName() + ":"
                    + metadata.getOutputFile().getLocalFile().getFile().getPath());
            out.println("Duration: " + duration);
            out.println("</PRE>");

            if (isSet(strDisplayResult)) {
                out.println("<PRE>");
                displayFile(metadata.getOutputFile().getLocalFile().getFile(), out);
                out.println("</PRE>");
            }
        } catch (ParseException pe) {
        	String message = pe.getMessage();
        	message = message.replace("<", "&lt");
        	message = message.replace(">", "&gt");
        	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());
       }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        final String form = "<form method=\"post\">"
                + "<center><textarea cols=\"80\" rows=\"25\" name=\"query\" ></textarea><br/>"
{'fraction_non_alphanumeric': 0.12218370883882149, 'fraction_numerical': 0.003639514731369151, 'mean_word_length': 2.8572860962566846, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772599', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3256, 'n_words': 568}