changeset 780:6ae3b6a8bd5d

ReaderMain - filter option
author Devel 2
date Tue, 19 Dec 2017 13:30:43 +0100
parents 1d0369d5006b
children 80b01b797330
files stress-tester/src/main/java/com/passus/st/ReaderMain.java
diffstat 1 files changed, 29 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Tue Dec 19 12:28:25 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Tue Dec 19 13:30:43 2017 +0100
@@ -8,7 +8,9 @@
 import com.passus.st.client.Event;
 import com.passus.st.client.SessionStatusEvent;
 import com.passus.st.client.http.HttpSessionPayloadEvent;
+import com.passus.st.client.http.filter.HttpMessagePredicate;
 import com.passus.st.emitter.SessionInfo;
+import com.passus.st.filter.Transformers;
 import com.passus.st.source.EventSource;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
@@ -16,6 +18,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.util.function.Predicate;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.HelpFormatter;
@@ -30,8 +33,11 @@
 public class ReaderMain {
 
     private Logger logger;
+
     private final CliHelper cliHelper = new CliHelper();
 
+    private HttpMessagePredicate filter;
+
     private void printHelp(Options options) {
         HelpFormatter formatter = new HelpFormatter();
         formatter.printHelp("[options] <file>", "description", options, "");
@@ -44,7 +50,12 @@
         options.addAllowPartialSessionOption();
         options.addHttpPortsOption();
 
-        options.addOption(option("f", "file").desc("Write output to file.")
+        options.addOption(option("f", "filter").desc("Filter out messages.")
+                .hasArg().argName("filter")
+                .build()
+        );
+
+        options.addOption(option("w", "file").desc("Write output to file.")
                 .hasArg().argName("file")
                 .build()
         );
@@ -65,10 +76,15 @@
             ps.println(SessionStatusEvent.statusToString(statusEvent.getStatus()));
         } else if (event.getType() == HttpSessionPayloadEvent.TYPE) {
             HttpSessionPayloadEvent payloadEvent = (HttpSessionPayloadEvent) event;
+            HttpRequest req = payloadEvent.getRequest();
+            HttpResponse resp = payloadEvent.getResponse();
+            if (filter != null && !filter.test(req, resp, null)) {
+                return;
+            }
+
             printSessionInfo(payloadEvent.getSessionInfo(), ps);
             ps.println(" payload: ");
 
-            HttpRequest req = payloadEvent.getRequest();
             if (req != null) {
                 try {
                     HttpRequestEncoder.INSTANCE.encode(req, ps);
@@ -80,7 +96,6 @@
                 }
             }
 
-            HttpResponse resp = payloadEvent.getResponse();
             if (resp != null) {
                 try {
                     HttpResponseEncoder.INSTANCE.encode(resp, ps);
@@ -135,8 +150,8 @@
             EventSource eventSource = cliHelper.createEventSource(clArgs[0], cl);
             PrintStream ps = System.out;
             File outFile = null;
-            if (cl.hasOption("f")) {
-                outFile = new File(cl.getOptionValue("f"));
+            if (cl.hasOption("w")) {
+                outFile = new File(cl.getOptionValue("w"));
                 if (outFile.exists()) {
                     outFile.delete();
                 }
@@ -144,6 +159,15 @@
                 ps = new PrintStream(outFile);
             }
 
+            if (cl.hasOption("f")) {
+                try {
+                    Predicate predicate = Transformers.PREDICATE.transform(cl.getOptionValue("f"));
+                    filter = new HttpMessagePredicate(predicate);
+                } catch (Exception e) {
+                    cliHelper.printError("Invalid filter expression. " + e.getMessage());
+                }
+            }
+
             try {
                 if (eventSource instanceof NcEventSource) {
                     readNcFile((NcEventSource) eventSource, ps);