org.zaproxy.zap.view.ContextTechnologyPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.zaproxy.zap.view.ContextTechnologyPanel.java

Source

/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2012 The ZAP Development Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 * Note that this extension ane the other classes in this package are heavily 
 * based on the orriginal Paros ExtensionSpider! 
 */
package org.zaproxy.zap.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.apache.commons.lang.ArrayUtils;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.model.Session;
import org.zaproxy.zap.model.Context;
import org.zaproxy.zap.model.Tech;
import org.zaproxy.zap.model.TechSet;

public class ContextTechnologyPanel extends AbstractContextPropertiesPanel {

    private static final String PANEL_NAME = Constant.messages.getString("context.technology.title");
    private static final long serialVersionUID = -8337361808959321380L;

    private JPanel panelSession = null;
    private JScrollPane jScrollPane = null;
    private TreeModel model = null;
    private HashMap<Tech, DefaultMutableTreeNode> techToNodeMap = new HashMap<>();

    private JCheckBoxTree techTree = null;

    public static String getPanelName(int contextId) {
        // Panel names have to be unique, so precede with the context id
        return contextId + ": " + PANEL_NAME;
    }

    public ContextTechnologyPanel(Context context) {
        super(context.getIndex());
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setLayout(new CardLayout());
        this.setName(getPanelName(getContextIndex()));
        this.add(getPanelSession(), getPanelSession().getName());
    }

    /**
     * This method initializes panelSession
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getPanelSession() {
        if (panelSession == null) {

            panelSession = new JPanel();
            panelSession.setLayout(new BorderLayout());
            panelSession.setName("SessionTech");
            panelSession.add(getJScrollPane(), BorderLayout.CENTER);
        }
        return panelSession;
    }

    private TreePath getPath(TreeNode node) {
        List<TreeNode> list = new ArrayList<>();

        // Add all nodes to list
        while (node != null) {
            list.add(node);
            node = node.getParent();
        }
        Collections.reverse(list);

        // Convert array of nodes to TreePath
        return new TreePath(list.toArray());
    }

    private JCheckBoxTree getTechTree() {
        if (techTree == null) {
            techTree = new JCheckBoxTree() {
                private static final long serialVersionUID = 1L;

                @Override
                protected void setExpandedState(TreePath path, boolean state) {
                    // Ignore all collapse requests; collapse events will not be fired
                    if (state) {
                        super.setExpandedState(path, state);
                    }
                }
            };
            // Initialise the structure based on all the tech we know about
            TechSet ts = new TechSet(Tech.builtInTech);
            Iterator<Tech> iter = ts.getIncludeTech().iterator();

            DefaultMutableTreeNode root = new DefaultMutableTreeNode(
                    Constant.messages.getString("context.technology.tree.root"));
            Tech tech;
            DefaultMutableTreeNode parent;
            DefaultMutableTreeNode node;
            while (iter.hasNext()) {
                tech = iter.next();
                if (tech.getParent() != null) {
                    parent = techToNodeMap.get(tech.getParent());
                } else {
                    parent = null;
                }
                if (parent == null) {
                    parent = root;
                }
                node = new DefaultMutableTreeNode(tech.getUiName());
                parent.add(node);
                techToNodeMap.put(tech, node);
            }

            model = new DefaultTreeModel(root);
            techTree.setModel(model);
            techTree.expandAll();
            techTree.setCheckBoxEnabled(new TreePath(root), false);

        }
        return techTree;
    }

    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setViewportView(getTechTree());
            jScrollPane.setBorder(
                    javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        }
        return jScrollPane;
    }

    @Override
    public String getHelpIndex() {
        return "ui.dialogs.contexts";
    }

    @Override
    public void initContextData(Session session, Context uiContext) {
        // Init model from context
        TechSet techSet = uiContext.getTechSet();
        Set<Tech> includedTech = techSet.getIncludeTech();
        Iterator<Entry<Tech, DefaultMutableTreeNode>> iter = techToNodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Tech, DefaultMutableTreeNode> node = iter.next();
            TreePath tp = this.getPath(node.getValue());
            Tech tech = node.getKey();
            if (ArrayUtils.contains(Tech.builtInTopLevelTech, tech)) {
                this.getTechTree().check(tp, containsAnyOfTopLevelTech(includedTech, tech));
            } else {
                this.getTechTree().check(tp, techSet.includes(tech));
            }
        }

    }

    private static boolean containsAnyOfTopLevelTech(Set<Tech> techSet, Tech topLevelTech) {
        for (Tech tech : techSet) {
            if (topLevelTech.equals(tech.getParent())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void validateContextData(Session session) throws Exception {
        // Nothing to validate

    }

    @Override
    public void saveContextData(Session session) throws Exception {

        session.getContext(getContextIndex()).setTechSet(buildTechSet());

    }

    private TechSet buildTechSet() {
        TechSet techSet = new TechSet();

        Iterator<Entry<Tech, DefaultMutableTreeNode>> iter = techToNodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Tech, DefaultMutableTreeNode> node = iter.next();
            TreePath tp = this.getPath(node.getValue());
            Tech tech = node.getKey();
            if (this.getTechTree().isSelectedFully(tp)) {
                techSet.include(tech);
            } else {
                techSet.exclude(tech);
            }
        }
        return techSet;
    }

    @Override
    public void saveTemporaryContextData(Context uiSharedContext) {
        uiSharedContext.setTechSet(buildTechSet());
    }

}