org.zaproxy.zap.control.AddOnRunIssuesUtils Maven / Gradle / Ivy
Show all versions of zap Show documentation
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2015 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.
*/
package org.zaproxy.zap.control;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.lang.SystemUtils;
import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXTree;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.view.View;
/**
* An utility/helper class that extract textual representations of running issues of add-ons and show warning messages of
* add-ons that can not be run.
*
* @since 2.4.0
*/
public final class AddOnRunIssuesUtils {
private static final Logger LOGGER = Logger.getLogger(AddOnRunIssuesUtils.class);
private AddOnRunIssuesUtils() {
}
/**
* Shows a warning dialogue with the add-ons and its corresponding running issues or the issues if is extensions.
*
* The dialogue is composed with the given {@code message} and a tree in which are shown the add-ons and its issues as child
* nodes of the add-ons.
*
* @param message the main message shown in the dialogue
* @param availableAddOns the add-ons that are available, used to create and check the running issues
* @param addOnsNotRunnable the add-ons with running issues that will be shown in the tree
*/
public static void showWarningMessageAddOnsNotRunnable(
String message,
AddOnCollection availableAddOns,
Collection addOnsNotRunnable) {
Object[] msgs = {
message,
createScrollableTreeAddOnsNotRunnable(
availableAddOns,
addOnsNotRunnable.toArray(new AddOn[addOnsNotRunnable.size()])) };
JOptionPane.showMessageDialog(
View.getSingleton().getMainFrame(),
msgs,
Constant.PROGRAM_NAME,
JOptionPane.WARNING_MESSAGE);
}
/**
* Creates a scrollable tree with the given add-ons as root nodes and its issues as child nodes.
*
* @param availableAddOns the add-ons that are available, used to create check the running issues
* @param addOnsNotRunnable the add-ons with running issues that will be shown in the tree
* @return the tree wrapper in a {@code JSCrollPane}
*/
private static JScrollPane createScrollableTreeAddOnsNotRunnable(
final AddOnCollection availableAddOns,
AddOn... addOnsNotRunnable) {
AddOnSearcher addOnSearcher = new AddOnSearcher() {
@Override
public AddOn searchAddOn(String id) {
return availableAddOns.getAddOn(id);
}
};
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("");
for (AddOn addOn : addOnsNotRunnable) {
DefaultMutableTreeNode addOnNode = new DefaultMutableTreeNode(addOn.getName());
AddOn.AddOnRunRequirements requirements = addOn.calculateRunRequirements(availableAddOns.getAddOns());
List issues = getUiRunningIssues(requirements, addOnSearcher);
if (issues.isEmpty()) {
issues.addAll(getUiExtensionsRunningIssues(requirements, addOnSearcher));
}
for (String issue : issues) {
addOnNode.add(new DefaultMutableTreeNode(issue));
}
rootNode.add(addOnNode);
}
JXTree tree = new JXTree(new DefaultTreeModel(rootNode));
tree.setVisibleRowCount(5);
tree.setEditable(false);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.expandAll();
return new JScrollPane(tree);
}
/**
* Shows a confirmation dialogue (yes-no question), with the given message at the top, followed by a tree in which is shown
* the add-on with its corresponding running issues and, at the bottom before the yes and no buttons, the given question.
*
* @param message the main message shown in the dialogue
* @param question the questions shown at the bottom of the dialogue, before the buttons
* @param availableAddOns the add-ons that are available, used to create check the running issues
* @param addOnNotRunnable the add-on with running issues that will be shown in the tree
* @return {@code true} if it is confirmed, {@code false} otherwise
*/
public static boolean askConfirmationAddOnNotRunnable(
String message,
String question,
AddOnCollection availableAddOns,
AddOn addOnNotRunnable) {
Object[] msgs = { message, createScrollableTreeAddOnsNotRunnable(availableAddOns, addOnNotRunnable), question };
return JOptionPane.showConfirmDialog(
View.getSingleton().getMainFrame(),
msgs,
Constant.PROGRAM_NAME,
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
}
/**
* Returns the textual representations of the running issues (Java version and dependency), if any.
*
* The messages are internationalised thus suitable for UI components.
*
* @param requirements the run requirements of the add-on
* @param addOnSearcher the class responsible for searching add-ons with a given id, used to search for add-ons that are
* missing for the add-on
* @return a {@code List} containing all the running issues of the add-on, empty if none
* @see #getRunningIssues(AddOn.BaseRunRequirements)
* @see #getUiExtensionsRunningIssues(AddOn.AddOnRunRequirements, AddOnSearcher)
*/
public static List getUiRunningIssues(AddOn.BaseRunRequirements requirements, AddOnSearcher addOnSearcher) {
List issues = new ArrayList<>(2);
if (requirements.isNewerJavaVersionRequired()) {
if (requirements.getAddOn() != requirements.getAddOnMinimumJavaVersion()) {
issues.add(MessageFormat.format(
Constant.messages.getString("cfu.warn.addon.with.missing.requirements.javaversion.dependency"),
requirements.getMinimumJavaVersion(),
(SystemUtils.JAVA_VERSION == null
? Constant.messages.getString("cfu.warn.unknownJavaVersion")
: SystemUtils.JAVA_VERSION),
requirements.getAddOnMinimumJavaVersion().getName()));
} else {
issues.add(MessageFormat.format(
Constant.messages.getString("cfu.warn.addon.with.missing.requirements.javaversion"),
requirements.getMinimumJavaVersion(),
(SystemUtils.JAVA_VERSION == null
? Constant.messages.getString("cfu.warn.unknownJavaVersion")
: SystemUtils.JAVA_VERSION)));
}
}
if (requirements.hasDependencyIssue()) {
List