Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit ada6052

Browse files
authored
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
1 parent 2f26b49 commit ada6052

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

heron/tools/apiserver/src/java/com/twitter/heron/apiserver/Runtime.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.twitter.heron.apiserver.resources.HeronResource;
3838
import com.twitter.heron.apiserver.utils.ConfigUtils;
3939
import com.twitter.heron.apiserver.utils.Logging;
40+
import com.twitter.heron.apiserver.utils.Utils;
4041
import com.twitter.heron.spi.common.Config;
4142
import com.twitter.heron.spi.common.Key;
4243

@@ -283,8 +284,6 @@ public static void main(String[] args) throws Exception {
283284
releaseFile,
284285
configurationOverrides);
285286

286-
LOG.info("heronCorePackagePath: " + heronCorePackagePath);
287-
288287
final ResourceConfig config = new ResourceConfig(Resources.get());
289288
final Server server = new Server(port);
290289

@@ -303,9 +302,11 @@ public static void main(String[] args) throws Exception {
303302
contextHandler.setAttribute(HeronResource.ATTRIBUTE_PORT,
304303
String.valueOf(port));
305304
contextHandler.setAttribute(HeronResource.ATTRIBUTE_DOWNLOAD_HOSTNAME,
306-
String.valueOf(downloadHostName));
305+
Utils.isNotEmpty(downloadHostName)
306+
? String.valueOf(downloadHostName) : null);
307307
contextHandler.setAttribute(HeronResource.ATTRIBUTE_HERON_CORE_PACKAGE_PATH,
308-
String.valueOf(heronCorePackagePath));
308+
Utils.isNotEmpty(heronCorePackagePath)
309+
? String.valueOf(heronCorePackagePath) : null);
309310

310311
server.setHandler(contextHandler);
311312

heron/tools/apiserver/src/java/com/twitter/heron/apiserver/resources/FileResource.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import javax.ws.rs.core.MediaType;
3030
import javax.ws.rs.core.Response;
3131

32-
import org.eclipse.jetty.util.StringUtil;
3332
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
3433
import org.glassfish.jersey.media.multipart.FormDataParam;
3534
import org.slf4j.Logger;
@@ -126,16 +125,7 @@ public Response downloadFile(final @PathParam("file") String file) {
126125
Config config = createConfig();
127126
String uploadDir = config.getStringValue(FILE_SYSTEM_DIRECTORY);
128127
String filePath = uploadDir + "/" + file;
129-
if (!new File(filePath).exists()) {
130-
LOG.debug("Download request file " + file + " doesn't exist at " + uploadDir);
131-
return Response.status(Response.Status.NOT_FOUND).build();
132-
}
133-
134-
String mimeType = new MimetypesFileTypeMap().getContentType(file);
135-
Response.ResponseBuilder rb = Response.ok(file, mimeType);
136-
rb.header("content-disposition", "attachment; filename = "
137-
+ file);
138-
return rb.build();
128+
return getResponseByFile(filePath);
139129
}
140130

141131
/**
@@ -145,7 +135,12 @@ public Response downloadFile(final @PathParam("file") String file) {
145135
@Path("/download/core")
146136
public Response downloadHeronCore() {
147137
String corePath = getHeronCorePackagePath();
148-
File file = new File(corePath);
138+
return getResponseByFile(corePath);
139+
}
140+
141+
private Response getResponseByFile(String filePath) {
142+
143+
File file = new File(filePath);
149144
if (!file.exists()) {
150145
return Response.status(Response.Status.NOT_FOUND).build();
151146
}
@@ -154,6 +149,7 @@ public Response downloadHeronCore() {
154149
rb.header("content-disposition", "attachment; filename = "
155150
+ file.getName());
156151
return rb.build();
152+
157153
}
158154

159155
private Config createConfig() {
@@ -164,11 +160,11 @@ private Config createConfig() {
164160

165161
private String getHostNameOrIP() {
166162
// Override hostname if provided in flags
167-
if (StringUtil.isNotBlank(getDownloadHostName())) {
163+
if (Utils.isNotEmpty(getDownloadHostName())) {
168164
return getDownloadHostName();
169-
} else if (StringUtil.isNotBlank(hostname)) {
165+
} else if (Utils.isNotEmpty(hostname)) {
170166
return hostname;
171-
} else if (ip != null && StringUtil.isNotBlank(ip.toString())) {
167+
} else if (ip != null && !ip.toString().isEmpty()) {
172168
return ip.toString();
173169
}
174170
return "";

heron/tools/apiserver/src/java/com/twitter/heron/apiserver/utils/Utils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public static String createValidationError(String message, List<String> missing)
4444

4545
return node.toString();
4646
}
47+
48+
public static boolean isNotEmpty(String str) {
49+
return str != null && !str.isEmpty();
50+
51+
}
4752
}

0 commit comments

Comments
 (0)