use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.
the class ProxyControl method addTimers.
/**
* Helper method to replicate any timers found within the Proxy Controller
* into the provided sampler, while replacing any occurrences of string _T_
* in the timer's configuration with the provided deltaT.
* Called from AWT Event thread
* @param model
* Test component tree model
* @param node
* Sampler node in where we will add the timers
* @param deltaT
* Time interval from the previous request
*/
private void addTimers(JMeterTreeModel model, JMeterTreeNode node, long deltaT) {
TestPlan variables = new TestPlan();
// $NON-NLS-1$
variables.addParameter("T", Long.toString(deltaT));
ValueReplacer replacer = new ValueReplacer(variables);
JMeterTreeNode mySelf = model.getNodeOf(this);
Enumeration<JMeterTreeNode> children = mySelf.children();
while (children.hasMoreElements()) {
JMeterTreeNode templateNode = children.nextElement();
if (templateNode.isEnabled()) {
TestElement template = templateNode.getTestElement();
if (template instanceof Timer) {
TestElement timer = (TestElement) template.clone();
try {
timer.setComment("Recorded:" + Long.toString(deltaT) + "ms");
replacer.undoReverseReplace(timer);
model.addComponent(timer, node);
} catch (InvalidVariableException | IllegalUserActionException e) {
// Not 100% sure, but I believe this can't happen, so
// I'll log and throw an error:
log.error("Program error", e);
throw new Error(e);
}
}
}
}
}
use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.
the class ProxyControl method setAuthorization.
/**
* Find if there is any AuthManager in JMeterTreeModel
* If there is no one, create and add it to tree
* Add authorization object to AuthManager
* @param authorization {@link Authorization}
* @param target {@link JMeterTreeNode}
*/
private void setAuthorization(Authorization authorization, JMeterTreeNode target) {
JMeterTreeModel jmeterTreeModel = getJmeterTreeModel();
List<JMeterTreeNode> authManagerNodes = jmeterTreeModel.getNodesOfType(AuthManager.class);
if (authManagerNodes.isEmpty()) {
try {
log.debug("Creating HTTP Authentication manager for authorization:" + authorization);
AuthManager authManager = newAuthorizationManager(authorization);
jmeterTreeModel.addComponent(authManager, target);
} catch (IllegalUserActionException e) {
log.error("Failed to add Authorization Manager to target node:" + target.getName(), e);
}
} else {
AuthManager authManager = (AuthManager) authManagerNodes.get(0).getTestElement();
authManager.addAuth(authorization);
}
}
use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.
the class NonGuiProxySample method main.
public static void main(String[] args) throws IllegalUserActionException, IOException {
// Or wherever you put it.
JMeterUtils.setJMeterHome("./");
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
JMeterUtils.initLocale();
TestPlan testPlan = new TestPlan();
ThreadGroup threadGroup = new ThreadGroup();
ListedHashTree testPlanTree = new ListedHashTree();
testPlanTree.add(testPlan);
testPlanTree.add(threadGroup, testPlan);
// deliberate use of deprecated ctor
@SuppressWarnings("deprecation") JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(testPlanTree, root);
ProxyControl proxy = new ProxyControl();
proxy.setNonGuiTreeModel(treeModel);
proxy.setTarget(treeModel.getNodeOf(threadGroup));
proxy.setPort(8282);
treeModel.addComponent(proxy, (JMeterTreeNode) root.getChildAt(1));
proxy.startProxy();
HttpHost proxyHost = new HttpHost("localhost", 8282);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();
try {
httpclient.execute(new HttpGet("http://example.invalid"));
} catch (Exception e) {
//
}
proxy.stopProxy();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
SaveService.saveTree(treeModel.getTestPlan(), out);
out.close();
System.out.println(out.toString());
}
}
use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.
the class SaveGraphics method doAction.
@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
if (!commands.contains(e.getActionCommand())) {
throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
}
if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS)) {
JMeterGUIComponent component = GuiPackage.getInstance().getCurrentGui();
// get the JComponent from the visualizer
if (component instanceof Printable) {
JComponent comp = ((Printable) component).getPrintableComponent();
saveImage(comp);
}
}
if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS_ALL)) {
JMeterGUIComponent component = GuiPackage.getInstance().getCurrentGui();
JComponent comp = ((JComponent) component).getRootPane();
saveImage(comp);
}
}
use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.
the class Save method doAction.
@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
HashTree subTree;
// are we saving the whole tree?
boolean fullSave = false;
if (!commands.contains(e.getActionCommand())) {
throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
}
if (e.getActionCommand().equals(ActionNames.SAVE_AS)) {
JMeterTreeNode[] nodes = GuiPackage.getInstance().getTreeListener().getSelectedNodes();
if (nodes.length > 1) {
JMeterUtils.reportErrorToUser(// $NON-NLS-1$
JMeterUtils.getResString("save_as_error"), // $NON-NLS-1$
JMeterUtils.getResString("save_as"));
return;
}
subTree = GuiPackage.getInstance().getCurrentSubTree();
} else if (e.getActionCommand().equals(ActionNames.SAVE_AS_TEST_FRAGMENT)) {
JMeterTreeNode[] nodes = GuiPackage.getInstance().getTreeListener().getSelectedNodes();
if (checkAcceptableForTestFragment(nodes)) {
subTree = GuiPackage.getInstance().getCurrentSubTree();
// Create Test Fragment node
TestElement element = GuiPackage.getInstance().createTestElement(TestFragmentControllerGui.class.getName());
HashTree hashTree = new ListedHashTree();
HashTree tfTree = hashTree.add(new JMeterTreeNode(element, null));
for (JMeterTreeNode node : nodes) {
// Clone deeply current node
TreeCloner cloner = new TreeCloner(false);
GuiPackage.getInstance().getTreeModel().getCurrentSubTree(node).traverse(cloner);
// Add clone to tfTree
tfTree.add(cloner.getClonedTree());
}
subTree = hashTree;
} else {
JMeterUtils.reportErrorToUser(// $NON-NLS-1$
JMeterUtils.getResString("save_as_test_fragment_error"), // $NON-NLS-1$
JMeterUtils.getResString("save_as_test_fragment"));
return;
}
} else {
fullSave = true;
HashTree testPlan = GuiPackage.getInstance().getTreeModel().getTestPlan();
// If saveWorkBench
if (isWorkbenchSaveable()) {
HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
testPlan.add(workbench);
}
subTree = testPlan;
}
String updateFile = GuiPackage.getInstance().getTestPlanFile();
if (!ActionNames.SAVE.equals(e.getActionCommand()) || updateFile == null) {
JFileChooser chooser = FileDialoger.promptToSaveFile(updateFile == null ? GuiPackage.getInstance().getTreeListener().getCurrentNode().getName() + JMX_FILE_EXTENSION : updateFile);
if (chooser == null) {
return;
}
updateFile = chooser.getSelectedFile().getAbsolutePath();
// Make sure the file ends with proper extension
if (FilenameUtils.getExtension(updateFile).isEmpty()) {
updateFile = updateFile + JMX_FILE_EXTENSION;
}
// Check if the user is trying to save to an existing file
File f = new File(updateFile);
if (f.exists()) {
int response = JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(), // $NON-NLS-1$
JMeterUtils.getResString("save_overwrite_existing_file"), // $NON-NLS-1$
JMeterUtils.getResString("save?"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.NO_OPTION) {
// Do not save, user does not want to overwrite
return;
}
}
if (!e.getActionCommand().equals(ActionNames.SAVE_AS)) {
GuiPackage.getInstance().setTestPlanFile(updateFile);
}
}
// backup existing file according to jmeter/user.properties settings
List<File> expiredBackupFiles = EMPTY_FILE_LIST;
File fileToBackup = new File(updateFile);
try {
expiredBackupFiles = createBackupFile(fileToBackup);
} catch (Exception ex) {
//$NON-NLS-1$
log.error("Failed to create a backup for {}", fileToBackup, ex);
}
try {
convertSubTree(subTree);
} catch (Exception err) {
if (log.isWarnEnabled()) {
log.warn("Error converting subtree. {}", err.toString());
}
}
try (FileOutputStream ostream = new FileOutputStream(updateFile)) {
SaveService.saveTree(subTree, ostream);
if (fullSave) {
// Only update the stored copy of the tree for a full save
// refetch, because convertSubTree affects it
subTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
if (isWorkbenchSaveable()) {
HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
subTree.add(workbench);
}
ActionRouter.getInstance().doActionNow(new ActionEvent(subTree, e.getID(), ActionNames.SUB_TREE_SAVED));
}
// proceed to deletion
for (File expiredBackupFile : expiredBackupFiles) {
try {
FileUtils.deleteQuietly(expiredBackupFile);
} catch (Exception ex) {
//$NON-NLS-1$
log.warn("Failed to delete backup file, {}", expiredBackupFile);
}
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
log.error("Error saving tree.", ex);
throw new IllegalUserActionException("Couldn't save test plan to file: " + updateFile, ex);
}
GuiPackage.getInstance().updateCurrentGui();
}
Aggregations