<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2011 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet_voltpatches.ServletException;
import javax.servlet_voltpatches.http.HttpServletRequest;
import javax.servlet_voltpatches.http.HttpServletResponse;

import org.eclipse.jetty_voltpatches.server.AsyncContinuation;
import org.eclipse.jetty_voltpatches.server.Request;
import org.eclipse.jetty_voltpatches.server.Server;
import org.eclipse.jetty_voltpatches.server.bio.SocketConnector;
import org.eclipse.jetty_voltpatches.server.handler.AbstractHandler;
import org.voltdb.CatalogContext;
import org.voltdb.HTTPClientInterface;
import org.voltdb.VoltDB;
import org.voltdb.logging.VoltLogger;

public class HTTPAdminListener {

    Server m_server = new Server();
    HTTPClientInterface httpClientInterface = new HTTPClientInterface();
    final boolean m_jsonEnabled;
    Map<String, String> m_htmlTemplates = new HashMap<String, String>();

    class RequestHandler extends AbstractHandler {

        @Override
        public void handle(String target,
                           Request baseRequest,
                           HttpServletRequest request,
                           HttpServletResponse response)
                           throws IOException, ServletException {

            // if this is an internal jetty retry, then just tell
            // jetty we're still working on it. There is a risk of
            // masking other errors in doing this, but it's probably
            // low compared with the default policy of retrys.
            AsyncContinuation cont = baseRequest.getAsyncContinuation();
            // this is set to false on internal jetty retrys
            if (!cont.isInitial()) {
                // The continuation object has been woken up by the
                // retry. Tell it to go back to sleep.
                cont.suspend();
                return;
            }

            // kick over to the HTTP/JSON interface
            if (baseRequest.getRequestURI().contains("/api/1.0/")) {
                response.setContentType("text/plain;charset=utf-8");
                if (m_jsonEnabled) {
                    httpClientInterface.process(baseRequest, response);

                    // used for perf testing of the http interface
                    /*String msg = "{\"status\":1,\"appstatus\":-128,\"statusstring\":null,\"appstatusstring\":null,\"exception\":null,\"results\":[{\"status\":-128,\"schema\":[{\"name\":\"SVAL1\",\"type\":9},{\"name\":\"SVAL2\",\"type\":9},{\"name\":\"SVAL3\",\"type\":9}],\"data\":[[\"FOO\",\"BAR\",\"BOO\"]]}]}";
                    response.setStatus(HttpServletResponse.SC_OK);
                    baseRequest.setHandled(true);
                    response.getWriter().print(msg);*/
                }
                else {
                    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                    baseRequest.setHandled(true);
                    response.getWriter().println("JSON API IS CURRENTLY DISABLED");
                }<commit_msg>Fix for ENG-996. MIME type for JSON api changed from text/plain to application/json per Network Working Group RFC-4627.<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2011 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet_voltpatches.ServletException;
import javax.servlet_voltpatches.http.HttpServletRequest;
import javax.servlet_voltpatches.http.HttpServletResponse;

import org.eclipse.jetty_voltpatches.server.AsyncContinuation;
import org.eclipse.jetty_voltpatches.server.Request;
import org.eclipse.jetty_voltpatches.server.Server;
import org.eclipse.jetty_voltpatches.server.bio.SocketConnector;
import org.eclipse.jetty_voltpatches.server.handler.AbstractHandler;
import org.voltdb.CatalogContext;
import org.voltdb.HTTPClientInterface;
import org.voltdb.VoltDB;
import org.voltdb.logging.VoltLogger;

public class HTTPAdminListener {

    Server m_server = new Server();
    HTTPClientInterface httpClientInterface = new HTTPClientInterface();
    final boolean m_jsonEnabled;
    Map<String, String> m_htmlTemplates = new HashMap<String, String>();

    class RequestHandler extends AbstractHandler {

        @Override
        public void handle(String target,
                           Request baseRequest,
                           HttpServletRequest request,
                           HttpServletResponse response)
                           throws IOException, ServletException {

            // if this is an internal jetty retry, then just tell
            // jetty we're still working on it. There is a risk of
            // masking other errors in doing this, but it's probably
            // low compared with the default policy of retrys.
            AsyncContinuation cont = baseRequest.getAsyncContinuation();
            // this is set to false on internal jetty retrys
            if (!cont.isInitial()) {
                // The continuation object has been woken up by the
                // retry. Tell it to go back to sleep.
                cont.suspend();
                return;
            }

            // kick over to the HTTP/JSON interface
            if (baseRequest.getRequestURI().contains("/api/1.0/")) {
                // http://www.ietf.org/rfc/rfc4627.txt dictates this mime type
                response.setContentType("application/json;charset=utf-8");
                if (m_jsonEnabled) {
                    httpClientInterface.process(baseRequest, response);

                    // used for perf testing of the http interface
                    /*String msg = "{\"status\":1,\"appstatus\":-128,\"statusstring\":null,\"appstatusstring\":null,\"exception\":null,\"results\":[{\"status\":-128,\"schema\":[{\"name\":\"SVAL1\",\"type\":9},{\"name\":\"SVAL2\",\"type\":9},{\"name\":\"SVAL3\",\"type\":9}],\"data\":[[\"FOO\",\"BAR\",\"BOO\"]]}]}";
                    response.setStatus(HttpServletResponse.SC_OK);
                    baseRequest.setHandled(true);
                    response.getWriter().print(msg);*/
                }
                else {
                    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                    baseRequest.setHandled(true);
                    response.getWriter().println("JSON API IS CURRENTLY DISABLED");
                }
{'fraction_non_alphanumeric': 0.10454937738873135, 'fraction_numerical': 0.007520650967821477, 'mean_word_length': 2.5377235063235934, 'pattern_counts': {'":': 30, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, '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': '6738536', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2089, 'n_words': 614}