fixing bugs in the API server when downloading files (#2749)

* fixing bugs in the API server when downloading files

* addressing comments

* cleaning up

* refactoring some code
diff --git a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/Runtime.java b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/Runtime.java
index 84a39a1..b369749 100644
--- a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/Runtime.java
+++ b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/Runtime.java
@@ -37,6 +37,7 @@
 import com.twitter.heron.apiserver.resources.HeronResource;
 import com.twitter.heron.apiserver.utils.ConfigUtils;
 import com.twitter.heron.apiserver.utils.Logging;
+import com.twitter.heron.apiserver.utils.Utils;
 import com.twitter.heron.spi.common.Config;
 import com.twitter.heron.spi.common.Key;
 
@@ -283,8 +284,6 @@
             releaseFile,
             configurationOverrides);
 
-    LOG.info("heronCorePackagePath: " + heronCorePackagePath);
-
     final ResourceConfig config = new ResourceConfig(Resources.get());
     final Server server = new Server(port);
 
@@ -303,9 +302,11 @@
     contextHandler.setAttribute(HeronResource.ATTRIBUTE_PORT,
         String.valueOf(port));
     contextHandler.setAttribute(HeronResource.ATTRIBUTE_DOWNLOAD_HOSTNAME,
-        String.valueOf(downloadHostName));
+        Utils.isNotEmpty(downloadHostName)
+            ? String.valueOf(downloadHostName) : null);
     contextHandler.setAttribute(HeronResource.ATTRIBUTE_HERON_CORE_PACKAGE_PATH,
-        String.valueOf(heronCorePackagePath));
+        Utils.isNotEmpty(heronCorePackagePath)
+            ? String.valueOf(heronCorePackagePath) : null);
 
     server.setHandler(contextHandler);
 
diff --git a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/resources/FileResource.java b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/resources/FileResource.java
index 79e32fd..e635d86 100644
--- a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/resources/FileResource.java
+++ b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/resources/FileResource.java
@@ -29,7 +29,6 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
-import org.eclipse.jetty.util.StringUtil;
 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
 import org.glassfish.jersey.media.multipart.FormDataParam;
 import org.slf4j.Logger;
@@ -126,16 +125,7 @@
     Config config = createConfig();
     String uploadDir = config.getStringValue(FILE_SYSTEM_DIRECTORY);
     String filePath = uploadDir + "/" + file;
-    if (!new File(filePath).exists()) {
-      LOG.debug("Download request file " + file + " doesn't exist at " + uploadDir);
-      return Response.status(Response.Status.NOT_FOUND).build();
-    }
-
-    String mimeType = new MimetypesFileTypeMap().getContentType(file);
-    Response.ResponseBuilder rb = Response.ok(file, mimeType);
-    rb.header("content-disposition", "attachment; filename = "
-        + file);
-    return rb.build();
+    return getResponseByFile(filePath);
   }
 
   /**
@@ -145,7 +135,12 @@
   @Path("/download/core")
   public Response downloadHeronCore() {
     String corePath = getHeronCorePackagePath();
-    File file = new File(corePath);
+    return getResponseByFile(corePath);
+  }
+
+  private Response getResponseByFile(String filePath) {
+
+    File file = new File(filePath);
     if (!file.exists()) {
       return Response.status(Response.Status.NOT_FOUND).build();
     }
@@ -154,6 +149,7 @@
     rb.header("content-disposition", "attachment; filename = "
         + file.getName());
     return rb.build();
+
   }
 
   private Config createConfig() {
@@ -164,11 +160,11 @@
 
   private String getHostNameOrIP() {
     // Override hostname if provided in flags
-    if (StringUtil.isNotBlank(getDownloadHostName())) {
+    if (Utils.isNotEmpty(getDownloadHostName())) {
       return getDownloadHostName();
-    } else if (StringUtil.isNotBlank(hostname)) {
+    } else if (Utils.isNotEmpty(hostname)) {
       return hostname;
-    } else if (ip != null && StringUtil.isNotBlank(ip.toString())) {
+    } else if (ip != null && !ip.toString().isEmpty()) {
       return ip.toString();
     }
     return "";
diff --git a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/utils/Utils.java b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/utils/Utils.java
index 906dd4a..a18d758 100644
--- a/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/utils/Utils.java
+++ b/heron/tools/apiserver/src/java/com/twitter/heron/apiserver/utils/Utils.java
@@ -44,4 +44,9 @@
 
     return node.toString();
   }
+
+  public static boolean isNotEmpty(String str) {
+    return str != null && !str.isEmpty();
+
+  }
 }