Mercurial > stress-tester
changeset 1279:895cbec4d75c
HttpDumperFilter - configuration + improvements
author | Devel 2 |
---|---|
date | Thu, 09 Jul 2020 11:01:52 +0200 |
parents | d07d00c530e8 |
children | f5ba6579b526 |
files | stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpDumper.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpDumperFilter.java stress-tester/src/test/java/com/passus/st/client/http/HttpMessageWriterTest.java |
diffstat | 4 files changed, 137 insertions(+), 103 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java Thu Jul 09 10:41:37 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java Thu Jul 09 11:01:52 2020 +0200 @@ -6,15 +6,15 @@ import com.passus.net.http.HttpMessageEncoder; import com.passus.net.http.HttpRequestEncoder; import com.passus.net.http.HttpResponseEncoder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; /** - * * @author mikolaj.podbielski */ public class HttpMessageWriter { @@ -55,7 +55,9 @@ LOGGER.warn("File '{}' already exists.", path); } write(message, file); - LOGGER.trace("File '{}' written", path); + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("File '{}' written", path); + } } catch (IOException ex) { LOGGER.debug("Could not save file '{}'.", path); }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpDumper.java Thu Jul 09 10:41:37 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -package com.passus.st.client.http.filter; - -import com.passus.commons.annotations.Plugin; -import com.passus.commons.utils.FormatUtils; -import com.passus.net.http.HttpRequest; -import com.passus.net.http.HttpResponse; -import com.passus.st.client.FlowContext; -import com.passus.st.client.http.HttpMessageWriter; -import com.passus.st.plugin.PluginConstants; - -import java.io.File; - -/** - * Dumps requests and responses to file. - * <p> - * This filter should be added as first filter in chain. Otherwise it will dump - * partially processed request instead of original one. - * - * @author mikolaj.podbielski - */ -@Plugin(name = HttpDumper.TYPE, category = PluginConstants.CATEGORY_FLOW_FILTER) -public class HttpDumper extends HttpFilter { - - public static final String TYPE = "httpDumper"; - - private static final int MSG_REQ_ORIG = 0; - private static final int MSG_REQ_PROC = 1; - private static final int MSG_RESP_ORIG = 2; - private static final int MSG_RESP_REAL = 3; - - private static final String[] NAMES = { - "_req_orig", "_req_proc", "_resp_orig", "_resp_real" - }; - - private final HttpMessageWriter writer = new HttpMessageWriter(); - - private final File directory; - private final String prefix; - private final String suffix; - - private int index = 0; - - private int workerIndex = -1; - - public HttpDumper(File directory) { - this.directory = directory; - this.prefix = ""; - this.suffix = ""; - } - - public HttpDumper(File directory, String prefix, String suffix) { - this.directory = directory; - this.prefix = prefix != null ? prefix : ""; - this.suffix = suffix != null ? suffix : ""; - } - - public void setDecodeContent(boolean decode) { - writer.setDecodeContent(decode); - } - - @Override - public int filterOutbound(HttpRequest request, HttpResponse resp, FlowContext context) { - index++; - writer.writeSilently(request, getFile(MSG_REQ_ORIG)); - writer.writeSilently(resp, getFile(MSG_RESP_ORIG)); - return DUNNO; - } - - @Override - public int filterInbound(HttpRequest request, HttpResponse resp, FlowContext context) { - writer.writeSilently(request, getFile(MSG_REQ_PROC)); - writer.writeSilently(resp, getFile(MSG_RESP_REAL)); - return DUNNO; - } - - File getFile(int msgType) { - StringBuilder sb = new StringBuilder(); - sb.append(prefix); - if (workerIndex != -1) { - sb.append("w").append(workerIndex).append('_'); - } - sb.append(FormatUtils.padStart(String.valueOf(index), 3, '0')); - sb.append(NAMES[msgType]); - sb.append(suffix); - - String filename = sb.toString(); - return new File(directory, filename); - } - - @Override - public HttpFilter instanceForWorker(int index) { - HttpDumper instance = new HttpDumper(directory, prefix, suffix); - instance.workerIndex = index; - return instance; - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpDumperFilter.java Thu Jul 09 11:01:52 2020 +0200 @@ -0,0 +1,131 @@ +package com.passus.st.client.http.filter; + +import com.passus.commons.annotations.Plugin; +import com.passus.commons.utils.FormatUtils; +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.ConfigurationException; +import com.passus.config.annotations.NodeDefinitionCreate; +import com.passus.config.schema.NodeDefinition; +import com.passus.config.schema.NodeDefinitionCreator; +import com.passus.net.http.HttpRequest; +import com.passus.net.http.HttpResponse; +import com.passus.st.client.FlowContext; +import com.passus.st.client.http.HttpMessageWriter; +import com.passus.st.config.FileTransformer; +import com.passus.st.filter.FilterDirection; +import com.passus.st.plugin.PluginConstants; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; + +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; + +/** + * Dumps requests and responses to file. + * <p> + * This filter should be added as first filter in chain. Otherwise it will dump + * partially processed request instead of original one. + * + * @author mikolaj.podbielski + */ +@NodeDefinitionCreate(HttpDumperFilter.HttpDumperFilterNodeDefCreator.class) +@Plugin(name = HttpDumperFilter.TYPE, category = PluginConstants.CATEGORY_FLOW_FILTER) +public class HttpDumperFilter extends HttpFilter { + + public static final String TYPE = "httpDumper"; + + private static final int MSG_REQ_ORIG = 0; + private static final int MSG_REQ_PROC = 1; + private static final int MSG_RESP_ORIG = 2; + private static final int MSG_RESP_REAL = 3; + + private static final String[] NAMES = { + "_req_orig", "_req_proc", "_resp_orig", "_resp_real" + }; + + private final HttpMessageWriter writer = new HttpMessageWriter(); + + private File directory; + + private int workerIndex = -1; + + private FilterDirection direction = FilterDirection.IN; + + public HttpDumperFilter() { + } + + public HttpDumperFilter(File directory) { + this.directory = directory; + } + + public void setDecodeContent(boolean decode) { + writer.setDecodeContent(decode); + } + + @Override + public void configure(Configuration config, ConfigurationContext context) { + direction = config.get("dir", FilterDirection.IN); + directory = (File) config.get("directory"); + if (!directory.exists()) { + try { + FileUtils.forceMkdir(directory); + } catch (IOException e) { + throw new ConfigurationException(e.getMessage(), e); + } + } + } + + @Override + public int filterOutbound(HttpRequest req, HttpResponse resp, FlowContext context) { + if (direction == FilterDirection.BOTH || direction == FilterDirection.OUT) { + writer.writeSilently(req, getFile(MSG_REQ_ORIG, req.getId())); + if (resp != null) writer.writeSilently(resp, getFile(MSG_RESP_ORIG, req.getId())); + } + + return DUNNO; + } + + @Override + public int filterInbound(HttpRequest req, HttpResponse resp, FlowContext context) { + if (direction == FilterDirection.BOTH || direction == FilterDirection.IN) { + writer.writeSilently(req, getFile(MSG_REQ_PROC, req.getId())); + if (resp != null) writer.writeSilently(resp, getFile(MSG_RESP_REAL, req.getId())); + } + return DUNNO; + } + + File getFile(int msgType, String reqId) { + StringBuilder sb = new StringBuilder(); + sb.append(reqId).append("_"); + if (workerIndex != -1) { + sb.append("w").append(workerIndex).append('_'); + } + + sb.append(NAMES[msgType]); + + String filename = sb.toString(); + return new File(directory, filename); + } + + @Override + public HttpFilter instanceForWorker(int index) { + HttpDumperFilter instance = new HttpDumperFilter(directory); + instance.workerIndex = index; + return instance; + } + + public static class HttpDumperFilterNodeDefCreator implements NodeDefinitionCreator { + + @Override + public NodeDefinition create() { + return mapDef( + tupleDef("dir", enumDef(FilterDirection.class)).setRequired(false), + tupleDef("directory", valueDef().setTransformer(new FileTransformer())) + ); + } + + } + +}
--- a/stress-tester/src/test/java/com/passus/st/client/http/HttpMessageWriterTest.java Thu Jul 09 10:41:37 2020 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/HttpMessageWriterTest.java Thu Jul 09 11:01:52 2020 +0200 @@ -6,11 +6,9 @@ package com.passus.st.client.http; import com.passus.net.http.HttpRequest; -import com.passus.st.client.http.filter.HttpDumper; import com.passus.st.client.http.filter.HttpFormLoginFilterTest; import com.passus.st.utils.TestHttpUtils; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files;