From c8b378fd58efcffc01a95a5eb1468ba7527d5167 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Tue, 14 Jan 2025 16:18:31 +0300 Subject: [PATCH] refactor: factor out getEnabledArguments to avoid "isEnabled" check all over the place --- .../org/apache/jmeter/config/Arguments.java | 16 +++++++++++++++- .../protocol/http/config/gui/UrlConfigGui.java | 5 +---- .../protocol/http/sampler/AjpSampler.java | 2 +- .../protocol/http/sampler/HTTPHC4Impl.java | 18 ++++-------------- .../protocol/http/sampler/HTTPSamplerBase.java | 16 +++++----------- .../protocol/http/sampler/PostWriter.java | 4 ++-- .../protocol/http/sampler/PutWriter.java | 5 +---- .../protocol/http/util/HTTPArgument.java | 3 +-- 8 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/core/src/main/java/org/apache/jmeter/config/Arguments.java b/src/core/src/main/java/org/apache/jmeter/config/Arguments.java index e6a060dd7fb..7f2127c48e1 100644 --- a/src/core/src/main/java/org/apache/jmeter/config/Arguments.java +++ b/src/core/src/main/java/org/apache/jmeter/config/Arguments.java @@ -23,11 +23,13 @@ import java.util.List; import java.util.Map; +import org.apache.commons.collections4.iterators.FilterIterator; import org.apache.jmeter.testelement.property.CollectionProperty; import org.apache.jmeter.testelement.property.JMeterProperty; import org.apache.jmeter.testelement.property.PropertyIterator; import org.apache.jmeter.testelement.property.TestElementProperty; import org.apache.jmeter.testelement.schema.PropertiesAccessor; +import org.apiguardian.api.API; /** * A set of Argument objects. @@ -100,7 +102,7 @@ public Map getArgumentsAsMap() { // that this element's values prevail over defaults provided by // configuration // elements: - if (!argMap.containsKey(arg.getName())) { + if (!argMap.containsKey(arg.getName()) && arg.isEnabled()) { argMap.put(arg.getName(), arg.getValue()); } } @@ -173,6 +175,18 @@ public PropertyIterator iterator() { return getArguments().iterator(); } + /** + * Returns the list of enabled arguments. + * @return the list of enabled arguments + */ + @API(since = "5.6", status = API.Status.EXPERIMENTAL) + public Iterable getEnabledArguments() { + return () -> new FilterIterator<>(iterator(), property -> { + Object value = property.getObjectValue(); + return value instanceof Argument && ((Argument) value).isEnabled(); + }); + } + /** * Create a string representation of the arguments. * diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java index 316a31022f3..ece7f2168cc 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java @@ -261,11 +261,8 @@ private static String computePostBody(Arguments arguments) { */ private static String computePostBody(Arguments arguments, boolean crlfToLF) { StringBuilder postBody = new StringBuilder(); - for (JMeterProperty argument : arguments) { + for (JMeterProperty argument : arguments.getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) argument.getObjectValue(); - if (!arg.isEnabled()) { - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } String value = arg.getValue(); if (crlfToLF) { value = value.replaceAll("\r\n", "\n"); // See modifyTestElement diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/AjpSampler.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/AjpSampler.java index d705ae726cb..2a5fe624bc7 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/AjpSampler.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/AjpSampler.java @@ -279,7 +279,7 @@ private String setConnectionHeaders(URL url, String host, String method) setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED); StringBuilder sb = new StringBuilder(); boolean first = true; - for (JMeterProperty arg : getArguments()) { + for (JMeterProperty arg : getArguments().getEnabledArguments()) { if (first) { first = false; } else { diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java index c042ed6f801..aacb9cd883f 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java @@ -1571,15 +1571,12 @@ protected String setupHttpEntityEnclosingRequestData(HttpEntityEnclosingRequestB } // Create the parts // Add any parameters - for (JMeterProperty jMeterProperty : getArguments()) { + for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); String parameterName = arg.getName(); if (arg.isSkippable(parameterName)) { continue; } - if (!arg.isEnabled()) { - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } ContentType contentType; if (arg.getContentType().indexOf(';') >= 0) { // assume, that the content type contains charset info @@ -1656,11 +1653,8 @@ else if(ADD_CONTENT_TYPE_TO_POST_IF_MISSING) { // Just append all the parameter values, and use that as the post body StringBuilder postBody = new StringBuilder(); - for (JMeterProperty jMeterProperty : getArguments()) { + for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); - if (!arg.isEnabled()) { - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } postBody.append(arg.getEncodedValue(contentEncoding)); } // Let StringEntity perform the encoding @@ -1802,10 +1796,9 @@ else if(getSendParameterValuesAsPostBody()) { private UrlEncodedFormEntity createUrlEncodedFormEntity(final String urlContentEncoding) throws UnsupportedEncodingException { // It is a normal request, with parameter names and values // Add the parameters - PropertyIterator args = getArguments().iterator(); List nvps = new ArrayList<>(); - while (args.hasNext()) { - HTTPArgument arg = (HTTPArgument) args.next().getObjectValue(); + for (JMeterProperty jMeterProperty: getArguments().getEnabledArguments()) { + HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); // The HTTPClient always urlencodes both name and value, // so if the argument is already encoded, we have to decode // it before adding it to the post request @@ -1813,9 +1806,6 @@ private UrlEncodedFormEntity createUrlEncodedFormEntity(final String urlContentE if (arg.isSkippable(parameterName)) { continue; } - if (!arg.isEnabled()) { - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } String parameterValue = arg.getValue(); if (!arg.isAlwaysEncoded()) { // The value is already encoded by the user diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java index 91ed064a1f9..e8fb59bff6d 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java @@ -409,7 +409,7 @@ public boolean getSendParameterValuesAsPostBody() { return true; } else { boolean hasArguments = false; - for (JMeterProperty jMeterProperty : getArguments()) { + for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) { hasArguments = true; HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); if (arg.getName() != null && !arg.getName().isEmpty()) { @@ -1155,9 +1155,10 @@ public String getQueryString() { */ public String getQueryString(final String contentEncoding) { - CollectionProperty arguments = getArguments().getArguments(); + Arguments args = getArguments(); + Iterator iter = args.getEnabledArguments().iterator(); // Optimisation : avoid building useless objects if empty arguments - if(arguments.isEmpty()) { + if (!iter.hasNext()) { return ""; } String lContentEncoding = contentEncoding; @@ -1167,8 +1168,7 @@ public String getQueryString(final String contentEncoding) { lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING; } - StringBuilder buf = new StringBuilder(arguments.size() * 15); - PropertyIterator iter = arguments.iterator(); + StringBuilder buf = new StringBuilder(args.getArgumentCount() * 15); boolean first = true; while (iter.hasNext()) { HTTPArgument item = null; @@ -1186,12 +1186,6 @@ public String getQueryString(final String contentEncoding) { item = new HTTPArgument((Argument) objectValue); } final String encodedName = item.getEncodedName(); - if (encodedName.isEmpty()) { - continue; // Skip parameters with a blank name (allows use of optional variables in parameter lists) - } - if(!item.isEnabled()){ - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } if (!first) { buf.append(QRY_SEP); } else { diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PostWriter.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PostWriter.java index 60d01217525..447a65f1073 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PostWriter.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PostWriter.java @@ -190,7 +190,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(bos, contentEncoding); // Add any parameters - for (JMeterProperty jMeterProperty : sampler.getArguments()) { + for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); String parameterName = arg.getName(); if (arg.isSkippable(parameterName)) { @@ -299,7 +299,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws // Just append all the parameter values, and use that as the post body StringBuilder postBodyBuffer = new StringBuilder(); - for (JMeterProperty jMeterProperty : sampler.getArguments()) { + for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); postBodyBuffer.append(arg.getEncodedValue(contentEncoding)); } diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PutWriter.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PutWriter.java index 6419919b01f..66a79b6b4e7 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PutWriter.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PutWriter.java @@ -84,11 +84,8 @@ else if(sampler.getSendParameterValuesAsPostBody()) { // Just append all the parameter values, and use that as the put body StringBuilder putBodyBuffer = new StringBuilder(); - for (JMeterProperty jMeterProperty : sampler.getArguments()) { + for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); - if (!arg.isEnabled()) { - continue; // Skip parameters if they've been disabled from GUI using the checkbox - } putBodyBuffer.append(arg.getEncodedValue(contentEncoding)); } diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPArgument.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPArgument.java index e73f40a0494..3353589b1f8 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPArgument.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPArgument.java @@ -134,7 +134,7 @@ public HTTPArgument(String name, String value, boolean alreadyEncoded) { } /** - * Construct a new HTTPArgument instance; enabled and alwaysEncoded set to true. + * Construct a new HTTPArgument instance; alwaysEncoded is set to true. * * @param name the name of the parameter * @param value the value of the parameter @@ -142,7 +142,6 @@ public HTTPArgument(String name, String value, boolean alreadyEncoded) { * @param contentEncoding the encoding used for the parameter value */ public HTTPArgument(String name, String value, boolean alreadyEncoded, String contentEncoding) { - setEnabled(true); setAlwaysEncoded(true); if (alreadyEncoded) { try {