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

Commit

Permalink
Add Download hostname override config (#2680)
Browse files Browse the repository at this point in the history
* Add Download hostname override config

* Fix Checkstyle errors

* Move to regular command line flag

* Refactor to method

* Remove static var manipulation

* Fix whitespace
  • Loading branch information
aahmed-se authored and kramasamy committed Jan 24, 2018
1 parent 90a615d commit 8874b23
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private enum Flag {
Port("port"),
Property("D"),
ReleaseFile("release-file"),
Verbose("verbose");
Verbose("verbose"),
DownloadHostName("download-hostname");

final String name;

Expand All @@ -73,7 +74,7 @@ private static Options createOptions() {
.build();

final Option baseTemplate = Option.builder()
.desc("Base configuration to use for deloying topologies")
.desc("Base configuration to use for deploying topologies")
.longOpt(Flag.BaseTemplate.name)
.hasArg()
.argName(Flag.BaseTemplate.name)
Expand Down Expand Up @@ -119,14 +120,23 @@ private static Options createOptions() {
.required(false)
.build();

final Option downloadHostName = Option.builder()
.desc("Download Hostname Override")
.longOpt(Flag.DownloadHostName.name)
.hasArg()
.argName(Flag.DownloadHostName.name)
.required(false)
.build();

return new Options()
.addOption(baseTemplate)
.addOption(cluster)
.addOption(config)
.addOption(port)
.addOption(release)
.addOption(property)
.addOption(verbose);
.addOption(verbose)
.addOption(downloadHostName);
}

private static Options constructHelpOptions() {
Expand Down Expand Up @@ -187,6 +197,13 @@ private static int getPort(CommandLine cmd) {
return Constants.DEFAULT_PORT;
}

private static String getDownloadHostName(CommandLine cmd) {
if (cmd.hasOption(Flag.DownloadHostName.name)) {
return String.valueOf(cmd.getOptionValue(Flag.DownloadHostName.name));
}
return null;
}

private static String loadOverrides(CommandLine cmd) throws IOException {
return ConfigUtils.createOverrideConfiguration(
cmd.getOptionProperties(Flag.Property.name));
Expand Down Expand Up @@ -240,6 +257,7 @@ public static void main(String[] args) throws Exception {
final String releaseFile = getReleaseFile(toolsHome, cmd);
final String configurationOverrides = loadOverrides(cmd);
final int port = getPort(cmd);
final String downloadHostName = getDownloadHostName(cmd);

final Config baseConfiguration =
ConfigUtils.getBaseConfiguration(heronDirectory,
Expand All @@ -264,6 +282,8 @@ public static void main(String[] args) throws Exception {
configurationOverrides);
contextHandler.setAttribute(HeronResource.ATTRIBUTE_PORT,
String.valueOf(port));
contextHandler.setAttribute(HeronResource.ATTRIBUTE_DOWNLOAD_HOSTNAME,
String.valueOf(downloadHostName));

server.setHandler(contextHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.eclipse.jetty.util.StringUtil;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.slf4j.Logger;
Expand All @@ -52,6 +53,9 @@ public class FileResource extends HeronResource {
private static final String FILE_SYSTEM_DIRECTORY
= "heron.apiserver.http.file.system.directory";

private static final String DOWNLOAD_HOSTNAME_OVERRIDE
= "heron.apiserver.http.download.hostname";

private static InetAddress ip;
private static String hostname;

Expand All @@ -60,10 +64,13 @@ public class FileResource extends HeronResource {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
} catch (UnknownHostException e) {
LOG.info("Failed or resolve IP address of localhost");
LOG.info("Failed to resolve IP address of localhost");
}
}

/**
* Endpoints for artifacts upload
*/
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Expand All @@ -90,9 +97,9 @@ public Response uploadFile(

String uploadDir = config.getStringValue(FILE_SYSTEM_DIRECTORY);

String fileName = UUID.randomUUID() + "-" + fileDetail.getFileName();
final String fileName = UUID.randomUUID() + "-" + fileDetail.getFileName();

String uploadedFileLocation
final String uploadedFileLocation
= uploadDir + "/" + fileName;

// save it
Expand All @@ -107,11 +114,14 @@ public Response uploadFile(
}

String uri = String.format("http://%s:%s/api/v1/file/download/%s",
(hostname != null) ? hostname : ip, getPort(), fileName);
getHostNameOrIP(), getPort(), fileName);

return Response.status(Response.Status.OK).entity(uri).build();
}

/**
* Endpoints for artifacts download
*/
@GET
@Path("/download/{file}")
public Response downloadPdfFile(final @PathParam("file") String file) {
Expand Down Expand Up @@ -143,4 +153,12 @@ private Config createConfig() {
return Config.toLocalMode(builder.build());
}

private String getHostNameOrIP() {
// Override hostname if provided in flags
if (StringUtil.isNotBlank(getDownloadHostName())) {
return getDownloadHostName();
}
return (hostname != null) ? hostname : ((ip != null) ? ip.toString() : "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class HeronResource {
public static final String ATTRIBUTE_CONFIGURATION_DIRECTORY = "configuration_directory";
public static final String ATTRIBUTE_CONFIGURATION_OVERRIDE_PATH = "configuration_override";
public static final String ATTRIBUTE_PORT = "port";
public static final String ATTRIBUTE_DOWNLOAD_HOSTNAME = "download_hostname";

@Context
protected ServletContext servletContext;
Expand All @@ -34,6 +35,7 @@ public class HeronResource {
private String configurationOverridePath;
private String cluster;
private String port;
private String downloadHostname;

Config getBaseConfiguration() {
if (baseConfiguration == null) {
Expand Down Expand Up @@ -74,4 +76,13 @@ String getPort() {

return port;
}

String getDownloadHostName() {
if (downloadHostname == null) {
downloadHostname = (String) servletContext.getAttribute(ATTRIBUTE_DOWNLOAD_HOSTNAME);
}

return downloadHostname;
}

}

0 comments on commit 8874b23

Please sign in to comment.