Skip to content

Commit

Permalink
Merge pull request #3745 from marcellorinaldo/add-cog-during-log-down…
Browse files Browse the repository at this point in the history
…load

Added waiting modal when downloading logs using cookies
  • Loading branch information
nicolatimeus committed Jan 19, 2022
2 parents bd9a1a0 + 05758b8 commit 2b9c4a8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2019, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,6 +17,8 @@

import org.eclipse.kura.web.Console;
import org.eclipse.kura.web.client.messages.Messages;
import org.eclipse.kura.web.client.ui.EntryClassUi;
import org.eclipse.kura.web.client.util.DownloadHelper;
import org.eclipse.kura.web.client.util.FailureHandler;
import org.eclipse.kura.web.client.util.LogPollService;
import org.eclipse.kura.web.shared.model.GwtLogEntry;
Expand All @@ -32,10 +34,14 @@
import org.gwtbootstrap3.client.ui.ListBox;
import org.gwtbootstrap3.client.ui.Panel;
import org.gwtbootstrap3.client.ui.Row;
import org.gwtbootstrap3.client.ui.Form;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
Expand All @@ -62,7 +68,7 @@ interface LogTabUiUiBinder extends UiBinder<Widget, LogTabUi> {
@UiField
Button execute;
@UiField
FormPanel logForm;
Form logForm;
@UiField
FormLabel logLabel;
@UiField
Expand All @@ -85,6 +91,41 @@ interface LogTabUiUiBinder extends UiBinder<Widget, LogTabUi> {
private boolean hasLogProvider = false;
private boolean autoFollow = true;

private final String nonce = Integer.toString(Random.nextInt());

private static final int DOWNLOAD_COMPLETE_WAIT_TIMEOUT = 5000;
private Timer waitDownloadCompleted = new Timer() {

// safety parameter, 9 = 45secs
private short retryLimit = 9;
private String cookieName;

@Override
public void run() {
cookieName = "LogsDownload-" + LogTabUi.this.nonce;

if (Cookies.getCookie(cookieName) != null) {
hideModalAndStop();
}

// eventually regain access to the UI
if (this.retryLimit <= 0) {
hideModalAndStop();
}

this.retryLimit--;
this.schedule(DOWNLOAD_COMPLETE_WAIT_TIMEOUT);
}

private void hideModalAndStop() {
EntryClassUi.hideWaitModal();
Cookies.removeCookie(cookieName, "/");
this.retryLimit = 9;
this.cancel();
}

};

public LogTabUi() {
initWidget(uiBinder.createAndBindUi(this));

Expand All @@ -101,7 +142,12 @@ public void onFailure(Throwable ex) {

@Override
public void onSuccess(GwtXSRFToken token) {
LogTabUi.this.logForm.submit();
final StringBuilder sbUrl = new StringBuilder();
sbUrl.append("/log?nonce=").append(LogTabUi.this.nonce);
DownloadHelper.instance().startDownload(token, sbUrl.toString());

EntryClassUi.showWaitModal();
LogTabUi.this.waitDownloadCompleted.schedule(DOWNLOAD_COMPLETE_WAIT_TIMEOUT);
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
</b:Row>

<b:Row addStyleNames="panel-footer">
<gwt:FormPanel ui:field="logForm">
<b:Form ui:field="logForm">
<b:FieldSet>
<b:ButtonGroup pull="LEFT">
<b:Button ui:field="execute" addStyleNames="fa fa-download">Download</b:Button>
<b:FormLabel ui:field="logLabel" addStyleNames="{style.download-label-text}" />
</b:ButtonGroup>
</b:FieldSet>
</gwt:FormPanel>
</b:Form>
</b:Row>

</b:Column>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2019, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -30,19 +30,25 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.kura.executor.Command;
import org.eclipse.kura.executor.CommandStatus;
import org.eclipse.kura.executor.PrivilegedExecutorService;
import org.eclipse.kura.system.SystemService;
import org.eclipse.kura.web.server.KuraRemoteServiceServlet;
import org.eclipse.kura.web.server.util.ServiceLocator;
import org.eclipse.kura.web.shared.GwtKuraException;
import org.eclipse.kura.web.shared.model.GwtXSRFToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gwt.user.client.Cookies;

public class LogServlet extends AuditServlet {

private static final long serialVersionUID = 3969980124054250070L;
Expand All @@ -57,7 +63,16 @@ public LogServlet() {
}

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException {
// BEGIN XSRF - Servlet dependent code
try {
GwtXSRFToken token = new GwtXSRFToken(httpServletRequest.getParameter("xsrfToken"));
KuraRemoteServiceServlet.checkXSRFToken(httpServletRequest, token);
} catch (Exception e) {
throw new ServletException("Security error: please retry this operation correctly.", e);
}
// END XSRF security check

SystemService ss = null;
ServiceLocator locator = ServiceLocator.getInstance();
Expand Down Expand Up @@ -109,14 +124,19 @@ protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse
logger.warn("Error producing: {}", SYSTEM_JOURNAL_LOG_FILE);
}

createReply(httpServletResponse, fileList);
String nonce = httpServletRequest.getParameter("nonce");
createReply(httpServletResponse, fileList, nonce);
removeTmpFiles();
}

private void createReply(HttpServletResponse httpServletResponse, List<File> fileList) {
private void createReply(HttpServletResponse httpServletResponse, List<File> fileList, String nonce) {
try {
byte[] zip = zipFiles(fileList);
ServletOutputStream sos = httpServletResponse.getOutputStream();

Cookie downloadedCookie = new Cookie("LogsDownload-" + nonce, "finished");
downloadedCookie.setPath("/");
httpServletResponse.addCookie(downloadedCookie);
httpServletResponse.setContentType("application/zip");
httpServletResponse.setHeader("Content-Disposition", "attachment; filename=\"Kura_Logs.zip\"");

Expand Down

0 comments on commit 2b9c4a8

Please sign in to comment.