Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

3.2 KB
Raw Blame
package opensuite.ui;

import opensuite.core.SuiteContext;

import java.awt.Component;
import java.awt.Window;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;

/**
 * A change that needs the application started again: the text size, the scale, the theme.
 *
 * <p>The restart is never a surprise. It is asked for first, in the window the change was made
 * in, and the question says what it costs: a project in a file is saved and opened again, a
 * temporary one is lost, a running scan is stopped. Refused, the setting stays as it was.
 */
public final class Restart {
    private Restart() {}

    /** What the restart means for the work that is open, as the sentences of the question. */
    static String cost(String change, boolean temporary, boolean hasContents, boolean scanning) {
        StringBuilder b = new StringBuilder("Open Suite restarts to apply ").append(change).append(".");
        if (!temporary) b.append(" The project is saved first and opened again.");
        else if (hasContents) b.append(" This project is temporary and not saved to a file, so its contents will be lost.");
        else b.append(" It comes back with a new temporary project.");
        if (scanning) b.append(" A scan is running and will be stopped.");
        return b.toString();
    }

    /** The arguments that start the application with {@code project} open: the flags it was started with, and that file. */
    static String[] arguments(String[] launched, File project) {
        List<String> out = new ArrayList<>();
        if (launched != null)
            for (String a : launched) if (a != null && !StateIO.isProjectName(a)) out.add(a);
        if (project != null) out.add(project.getAbsolutePath());
        return out.toArray(new String[0]);
    }

    /** The arguments for a restart of the application as it stands. */
    public static String[] arguments(SuiteContext ctx) {
        return arguments(Main.launchArgs, ctx.projectFile());
    }

    /**
     * Ask whether to restart for {@code change} ("the new text size"). Agreed to, the project is
     * saved and {@code apply} runs, which stores the setting and starts the application again;
     * refused, {@code refused} runs, to put a control back to what it showed.
     */
    public static void ask(Component from, SuiteContext ctx, String change, Runnable apply, Runnable refused) {
        boolean scanning = false;
        for (opensuite.core.ScanTask t : ctx.tasks())
            if (t.sites().size() > 0 && t.state() != opensuite.core.ScanTask.State.FINISHED) scanning = true;
        boolean temporary = ctx.projectFile() == null;
        boolean contents = !ctx.store().all().isEmpty() || !ctx.issues().isEmpty();
        String cost = cost(change, temporary, contents, scanning);
        Runnable go = () -> { StateIO.saveBeforeRestart(ctx); apply.run(); };
        Window w = from instanceof Window ? (Window) from : SwingUtilities.getWindowAncestor(from);
        if (!(w instanceof RootPaneContainer)) { go.run(); return; }
        ExitPrompt.ask((RootPaneContainer) w, "Restart Open Suite?", cost, "Restart", "Apply the change and start Open Suite again",
                "Leave the setting as it is", go, refused);
    }
}