Skip to content

Commit

Permalink
The launch page will now redirect to S3 if the installer file isn't
Browse files Browse the repository at this point in the history
present.
  • Loading branch information
narupley committed Dec 19, 2018
1 parent 344adf6 commit cbfa04b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 43 deletions.
95 changes: 53 additions & 42 deletions server/src/com/mirth/connect/server/MirthWebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,18 +701,22 @@ public String getBlobType() {
private void addLauncherInstallerContextHandlers(String contextPath) {
File installersDirectory = new File(ControllerFactory.getFactory().createConfigurationController().getBaseDir() + File.separator + "public_html" + File.separator + "installers");

Collection<File> installerFiles;
if (installersDirectory.exists() && installersDirectory.isDirectory()) {
MimeTypes mimeTypes = new MimeTypes();
mimeTypes.addMimeMapping("dmg", "application/x-apple-diskimage");
mimeTypes.addMimeMapping("sh", "text/x-sh");
mimeTypes.addMimeMapping("exe", "application/octet-stream");

Collection<File> installerFiles = FileUtils.listFiles(installersDirectory, TrueFileFilter.TRUE, FalseFileFilter.FALSE);
addLauncherInstallerContextHandler(contextPath, "macos", "macos", ".dmg", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "linux", "unix", ".sh", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "windows", "windows", ".exe", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "windows-x64", "windows-x64", ".exe", installerFiles, mimeTypes);
installerFiles = FileUtils.listFiles(installersDirectory, TrueFileFilter.TRUE, FalseFileFilter.FALSE);
} else {
installerFiles = new ArrayList<File>();
}

MimeTypes mimeTypes = new MimeTypes();
mimeTypes.addMimeMapping("dmg", "application/x-apple-diskimage");
mimeTypes.addMimeMapping("sh", "text/x-sh");
mimeTypes.addMimeMapping("exe", "application/octet-stream");

addLauncherInstallerContextHandler(contextPath, "macos", "macos", ".dmg", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "linux", "unix", ".sh", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "windows", "windows", ".exe", installerFiles, mimeTypes);
addLauncherInstallerContextHandler(contextPath, "windows-x64", "windows-x64", ".exe", installerFiles, mimeTypes);
}

private void addLauncherInstallerContextHandler(String contextPath, String os, String fileSuffix, String fileExt, Collection<File> installers, MimeTypes mimeTypes) {
Expand All @@ -724,13 +728,11 @@ private void addLauncherInstallerContextHandler(String contextPath, String os, S
}
}

if (installerFile != null) {
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath(contextPath + "/launcher/" + os + fileExt);
contextHandler.setAllowNullPathInfo(true);
contextHandler.setHandler(new InstallerFileHandler(installerFile, mimeTypes));
handlers.addHandler(contextHandler);
}
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath(contextPath + "/launcher/" + os + fileExt);
contextHandler.setAllowNullPathInfo(true);
contextHandler.setHandler(new InstallerFileHandler(installerFile, mimeTypes));
handlers.addHandler(contextHandler);
}

private class InstallerFileHandler extends AbstractHandler {
Expand All @@ -741,47 +743,56 @@ private class InstallerFileHandler extends AbstractHandler {
public InstallerFileHandler(File file, MimeTypes mimeTypes) {
this.file = file;

contentType = mimeTypes.getMimeByExtension(file.getName());
if (file != null) {
contentType = mimeTypes.getMimeByExtension(file.getName());
}
if (StringUtils.isBlank(contentType)) {
contentType = ContentType.APPLICATION_OCTET_STREAM.getMimeType();
}
}

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Only allow GET requests, otherwise pass to the next request handler
if (!baseRequest.getMethod().equalsIgnoreCase(HttpMethod.GET.asString())) {
// Only allow GET/HEAD requests, otherwise pass to the next request handler
if (!baseRequest.getMethod().equalsIgnoreCase(HttpMethod.GET.asString()) && !baseRequest.getMethod().equalsIgnoreCase(HttpMethod.HEAD.asString())) {
return;
}

FileInputStream fis = null;
try {
if (file != null && file.exists()) {
response.setStatus(HttpStatus.SC_OK);
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());

OutputStream responseOutputStream = response.getOutputStream();
if (baseRequest.getMethod().equalsIgnoreCase(HttpMethod.GET.asString())) {
FileInputStream fis = null;
try {
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());

// If the client accepts GZIP compression, compress the content
for (Enumeration<String> en = request.getHeaders("Accept-Encoding"); en.hasMoreElements();) {
if (StringUtils.contains(en.nextElement(), "gzip")) {
response.setHeader(HTTP.CONTENT_ENCODING, "gzip");
responseOutputStream = new GZIPOutputStream(responseOutputStream);
break;
}
}
OutputStream responseOutputStream = response.getOutputStream();

// If the client accepts GZIP compression, compress the content
for (Enumeration<String> en = request.getHeaders("Accept-Encoding"); en.hasMoreElements();) {
if (StringUtils.contains(en.nextElement(), "gzip")) {
response.setHeader(HTTP.CONTENT_ENCODING, "gzip");
responseOutputStream = new GZIPOutputStream(responseOutputStream);
break;
}
}

fis = new FileInputStream(file);
IOUtils.copy(fis, responseOutputStream);
fis = new FileInputStream(file);
IOUtils.copy(fis, responseOutputStream);

// If we gzipped, we need to finish the stream now
if (responseOutputStream instanceof GZIPOutputStream) {
((GZIPOutputStream) responseOutputStream).finish();
// If we gzipped, we need to finish the stream now
if (responseOutputStream instanceof GZIPOutputStream) {
((GZIPOutputStream) responseOutputStream).finish();
}
} catch (Throwable t) {
IOUtils.closeQuietly(fis);
response.reset();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
} catch (Throwable t) {
IOUtils.closeQuietly(fis);
response.reset();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} else {
response.setStatus(HttpStatus.SC_NOT_FOUND);
}

baseRequest.setHandled(true);
Expand Down
12 changes: 11 additions & 1 deletion webadmin/WebContent/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,17 @@
</script>
<script type="text/javascript">
function downloadAdministratorLauncher(){
window.location.href = '${actionBean.context.currentScheme}://' + window.location.hostname + ':${actionBean.context.currentPort}${actionBean.context.contextPath}/launcher/' + $('#operatingSystemSelect').val();
var url = '${actionBean.context.currentScheme}://' + window.location.hostname + ':${actionBean.context.currentPort}${actionBean.context.contextPath}/launcher/' + $('#operatingSystemSelect').val();
$.ajax({url: url, type: 'HEAD', success: function() {
window.location.href = url;
}, error: function() {
var suffix = $('#operatingSystemSelect').val();
if (suffix == 'linux.sh') {
suffix = 'unix.sh';
}
window.location.href = 'https://s3.amazonaws.com/downloads.mirthcorp.com/connect-client-launcher/mirth-administrator-launcher-1.0.0-' + suffix;
}});
}
function launchAdministrator(){
Expand Down

0 comments on commit cbfa04b

Please sign in to comment.