Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: partial snapshot download requestes turned into POST #5633

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: correctly updated requests
Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>
  • Loading branch information
sfiorani committed Jan 7, 2025
commit d2550d25aea8702b0b357dfe0687d65c962513a8
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ interface SnapshotDownloadModalUiBinder extends UiBinder<Widget, SnapshotDownloa

HandlerRegistration anchorClickHandler;
HandlerRegistration downloadHandler;
HandlerRegistration xmlDownloadHandler;
HandlerRegistration jsonDownloadHandler;

Consumer<String> wiregraphDownloadConsumer;

Expand Down Expand Up @@ -207,20 +209,20 @@ private void initSnapshotScrollPanel() {

private void initSnapshotDownloadButtons(Long snapshotId) {

if (this.downloadHandler != null) {
this.downloadHandler.removeHandler();
}
cleanDownloadHandlers();

this.downloadHandler = this.snapshotDownloadForm
.addSubmitCompleteHandler(event -> this.downloadCallback.onSuccess(event));

this.downloadJson.addClickHandler(e -> onSnapshotDownloadButtonClick(snapshotId, "JSON"));
this.jsonDownloadHandler = this.downloadJson
.addClickHandler(e -> onSnapshotDownloadButtonClick(snapshotId, "JSON"));

this.downloadXml.addClickHandler(e -> onSnapshotDownloadButtonClick(snapshotId, "XML"));
this.xmlDownloadHandler = this.downloadXml
.addClickHandler(e -> onSnapshotDownloadButtonClick(snapshotId, "XML"));
}

private void initHiddenFields() {
this.snapshotDownloadForm.setEncoding(com.google.gwt.user.client.ui.FormPanel.ENCODING_MULTIPART);
this.snapshotDownloadForm.setEncoding(com.google.gwt.user.client.ui.FormPanel.ENCODING_URLENCODED);
this.snapshotDownloadForm.setMethod(com.google.gwt.user.client.ui.FormPanel.METHOD_POST);
this.snapshotDownloadForm.setAction(Console.ADMIN_ROOT + '/' + GWT.getModuleName() + "/device_snapshots");

Expand Down Expand Up @@ -344,7 +346,7 @@ private void onSelectOrRemoveAllSelection(ClickEvent handler) {
*/

private String getSelectedPids() {
StringBuilder selectedPidsBuilder = new StringBuilder();
StringBuilder selectedPidsBuilder = new StringBuilder("SelectedPids: ");
this.pidPanel.iterator().forEachRemaining(pid -> {
CheckBox checkBox = (CheckBox) pid;
if (checkBox.getValue().booleanValue()) {
Expand All @@ -358,7 +360,7 @@ private String getSelectedPids() {
private void downloadEntireSnapshot(Long snapshotId, String format) {
RequestQueue.submit(context -> this.gwtXSRFService.generateSecurityToken(context.callback(token -> {
xsrfTokenField.setValue(token.getToken());
pidsListField.setValue("");
pidsListField.setValue("EntireSnapshot");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to download the entire snapshot if no pidsList is specified?
I am kind of scared of those magic values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. Indeed, I started with the idea "EmptyValue --> Download All", but then I changed my mind to avoid empty values...but thinking about it, it's probably more secure and robust. I'll change it

snapshotDownloadFormatField.setValue(format);
snapshotIdField.setValue(snapshotId.toString());
snapshotDownloadForm.submit();
Expand Down Expand Up @@ -455,4 +457,18 @@ private void updateSelectedPidsCounter() {
private boolean isMatchingSearch(Widget widget, String searchedPid) {
return ((CheckBox) widget).getText().toLowerCase().contains(searchedPid.toLowerCase());
}

private void cleanDownloadHandlers() {
if (this.downloadHandler != null) {
this.downloadHandler.removeHandler();
}

if (this.jsonDownloadHandler != null) {
this.jsonDownloadHandler.removeHandler();
}

if (this.xmlDownloadHandler != null) {
this.xmlDownloadHandler.removeHandler();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.web.server.KuraRemoteServiceServlet;
import org.eclipse.kura.web.server.RequiredPermissions.Mode;
import org.eclipse.kura.web.server.util.GwtServerUtil;
import org.eclipse.kura.web.server.util.ServiceLocator;
import org.eclipse.kura.web.shared.GwtKuraException;
import org.eclipse.kura.web.shared.KuraPermission;
import org.eclipse.kura.web.shared.model.GwtXSRFToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DeviceSnapshotsServlet extends AuditServlet {

private static final String SNAPSHOT_DOWNLOAD_TAG = "snapshot_";

private static final long serialVersionUID = -2533869595709953567L;

private static Logger logger = LoggerFactory.getLogger(DeviceSnapshotsServlet.class);
Expand Down Expand Up @@ -66,7 +71,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro

long sid = Long.parseLong(snapshotId);

GwtServerUtil.writeSnapshot(response, cs.getSnapshot(sid), "snapshot_" + sid,
GwtServerUtil.writeSnapshot(response, cs.getSnapshot(sid), SNAPSHOT_DOWNLOAD_TAG + sid,
request.getParameter("format"));

}
Expand All @@ -92,24 +97,16 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr

try {

ServiceLocator locator = ServiceLocator.getInstance();
ConfigurationService cs = locator.getService(ConfigurationService.class);

String format = request.getParameter("downloadFormat");
Long snapshotId = Long.parseLong(request.getParameter("snapshotId"));

if (request.getParameter("pidsList").isEmpty() || request.getParameter("pidsList") == null) {
String downloadFormat = Objects.requireNonNull(request.getParameter("downloadFormat"));
Long snapshotId = Objects.requireNonNull(Long.parseLong(request.getParameter("snapshotId")));
String pidList = Objects.requireNonNull(request.getParameter("pidsList"));

GwtServerUtil.writeSnapshot(response, cs.getSnapshot(snapshotId), "snapshot_" + snapshotId,
request.getParameter("format"));
} else {
List<String> selectedPids = Arrays.asList(request.getParameter("pidsList").split(","));

List<ComponentConfiguration> configs = cs.getSnapshot(snapshotId).stream().filter(config -> {
return selectedPids.contains(config.getPid());
}).collect(Collectors.toList());
if (!pidList.isEmpty() && pidList.equals("EntireSnapshot")) {
parseEntireSnapshot(response, snapshotId, downloadFormat);
}

GwtServerUtil.writeSnapshot(response, configs, "snapshot_" + snapshotId, format);
if (!pidList.isEmpty() && pidList.startsWith("SelectedPids: ")) {
parsePartialSnapshot(response, snapshotId, downloadFormat, pidList);
}

} catch (Exception e) {
Expand All @@ -119,4 +116,28 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr

}

}
private void parseEntireSnapshot(HttpServletResponse response, Long snapshotId, String downloadFormat)
throws GwtKuraException, ServletException, KuraException {

ServiceLocator locator = ServiceLocator.getInstance();
ConfigurationService cs = locator.getService(ConfigurationService.class);

GwtServerUtil.writeSnapshot(response, cs.getSnapshot(snapshotId), SNAPSHOT_DOWNLOAD_TAG + snapshotId,
downloadFormat);
}

private void parsePartialSnapshot(HttpServletResponse response, Long snapshotId, String downloadFormat,
String pidList) throws GwtKuraException, ServletException, KuraException {

ServiceLocator locator = ServiceLocator.getInstance();
ConfigurationService cs = locator.getService(ConfigurationService.class);

List<String> selectedPids = Arrays.asList(pidList.replace("SelectedPids: ", "").split(","));

List<ComponentConfiguration> configs = cs.getSnapshot(snapshotId).stream()
.filter(config -> selectedPids.contains(config.getPid())).collect(Collectors.toList());

GwtServerUtil.writeSnapshot(response, configs, SNAPSHOT_DOWNLOAD_TAG + snapshotId, downloadFormat);
}

}
Loading