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);
}
}
}
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;
}
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;
}
}
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();
}
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();
}
Aggregations