Skip to content

Commit

Permalink
Fixes for forms downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jun 15, 2024
1 parent a19d2ea commit fbed54e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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(*)();
}
}
}
}-*/;
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
});
});
}
}

0 comments on commit fbed54e

Please sign in to comment.