Search in sources :

Example 16 with URLConnection

use of java.net.URLConnection in project languagetool by languagetool-org.

the class HTTPTools method checkAtUrlByPost.

static String checkAtUrlByPost(URL url, String postData) throws IOException {
    String keepAlive = System.getProperty("http.keepAlive");
    try {
        // without this, there's an overhead of about 1 second - not sure why
        System.setProperty("http.keepAlive", "false");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())) {
            writer.write(postData);
            writer.flush();
            return StringTools.streamToString(connection.getInputStream(), "UTF-8");
        }
    } finally {
        if (keepAlive != null) {
            System.setProperty("http.keepAlive", keepAlive);
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection)

Example 17 with URLConnection

use of java.net.URLConnection in project pinot by linkedin.

the class AbstractBaseAdminCommand method postQuery.

public static JSONObject postQuery(String query, String brokerBaseApiUrl) throws Exception {
    final JSONObject json = new JSONObject();
    json.put("pql", query);
    final long start = System.currentTimeMillis();
    final URLConnection conn = new URL(brokerBaseApiUrl + "/query").openConnection();
    conn.setDoOutput(true);
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
    final String reqStr = json.toString();
    System.out.println("reqStr = " + reqStr);
    writer.write(reqStr, 0, reqStr.length());
    writer.flush();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    final long stop = System.currentTimeMillis();
    final String res = sb.toString();
    System.out.println("res = " + res);
    final JSONObject ret = new JSONObject(res);
    ret.put("totalTime", (stop - start));
    return ret;
}
Also used : JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URLConnection(java.net.URLConnection) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Example 18 with URLConnection

use of java.net.URLConnection in project pinot by linkedin.

the class PerfBenchmarkDriver method postQuery.

public JSONObject postQuery(String query, String optimizationFlags) throws Exception {
    JSONObject requestJson = new JSONObject();
    requestJson.put("pql", query);
    if (optimizationFlags != null && !optimizationFlags.isEmpty()) {
        requestJson.put("debugOptions", "optimizationFlags=" + optimizationFlags);
    }
    long start = System.currentTimeMillis();
    URLConnection conn = new URL(_brokerBaseApiUrl + "/query").openConnection();
    conn.setDoOutput(true);
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"))) {
        String requestString = requestJson.toString();
        writer.write(requestString);
        writer.flush();
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        }
        long totalTime = System.currentTimeMillis() - start;
        String responseString = stringBuilder.toString();
        JSONObject responseJson = new JSONObject(responseString);
        responseJson.put("totalTime", totalTime);
        if (_verbose && (responseJson.getLong("numDocsScanned") > 0)) {
            LOGGER.info("requestString: {}", requestString);
            LOGGER.info("responseString: {}", responseString);
        }
        return responseJson;
    }
}
Also used : JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URLConnection(java.net.URLConnection) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Example 19 with URLConnection

use of java.net.URLConnection in project pinot by linkedin.

the class PostQueryCommand method run.

public String run() throws Exception {
    if (_brokerHost == null) {
        _brokerHost = NetUtil.getHostAddress();
    }
    LOGGER.info("Executing command: " + toString());
    final JSONObject json = new JSONObject();
    json.put("pql", _query);
    String brokerUrl = "http://" + _brokerHost + ":" + _brokerPort;
    final URLConnection conn = new URL(brokerUrl + "/query").openConnection();
    conn.setDoOutput(true);
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
    String request = json.toString();
    writer.write(request, 0, request.length());
    writer.flush();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    return sb.toString();
}
Also used : JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URLConnection(java.net.URLConnection) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Example 20 with URLConnection

use of java.net.URLConnection in project pinot by linkedin.

the class ExternalApiQueryUtil method retrieveInformedEvents.

public InFormedPosts retrieveInformedEvents(String informedEventName, DateTime startTimeInclusive, DateTime endTimeExclusive) {
    long startMillis = startTimeInclusive.getMillis() / TIME_PRECISION_IN_MILLIS;
    long endMillis = endTimeExclusive.getMillis() / TIME_PRECISION_IN_MILLIS;
    String fetchUrl = new StringBuilder(String.format(queryPrefix, informedEventName)).append("&start=").append(startMillis).append("&end=").append(endMillis).toString();
    LOG.info("Fetching {} events between {} and {} from {}", informedEventName, startMillis, endMillis, fetchUrl);
    long start = System.currentTimeMillis();
    InFormedPosts informedPosts = null;
    try {
        URL website = new URL(fetchUrl);
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        informedPosts = OBJECT_MAPPER.readValue(response.toString(), InFormedPosts.class);
    } catch (Exception e) {
        LOG.error("Error reading from informed API", e);
    }
    LOG.info("Fetching Time: {} ms", System.currentTimeMillis() - start);
    LOG.info("Message Count: {}", informedPosts.data.size());
    if (informedPosts.status.equals("success")) {
        if (LOG.isTraceEnabled()) {
            for (InFormedPost post : informedPosts.data) {
                LOG.trace(post.toString());
            }
        }
        //        informedPosts.sortInAscendingOrder();
        return informedPosts;
    }
    return new InFormedPosts();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

URLConnection (java.net.URLConnection)1686 URL (java.net.URL)1180 IOException (java.io.IOException)740 InputStream (java.io.InputStream)569 HttpURLConnection (java.net.HttpURLConnection)465 InputStreamReader (java.io.InputStreamReader)404 BufferedReader (java.io.BufferedReader)358 Test (org.junit.Test)206 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)202 File (java.io.File)196 MalformedURLException (java.net.MalformedURLException)190 BufferedInputStream (java.io.BufferedInputStream)119 FileOutputStream (java.io.FileOutputStream)112 OutputStream (java.io.OutputStream)112 FileInputStream (java.io.FileInputStream)111 JarURLConnection (java.net.JarURLConnection)106 ArrayList (java.util.ArrayList)92 MockResponse (okhttp3.mockwebserver.MockResponse)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)74 FileNotFoundException (java.io.FileNotFoundException)59