From fbed54ea3c9673b8c85a1f94d661d6bf102d20d7 Mon Sep 17 00:00:00 2001 From: Matthew Horridge Date: Fri, 14 Jun 2024 20:14:05 -0700 Subject: [PATCH] Fixes for forms downloader --- .../download/FetchAndOpenInBrowserWindow.java | 41 +++++++++++++++++++ ...tchAndOpenInBrowserWindowErrorHandler.java | 11 +++++ .../web/client/form/FormsDownloader.java | 25 ++++++++--- 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindow.java create mode 100644 webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindowErrorHandler.java diff --git a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindow.java b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindow.java new file mode 100644 index 0000000000..e3771d5f7c --- /dev/null +++ b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindow.java @@ -0,0 +1,41 @@ +package edu.stanford.bmir.protege.web.client.download; + +import javax.annotation.Nonnull; + +/** + * Matthew Horridge + * Stanford Center for Biomedical Informatics Research + * 2024-06-14 + */ +public class FetchAndOpenInBrowserWindow { + + /** + * GET the specified path and open the result as a blob in a new window + * @param path The path + * @param token The access token to use. This will be set in the authorization header. + */ + public static native void fetchUrlAndOpenInWindow(@Nonnull String path, + @Nonnull String token, + @Nonnull FetchAndOpenInBrowserWindowErrorHandler errorHandler)/*-{ + var xhr = new XMLHttpRequest(); + + xhr.open('GET', path); + xhr.onreadystatechange = handler; + xhr.responseType = 'blob'; + xhr.setRequestHeader('Authorization', 'Bearer ' + token); + xhr.send(); + + function handler() { + if (this.readyState === this.DONE) { + if (this.status === 200) { + // this.response is a Blob, because we set responseType above + var data_url = URL.createObjectURL(this.response); + $wnd.open(data_url, '_blank'); + } else { + console.error('Error'); + errorHandler.@edu.stanford.bmir.protege.web.client.download.FetchAndOpenInBrowserWindowErrorHandler::handleError(*)(); + } + } + } + }-*/; +} diff --git a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindowErrorHandler.java b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindowErrorHandler.java new file mode 100644 index 0000000000..55a8087e91 --- /dev/null +++ b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/download/FetchAndOpenInBrowserWindowErrorHandler.java @@ -0,0 +1,11 @@ +package edu.stanford.bmir.protege.web.client.download; + +/** + * Matthew Horridge + * Stanford Center for Biomedical Informatics Research + * 2024-06-14 + */ +public interface FetchAndOpenInBrowserWindowErrorHandler { + + void handleError(); +} diff --git a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/form/FormsDownloader.java b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/form/FormsDownloader.java index 7bee6f7fc0..13e092fb53 100644 --- a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/form/FormsDownloader.java +++ b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/form/FormsDownloader.java @@ -1,7 +1,9 @@ package edu.stanford.bmir.protege.web.client.form; import com.google.gwt.core.client.GWT; -import com.google.gwt.user.client.Window; +import edu.stanford.bmir.protege.web.client.dispatch.*; +import edu.stanford.bmir.protege.web.client.download.FetchAndOpenInBrowserWindow; +import edu.stanford.bmir.protege.web.shared.dispatch.actions.GetUserInfoAction; import edu.stanford.bmir.protege.web.shared.project.ProjectId; import javax.annotation.Nonnull; @@ -19,14 +21,27 @@ public class FormsDownloader { @Nonnull private final ProjectId projectId; + private final DispatchServiceManager dispatch; + + private final MessageBoxErrorDisplay errorDisplay; + @Inject - public FormsDownloader(@Nonnull ProjectId projectId) { + public FormsDownloader(@Nonnull ProjectId projectId, DispatchServiceManager dispatch, MessageBoxErrorDisplay errorDisplay) { this.projectId = checkNotNull(projectId); + this.dispatch = checkNotNull(dispatch); + this.errorDisplay = checkNotNull(errorDisplay); } public void download() { - String baseURL = GWT.getHostPageBaseURL() + "data/"; - String downloadURL = baseURL + "projects/" + projectId.getId() + "/forms"; - Window.open(downloadURL, "Download forms", ""); + dispatch.execute(new GetUserInfoAction(), result -> { + String baseURL = GWT.getHostPageBaseURL() + "data/"; + String token = result.getToken(); + FetchAndOpenInBrowserWindow.fetchUrlAndOpenInWindow("/data/projects/" + projectId.getId() + "/forms", + token, + () -> { + errorDisplay.displayGeneralErrorMessage("Download error", + "Could not download project forms"); + }); + }); } }