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.

2.9 KB
Raw Blame
package opensuite.ui;

import opensuite.core.LogEntry;

import javax.swing.*;
import java.awt.*;

/**
 * A request editor and a response editor shown together, as every tool that
 * displays a message shows them: side by side and parted by one hairline,
 * with the layout switch on the response's heading line (side by side,
 * stacked, or one at a time). While nothing is selected the pair is blank,
 * rather than two empty editors.
 */
public final class MessagePair extends JPanel {
    private final MessageEditor req, resp;
    private final JSplitPane split;
    private final JPanel body = new JPanel(new BorderLayout());
    private final CardLayout cards = new CardLayout();
    private JTabbedPane tabs;

    private final JPanel blank = new JPanel();

    /** The colour of the pane while nothing is shown in it. */
    public void setBlankBackground(Color c) { blank.setBackground(c); }

    public MessagePair(MessageEditor req, MessageEditor resp) {
        this.req = req;
        this.resp = resp;
        req.setRelatedResponse(resp::message);
        setLayout(cards);
        split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, req, resp);
        split.setBorder(null);
        Ui.balance(split, 0.5);
        resp.setTitleTrailing(LayoutIcons.create(this::setMode));
        resp.pairWith(req);
        body.add(split, BorderLayout.CENTER);
        JComponent dock = resp.detachDock();
        if (dock != null) body.add(dock, resp.dockSide());
        blank.setBackground(Theme.CONTENT);
        add(blank, "blank");
        add(body, "pair");
        cards.show(this, "blank");
    }

    public MessageEditor request() { return req; }
    public MessageEditor response() { return resp; }

    /** Show an entry's request and response, or go blank when there is none. */
    public void show(LogEntry e) {
        if (e == null) { setBlank(true); return; }
        req.showRequest(e);
        resp.showResponse(e);
        setBlank(false);
    }

    public void setBlank(boolean blank) { cards.show(this, blank ? "blank" : "pair"); }

    /** 0 side by side, 1 stacked, 2 one at a time. */
    public void setMode(int mode) {
        if (tabs != null) { body.remove(tabs); tabs.removeAll(); tabs = null; }
        if (mode == 2) {
            body.remove(split);
            split.setLeftComponent(null);
            split.setRightComponent(null);
            tabs = new JTabbedPane();
            tabs.setFont(Ui.PLAIN);
            tabs.addTab("Request", req);
            tabs.addTab("Response", resp);
            body.add(tabs, BorderLayout.CENTER);
        } else {
            split.setOrientation(mode == 0 ? JSplitPane.HORIZONTAL_SPLIT : JSplitPane.VERTICAL_SPLIT);
            split.setLeftComponent(req);
            split.setRightComponent(resp);
            if (split.getParent() != body) body.add(split, BorderLayout.CENTER);
            split.setDividerLocation(0.5);
        }
        body.revalidate();
        body.repaint();
    }
}