Mercurial > stress-tester
changeset 924:3a6dc239361d
refactoring
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Fri Apr 27 10:30:37 2018 +0200 @@ -8,25 +8,21 @@ import com.passus.config.schema.KeyNameVaryListNodeDefinition; import com.passus.config.schema.NodeDefinition; import com.passus.config.schema.NodeDefinitionCreator; -import com.passus.data.ByteString; -import com.passus.filter.UnmutableValueExtractor; -import com.passus.filter.ValueExtractor; import com.passus.lookup.filter.LookupValueExtractorDefinitionCreator; import com.passus.net.http.*; import com.passus.st.client.http.HttpFlowContext; -import com.passus.st.client.http.extractor.ContentReplacer; import com.passus.st.config.HeaderOperationNodeDefinition; import com.passus.st.plugin.PluginConstants; import com.passus.st.validation.HeaderNameValidator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import static com.passus.config.schema.ConfigurationSchemaBuilder.*; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.Operation; /** * @author Mirosław Hawrot @@ -39,529 +35,6 @@ public static final String TYPE = "modifyMessage"; - public abstract static class Operation { - - private Operation() { - } - - public abstract void process(HttpRequest req, HttpResponse resp, HttpFlowContext context); - - protected final CharSequence extractValue(ValueExtractor extractor, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - Object value = HttpFiltersUtils.extractValue(extractor, req, resp, context); - if (value instanceof CharSequence) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("extractor={} extracted={}", extractor, value); - } - - return (CharSequence) value; - } - - return null; - } - - } - - private static abstract class AbstractRemoveOperation extends Operation { - - protected final ValueExtractor nameExtractor; - - public AbstractRemoveOperation(CharSequence name) { - this(new UnmutableValueExtractor(ByteString.create(name))); - } - - public AbstractRemoveOperation(ValueExtractor extractor) { - Assert.notNull(extractor, "extractor"); - this.nameExtractor = extractor; - } - - public ValueExtractor getNameExtractor() { - return nameExtractor; - } - - @Override - public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { - CharSequence name = extractValue(nameExtractor, req, resp, context); - if (name != null) { - doProcess(name, req, resp, context); - } - } - - protected abstract void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context); - - } - - protected static abstract class AbstractNameValueOperation extends Operation { - - protected final ByteString name; - - protected final ValueExtractor valueExtractor; - - protected boolean escape = true; - - public AbstractNameValueOperation(CharSequence name, CharSequence value) { - this(name, new UnmutableValueExtractor(value)); - } - - public AbstractNameValueOperation(CharSequence name, ValueExtractor valueExtractor) { - Assert.notNull(name, "name"); - Assert.notNull(valueExtractor, "valueExtractor"); - this.name = ByteString.create(name); - this.valueExtractor = valueExtractor; - } - - public ByteString getName() { - return name; - } - - public ValueExtractor getValueExtractor() { - return valueExtractor; - } - - public boolean isEscape() { - return escape; - } - - public void setEscape(boolean escape) { - this.escape = escape; - } - - @Override - public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { - CharSequence value = extractValue(valueExtractor, req, resp, context); - if (value != null) { - doProcess(value, req, resp, context); - } - } - - protected abstract void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context); - - } - - public static class RemoveHeaderOperation extends AbstractRemoveOperation { - - public RemoveHeaderOperation(CharSequence name) { - super(name); - } - - public RemoveHeaderOperation(ValueExtractor extractor) { - super(extractor); - } - - @Override - protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - req.getHeaders().delete(name); - } - - } - - public static class AddHeaderOperation extends AbstractNameValueOperation { - - public AddHeaderOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public AddHeaderOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - req.getHeaders().add(name, value); - } - - } - - public static class SetHeaderOperation extends AbstractNameValueOperation { - - public SetHeaderOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public SetHeaderOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - req.getHeaders().set(name, value); - } - - } - - public static class RemoveCookieOperation extends AbstractRemoveOperation { - - public RemoveCookieOperation(CharSequence name) { - super(name); - } - - public RemoveCookieOperation(ValueExtractor extractor) { - super(extractor); - } - - @Override - protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - HttpMessageHelper.get().removeCookie(req, name); - } - - } - - public static class AddCookieOperation extends AbstractNameValueOperation { - - public AddCookieOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public AddCookieOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - HttpMessageHelper.get().addCookie(req, name, value); - } - - } - - public static class SetCookieOperation extends AbstractNameValueOperation { - - public SetCookieOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public SetCookieOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - HttpMessageHelper.get().setCookie(req, name, value); - } - - } - - public static class RemoveQueryParameterOperation extends AbstractRemoveOperation { - - public RemoveQueryParameterOperation(CharSequence paramName) { - super(paramName); - } - - public RemoveQueryParameterOperation(ValueExtractor extractor) { - super(extractor); - } - - @Override - protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper.get().removeQueryParameter(req, name); - } catch (ParseException ex) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(ex.getMessage(), ex); - } - } - } - - } - - public static class AddQueryParameterOperation extends AbstractNameValueOperation { - - public AddQueryParameterOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public AddQueryParameterOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper.get().addQueryParameter(req, name, value, escape); - } catch (ParseException ex) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(ex.getMessage(), ex); - } - } - } - - } - - public static class SetQueryParameterOperation extends AbstractNameValueOperation { - - public SetQueryParameterOperation(CharSequence name, CharSequence value) { - super(name, value); - } - - public SetQueryParameterOperation(CharSequence name, ValueExtractor valueExtractor) { - super(name, valueExtractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper.get().setQueryParameter(req, name, value, escape); - } catch (ParseException ex) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(ex.getMessage(), ex); - } - } - } - - } - - protected static abstract class RemoveParamOperation extends AbstractRemoveOperation { - - public RemoveParamOperation(CharSequence name) { - super(name); - } - - public RemoveParamOperation(ValueExtractor extractor) { - super(extractor); - } - - protected boolean doRemove(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - CharSequence name = extractValue(nameExtractor, req, resp, context); - if (name != null) { - return params.remove(name); - } - - return false; - } - - } - - protected static abstract class AddParamOperation extends Operation { - - protected final ByteString name; - - protected final ValueExtractor valueExtractor; - - protected boolean escape = true; - - public AddParamOperation(CharSequence name, Object value) { - Assert.notNull(name, "name"); - Assert.notNull(value, "value"); - - if (value instanceof CharSequence) { - this.valueExtractor = new UnmutableValueExtractor(value); - } else if (value instanceof List) { - for (Object val : (List) value) { - if (!(val instanceof CharSequence)) { - throw new IllegalArgumentException("Value should be CharSequence or List<CharSequence>."); - } - } - - this.valueExtractor = new UnmutableValueExtractor(value); - } else { - throw new IllegalArgumentException("Value should be CharSequence or List<CharSequence>."); - } - - this.name = ByteString.create(name); - } - - public AddParamOperation(CharSequence name, ValueExtractor extractor) { - Assert.notNull(name, "name"); - Assert.notNull(extractor, "extractor"); - this.name = ByteString.create(name); - this.valueExtractor = extractor; - } - - public boolean isEscape() { - return escape; - } - - public void setEscape(boolean escape) { - this.escape = escape; - } - - protected void doAdd(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); - try { - if (value instanceof CharSequence) { - params.add(name, (CharSequence) value); - } else if (value instanceof List) { - params.add(name, (List<CharSequence>) value); - } - } catch (Exception ex) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(ex.getMessage(), ex); - } - } - - } - - } - - private static abstract class SetParamOperation extends AddParamOperation { - - public SetParamOperation(CharSequence name, Object value) { - this(name, new UnmutableValueExtractor(value)); - } - - public SetParamOperation(CharSequence name, ValueExtractor extractor) { - super(name, extractor); - } - - protected void doSet(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - params.remove(name); - doAdd(params, req, resp, context); - } - } - - public static class PostDataRemoveParamOperation extends RemoveParamOperation { - - public PostDataRemoveParamOperation(CharSequence name) { - super(name); - } - - public PostDataRemoveParamOperation(ValueExtractor extractor) { - super(extractor); - } - - @Override - protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper helper = HttpMessageHelper.get(); - - if (helper.isFormUrlencoded(req)) { - HttpParameters params = helper.decodeFormUrlencoded(req); - if (params != null) { - if (doRemove(params, req, resp, context)) { - helper.setFormUrlencoded(req, params); - } - } - } else if (helper.isMultipart(req)) { - HttpMultipartData parts = helper.decodeMultipart(req); - if (parts == null) { - return; - } - - CharSequence name = extractValue(nameExtractor, req, resp, context); - HttpDataPart part = parts.findByContentDispositionName(name); - if (part != null) { - parts.remove(part); - helper.setMultipartContent(req, parts); - } - } - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(e.getMessage(), e); - } - } - } - } - - public static class PostDataAddParamOperation extends AddParamOperation { - - public PostDataAddParamOperation(CharSequence name, Object value) { - super(name, value); - } - - public PostDataAddParamOperation(CharSequence name, ValueExtractor extractor) { - super(name, extractor); - } - - @Override - public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper helper = HttpMessageHelper.get(); - - if (helper.isFormUrlencoded(req)) { - HttpParameters params = helper.decodeFormUrlencoded(req, escape); - if (params != null) { - doAdd(params, req, resp, context); - helper.setFormUrlencoded(req, params, escape); - } - } else if (helper.isMultipart(req)) { - HttpMultipartData parts = helper.decodeMultipart(req); - if (parts == null) { - return; - } - - Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); - parts.addFormData(this.name, value); - helper.setMultipartContent(req, parts); - } - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(e.getMessage(), e); - } - } - } - } - - public static class PostDataSetParamOperation extends SetParamOperation { - - public PostDataSetParamOperation(CharSequence name, Object value) { - super(name, value); - } - - public PostDataSetParamOperation(CharSequence name, ValueExtractor extractor) { - super(name, extractor); - } - - @Override - public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { - try { - HttpMessageHelper helper = HttpMessageHelper.get(); - if (helper.isFormUrlencoded(req)) { - HttpParameters params = helper.decodeFormUrlencoded(req, escape); - if (params != null) { - doSet(params, req, resp, context); - helper.setFormUrlencoded(req, params, escape); - } - } else if (helper.isMultipart(req)) { - HttpMultipartData parts = helper.decodeMultipart(req); - if (parts == null) { - return; - } - - Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); - HttpDataPart part = parts.findByContentDispositionName(this.name); - if (part == null) { - parts.addFormData(this.name, value); - } else { - part.setContent(helper.toDataSource(value)); - } - - helper.setMultipartContent(req, parts); - } - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(e.getMessage(), e); - } - } - } - - } - - public static class ContentOperation extends Operation { - - private final ContentReplacer replacer; - - private final CharSequence newValue; - - public ContentOperation(ContentReplacer replacer, CharSequence newValue) { - this.replacer = replacer; - this.newValue = newValue; - } - - @Override - public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { - if (req == null) { - return; - } - - try { - replacer.replace(req, newValue); - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(e.getMessage(), e); - } - } - } - - } - private final List<Operation> operations = new ArrayList<>(); private HttpMessagePredicate predicate;
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Fri Apr 27 10:30:37 2018 +0200 @@ -8,7 +8,7 @@ import com.passus.lookup.filter.LookupValueExtractorTransformer; import com.passus.st.client.http.extractor.ContentExtractorUtils; import com.passus.st.client.http.extractor.ContentReplacer; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.*; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import com.passus.st.filter.Transformers; import java.util.ArrayList;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java Fri Apr 27 10:30:37 2018 +0200 @@ -0,0 +1,551 @@ +package com.passus.st.client.http.filter; + +import com.passus.commons.Assert; +import com.passus.data.ByteString; +import com.passus.filter.UnmutableValueExtractor; +import com.passus.filter.ValueExtractor; +import com.passus.net.http.HttpDataPart; +import com.passus.net.http.HttpMessageHelper; +import com.passus.net.http.HttpMultipartData; +import com.passus.net.http.HttpParameters; +import com.passus.net.http.HttpRequest; +import com.passus.net.http.HttpResponse; +import com.passus.st.client.http.HttpFlowContext; +import com.passus.st.client.http.extractor.ContentReplacer; +import java.text.ParseException; +import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * + * @author mikolaj.podbielski + */ +public class HttpMessageModificationOperations { + + private static final Logger LOGGER = LogManager.getLogger(HttpMessageModificationFilter.class); + + public abstract static class Operation { + + private Operation() { + } + + public abstract void process(HttpRequest req, HttpResponse resp, HttpFlowContext context); + + protected final CharSequence extractValue(ValueExtractor extractor, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + Object value = HttpFiltersUtils.extractValue(extractor, req, resp, context); + if (value instanceof CharSequence) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("extractor={} extracted={}", extractor, value); + } + + return (CharSequence) value; + } + + return null; + } + + } + + private static abstract class AbstractRemoveOperation extends Operation { + + protected final ValueExtractor nameExtractor; + + public AbstractRemoveOperation(CharSequence name) { + this(new UnmutableValueExtractor(ByteString.create(name))); + } + + public AbstractRemoveOperation(ValueExtractor extractor) { + Assert.notNull(extractor, "extractor"); + this.nameExtractor = extractor; + } + + public ValueExtractor getNameExtractor() { + return nameExtractor; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + CharSequence name = extractValue(nameExtractor, req, resp, context); + if (name != null) { + doProcess(name, req, resp, context); + } + } + + protected abstract void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context); + + } + + protected static abstract class AbstractNameValueOperation extends Operation { + + protected final ByteString name; + + protected final ValueExtractor valueExtractor; + + protected boolean escape = true; + + public AbstractNameValueOperation(CharSequence name, CharSequence value) { + this(name, new UnmutableValueExtractor(value)); + } + + public AbstractNameValueOperation(CharSequence name, ValueExtractor valueExtractor) { + Assert.notNull(name, "name"); + Assert.notNull(valueExtractor, "valueExtractor"); + this.name = ByteString.create(name); + this.valueExtractor = valueExtractor; + } + + public ByteString getName() { + return name; + } + + public ValueExtractor getValueExtractor() { + return valueExtractor; + } + + public boolean isEscape() { + return escape; + } + + public void setEscape(boolean escape) { + this.escape = escape; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + CharSequence value = extractValue(valueExtractor, req, resp, context); + if (value != null) { + doProcess(value, req, resp, context); + } + } + + protected abstract void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context); + + } + + public static class RemoveHeaderOperation extends AbstractRemoveOperation { + + public RemoveHeaderOperation(CharSequence name) { + super(name); + } + + public RemoveHeaderOperation(ValueExtractor extractor) { + super(extractor); + } + + @Override + protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.getHeaders().delete(name); + } + + } + + public static class AddHeaderOperation extends AbstractNameValueOperation { + + public AddHeaderOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public AddHeaderOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.getHeaders().add(name, value); + } + + } + + public static class SetHeaderOperation extends AbstractNameValueOperation { + + public SetHeaderOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public SetHeaderOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.getHeaders().set(name, value); + } + + } + + public static class RemoveCookieOperation extends AbstractRemoveOperation { + + public RemoveCookieOperation(CharSequence name) { + super(name); + } + + public RemoveCookieOperation(ValueExtractor extractor) { + super(extractor); + } + + @Override + protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + HttpMessageHelper.get().removeCookie(req, name); + } + + } + + public static class AddCookieOperation extends AbstractNameValueOperation { + + public AddCookieOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public AddCookieOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + HttpMessageHelper.get().addCookie(req, name, value); + } + + } + + public static class SetCookieOperation extends AbstractNameValueOperation { + + public SetCookieOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public SetCookieOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + HttpMessageHelper.get().setCookie(req, name, value); + } + + } + + public static class RemoveQueryParameterOperation extends AbstractRemoveOperation { + + public RemoveQueryParameterOperation(CharSequence paramName) { + super(paramName); + } + + public RemoveQueryParameterOperation(ValueExtractor extractor) { + super(extractor); + } + + @Override + protected void doProcess(CharSequence name, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper.get().removeQueryParameter(req, name); + } catch (ParseException ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + + } + + public static class AddQueryParameterOperation extends AbstractNameValueOperation { + + public AddQueryParameterOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public AddQueryParameterOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper.get().addQueryParameter(req, name, value, escape); + } catch (ParseException ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + + } + + public static class SetQueryParameterOperation extends AbstractNameValueOperation { + + public SetQueryParameterOperation(CharSequence name, CharSequence value) { + super(name, value); + } + + public SetQueryParameterOperation(CharSequence name, ValueExtractor valueExtractor) { + super(name, valueExtractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper.get().setQueryParameter(req, name, value, escape); + } catch (ParseException ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + + } + + protected static abstract class RemoveParamOperation extends AbstractRemoveOperation { + + public RemoveParamOperation(CharSequence name) { + super(name); + } + + public RemoveParamOperation(ValueExtractor extractor) { + super(extractor); + } + + protected boolean doRemove(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + CharSequence name = extractValue(nameExtractor, req, resp, context); + if (name != null) { + return params.remove(name); + } + + return false; + } + + } + + protected static abstract class AddParamOperation extends Operation { + + protected final ByteString name; + + protected final ValueExtractor valueExtractor; + + protected boolean escape = true; + + public AddParamOperation(CharSequence name, Object value) { + Assert.notNull(name, "name"); + Assert.notNull(value, "value"); + + if (value instanceof CharSequence) { + this.valueExtractor = new UnmutableValueExtractor(value); + } else if (value instanceof List) { + for (Object val : (List) value) { + if (!(val instanceof CharSequence)) { + throw new IllegalArgumentException("Value should be CharSequence or List<CharSequence>."); + } + } + + this.valueExtractor = new UnmutableValueExtractor(value); + } else { + throw new IllegalArgumentException("Value should be CharSequence or List<CharSequence>."); + } + + this.name = ByteString.create(name); + } + + public AddParamOperation(CharSequence name, ValueExtractor extractor) { + Assert.notNull(name, "name"); + Assert.notNull(extractor, "extractor"); + this.name = ByteString.create(name); + this.valueExtractor = extractor; + } + + public boolean isEscape() { + return escape; + } + + public void setEscape(boolean escape) { + this.escape = escape; + } + + protected void doAdd(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); + try { + if (value instanceof CharSequence) { + params.add(name, (CharSequence) value); + } else if (value instanceof List) { + params.add(name, (List<CharSequence>) value); + } + } catch (Exception ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + + } + + } + + private static abstract class SetParamOperation extends AddParamOperation { + + public SetParamOperation(CharSequence name, Object value) { + this(name, new UnmutableValueExtractor(value)); + } + + public SetParamOperation(CharSequence name, ValueExtractor extractor) { + super(name, extractor); + } + + protected void doSet(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + params.remove(name); + doAdd(params, req, resp, context); + } + } + + public static class PostDataRemoveParamOperation extends RemoveParamOperation { + + public PostDataRemoveParamOperation(CharSequence name) { + super(name); + } + + public PostDataRemoveParamOperation(ValueExtractor extractor) { + super(extractor); + } + + @Override + protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper helper = HttpMessageHelper.get(); + + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req); + if (params != null) { + if (doRemove(params, req, resp, context)) { + helper.setFormUrlencoded(req, params); + } + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + CharSequence name = extractValue(nameExtractor, req, resp, context); + HttpDataPart part = parts.findByContentDispositionName(name); + if (part != null) { + parts.remove(part); + helper.setMultipartContent(req, parts); + } + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + } + + public static class PostDataAddParamOperation extends AddParamOperation { + + public PostDataAddParamOperation(CharSequence name, Object value) { + super(name, value); + } + + public PostDataAddParamOperation(CharSequence name, ValueExtractor extractor) { + super(name, extractor); + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper helper = HttpMessageHelper.get(); + + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req, escape); + if (params != null) { + doAdd(params, req, resp, context); + helper.setFormUrlencoded(req, params, escape); + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); + parts.addFormData(this.name, value); + helper.setMultipartContent(req, parts); + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + } + + public static class PostDataSetParamOperation extends SetParamOperation { + + public PostDataSetParamOperation(CharSequence name, Object value) { + super(name, value); + } + + public PostDataSetParamOperation(CharSequence name, ValueExtractor extractor) { + super(name, extractor); + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + HttpMessageHelper helper = HttpMessageHelper.get(); + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req, escape); + if (params != null) { + doSet(params, req, resp, context); + helper.setFormUrlencoded(req, params, escape); + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); + HttpDataPart part = parts.findByContentDispositionName(this.name); + if (part == null) { + parts.addFormData(this.name, value); + } else { + part.setContent(helper.toDataSource(value)); + } + + helper.setMultipartContent(req, parts); + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + + } + + public static class ContentOperation extends Operation { + + private final ContentReplacer replacer; + + private final CharSequence newValue; + + public ContentOperation(ContentReplacer replacer, CharSequence newValue) { + this.replacer = replacer; + this.newValue = newValue; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + if (req == null) { + return; + } + + try { + replacer.replace(req, newValue); + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + + } + +}
--- a/stress-tester/src/test/java/com/passus/st/ConverterHttpClientTest.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/ConverterHttpClientTest.java Fri Apr 27 10:30:37 2018 +0200 @@ -11,7 +11,6 @@ import com.passus.net.http.HttpResponse; import com.passus.net.http.HttpResponseBuilder; import com.passus.net.http.HttpStatus; -import com.passus.st.client.Event; import com.passus.st.client.credentials.Credentials; import com.passus.st.client.credentials.CredentialsProvider; import static com.passus.st.client.http.HttpConsts.PARAM_USERNAME; @@ -20,16 +19,12 @@ import static com.passus.st.client.http.HttpConsts.TAG_ZONE; import com.passus.st.client.http.HttpSessionPayloadEvent; import com.passus.st.client.http.filter.*; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataSetParamOperation; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetCookieOperation; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetHeaderOperation; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetQueryParameterOperation; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import com.passus.st.emitter.SessionInfo; import com.passus.st.source.ArrayListEventDestination; import com.passus.st.source.NullEventDestination; import com.passus.st.utils.TestHttpUtils; import java.io.IOException; -import java.util.Arrays; import java.util.List; import org.apache.logging.log4j.Level; import static org.testng.AssertJUnit.*;
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFiltersNodeDefinitionCreatorTest.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFiltersNodeDefinitionCreatorTest.java Fri Apr 27 10:30:37 2018 +0200 @@ -10,7 +10,7 @@ import com.passus.config.YamlConfigurationReader; import com.passus.config.schema.NodeDefinition; import com.passus.config.validation.Errors; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.*; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import java.io.File; import java.util.List; import static org.testng.AssertJUnit.*;
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Fri Apr 27 10:30:37 2018 +0200 @@ -11,7 +11,7 @@ import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID; import com.passus.st.client.http.HttpFlowContext; import com.passus.st.client.http.extractor.ContentExtractorUtils; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.*; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import static com.passus.st.client.http.filter.HttpVarsFilterTest.expr; import static com.passus.st.client.http.filter.HttpVarsFilterTest.val; import com.passus.st.utils.ConfigurationContextConsts;
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Thu Apr 26 13:56:09 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Fri Apr 27 10:30:37 2018 +0200 @@ -8,7 +8,7 @@ import com.passus.lookup.Lookup; import com.passus.lookup.LookupList; import com.passus.lookup.filter.LookupValueExtractor; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.*; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import com.passus.st.utils.ConfigurationContextConsts; import org.testng.annotations.Test;