You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2016/12/07 00:33:25 UTC

calcite git commit: [CALCITE-1532] In HttpUtils, don't log HTTP requests; they may contain user name, password

Repository: calcite
Updated Branches:
  refs/heads/master 1ca155249 -> c397f37bd


[CALCITE-1532] In HttpUtils, don't log HTTP requests; they may contain user name, password

Also, refactor some code to use try-with-resources.


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/c397f37b
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/c397f37b
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/c397f37b

Branch: refs/heads/master
Commit: c397f37bda7bbb6f323aa43dd44f0a3733c2d8b6
Parents: 1ca1552
Author: Julian Hyde <jh...@apache.org>
Authored: Tue Dec 6 10:20:07 2016 -0800
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Dec 6 10:20:07 2016 -0800

----------------------------------------------------------------------
 .../org/apache/calcite/runtime/HttpUtils.java   | 63 +++++++-------------
 .../splunk/search/SplunkConnectionImpl.java     | 14 ++---
 2 files changed, 24 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/c397f37b/core/src/main/java/org/apache/calcite/runtime/HttpUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/runtime/HttpUtils.java b/core/src/main/java/org/apache/calcite/runtime/HttpUtils.java
index 4519281..50deeef 100644
--- a/core/src/main/java/org/apache/calcite/runtime/HttpUtils.java
+++ b/core/src/main/java/org/apache/calcite/runtime/HttpUtils.java
@@ -16,14 +16,11 @@
  */
 package org.apache.calcite.runtime;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -39,8 +36,6 @@ import javax.net.ssl.SSLSession;
 public class HttpUtils {
   private HttpUtils() {}
 
-  private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);
-
   public static HttpURLConnection getURLConnection(String url)
       throws IOException {
     URLConnection conn = new URL(url).openConnection();
@@ -105,14 +100,6 @@ public class HttpUtils {
     }
   }
 
-  public static void close(Closeable c) {
-    try {
-      c.close();
-    } catch (Exception ignore) {
-      // ignore
-    }
-  }
-
   public static InputStream post(
       String url,
       CharSequence data,
@@ -133,36 +120,26 @@ public class HttpUtils {
   public static InputStream executeMethod(
       String method, String url,
       CharSequence data, Map<String, String> headers,
-      int ctimeout, int rtimeout) throws IOException {
-    HttpURLConnection conn;
-    OutputStreamWriter wr = null;
-
-    try {
-      conn = getURLConnection(url);
-      conn.setRequestMethod(method);
-      conn.setReadTimeout(rtimeout);
-      conn.setConnectTimeout(ctimeout);
-
-      if (headers != null) {
-        for (Map.Entry<String, String> me : headers.entrySet()) {
-          conn.setRequestProperty(me.getKey(), me.getValue());
-        }
-      }
-      if (data != null) {
-        conn.setDoOutput(true);
-        wr = new OutputStreamWriter(conn.getOutputStream());
-        wr.write(data.toString());
-        wr.flush(); // Get the response
+      int cTimeout, int rTimeout) throws IOException {
+    // NOTE: do not log "data" or "url"; may contain user name or password.
+    final HttpURLConnection conn = getURLConnection(url);
+    conn.setRequestMethod(method);
+    conn.setReadTimeout(rTimeout);
+    conn.setConnectTimeout(cTimeout);
+
+    if (headers != null) {
+      for (Map.Entry<String, String> me : headers.entrySet()) {
+        conn.setRequestProperty(me.getKey(), me.getValue());
       }
-      InputStream in = conn.getInputStream();
-      if (wr != null) {
-        wr.close();
-      }
-
-      LOGGER.debug("url: {}, data: {}", url, String.valueOf(data));
-      return in;
-    } finally {
-      close(wr);
+    }
+    if (data == null) {
+      return conn.getInputStream();
+    }
+    conn.setDoOutput(true);
+    try (Writer w = new OutputStreamWriter(conn.getOutputStream())) {
+      w.write(data.toString());
+      w.flush(); // Get the response
+      return conn.getInputStream();
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/calcite/blob/c397f37b/splunk/src/main/java/org/apache/calcite/adapter/splunk/search/SplunkConnectionImpl.java
----------------------------------------------------------------------
diff --git a/splunk/src/main/java/org/apache/calcite/adapter/splunk/search/SplunkConnectionImpl.java b/splunk/src/main/java/org/apache/calcite/adapter/splunk/search/SplunkConnectionImpl.java
index b6297fa..aeb4a91 100644
--- a/splunk/src/main/java/org/apache/calcite/adapter/splunk/search/SplunkConnectionImpl.java
+++ b/splunk/src/main/java/org/apache/calcite/adapter/splunk/search/SplunkConnectionImpl.java
@@ -19,7 +19,6 @@ package org.apache.calcite.adapter.splunk.search;
 import org.apache.calcite.adapter.splunk.util.StringUtils;
 import org.apache.calcite.linq4j.Enumerator;
 import org.apache.calcite.linq4j.Linq4j;
-import org.apache.calcite.runtime.HttpUtils;
 
 import au.com.bytecode.opencsv.CSVReader;
 
@@ -182,19 +181,16 @@ public class SplunkConnectionImpl implements SplunkConnection {
     }
   }
 
-  private static void parseResults(InputStream in, SearchResultListener srl)
-      throws IOException {
-    CSVReader csvr = new CSVReader(new InputStreamReader(in));
-    try {
-      String [] header = csvr.readNext();
-
+  private static void parseResults(InputStream in, SearchResultListener srl) {
+    try (CSVReader r = new CSVReader(new InputStreamReader(in))) {
+      String[] header = r.readNext();
       if (header != null
           && header.length > 0
           && !(header.length == 1 && header[0].isEmpty())) {
         srl.setFieldNames(header);
 
         String[] line;
-        while ((line = csvr.readNext()) != null) {
+        while ((line = r.readNext()) != null) {
           if (line.length == header.length) {
             srl.processSearchResult(line);
           }
@@ -204,8 +200,6 @@ public class SplunkConnectionImpl implements SplunkConnection {
       StringWriter sw = new StringWriter();
       ignore.printStackTrace(new PrintWriter(sw));
       LOGGER.warn("{}\n{}", ignore.getMessage(), sw);
-    } finally {
-      HttpUtils.close(csvr); // CSVReader closes the input stream too
     }
   }