changeset 771:8bac181dfdfb

ConverterMain - request, response write mode
author Devel 2
date Mon, 18 Dec 2017 11:58:39 +0100
parents fdff2d086ce5
children b2e54c963007
files stress-tester/src/main/java/com/passus/st/ConverterMain.java stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java stress-tester/src/main/java/com/passus/st/reader/nc/HttpWriteMode.java stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java
diffstat 5 files changed, 80 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ConverterMain.java	Mon Dec 18 11:09:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/ConverterMain.java	Mon Dec 18 11:58:39 2017 +0100
@@ -1,13 +1,6 @@
 package com.passus.st;
 
-import com.passus.config.Configuration;
-import com.passus.config.YamlConfigurationReader;
-import com.passus.config.validation.Errors;
-import static com.passus.config.validation.ErrorsUtils.objectErrorToString;
-import com.passus.config.validation.ObjectError;
-import com.passus.net.PortRangeSet;
 import com.passus.st.client.http.filter.HttpFilter;
-import com.passus.st.client.http.filter.HttpFiltersConfigurator;
 import com.passus.st.reader.nc.HttpWriteMode;
 import com.passus.st.source.NcEventDestination;
 import com.passus.st.source.PcapSessionEventSource;
@@ -28,41 +21,23 @@
 
     private final CliHelper cliHelper = new CliHelper();
 
-    private static final String WRITE_MODES
-            = "1|req|reqonly|req-only | "
-            + "2|rrh|reqresphdr|req-resp-hdr | "
-            + "3|rr|reqresp|req-resp (default) | "
-            + "0|mix|mixed|dyn|dynamic (not implemented)";
+    private static final String WRITE_MODES = "skip|headers-only|full-message";
 
-    private static HttpWriteMode resolveWriteMode(String raw) {
+    private static HttpWriteMode resolveWriteMode(String raw, HttpWriteMode defaultValue) {
         if (raw == null) {
-            return HttpWriteMode.defaultMode();
+            return defaultValue;
         }
+
         raw = raw.toLowerCase();
         switch (raw) {
-            case "0":
-            case "mix":
-            case "mixed":
-            case "dyn":
-            case "dynamic":
-                return HttpWriteMode.REQUEST_FULL_RESPONSE_IF_TAGGED;
-            case "1":
-            case "req":
-            case "reqonly":
-            case "req-only":
-                return HttpWriteMode.REQUEST_FULL_RESPONSE_NO;
-            case "2":
-            case "rrh":
-            case "reqresphdr":
-            case "req-resp-hdr":
-                return HttpWriteMode.REQUEST_FULL_RESPONSE_HEADERS;
-            case "3":
-            case "rr":
-            case "reqresp":
-            case "req-resp":
-                return HttpWriteMode.REQUEST_FULL_RESPONSE_FULL;
+            case "skip":
+                return HttpWriteMode.SKIP;
+            case "headers-only":
+                return HttpWriteMode.HEADERS_ONLY;
+            case "full-message":
+                return HttpWriteMode.FULL_MESSAGE;
             default:
-                throw new IllegalArgumentException("Invalid write mode: " + raw);
+                throw new IllegalArgumentException("Invalid write mode '" + raw + "'.");
         }
     }
 
@@ -96,11 +71,17 @@
                 .build()
         );
 
+        
 //        options.addOption(option("wm", "writeMetrics").desc("Write metrics to file.")
 //                .hasArg().argName("file")
 //                .build()
 //        );
-        options.addOption(option("hwm", "http-write-mode").desc(WRITE_MODES)
+        options.addOption(option("hreqpwm", "http-request-write-mode").desc(WRITE_MODES + " (default full-message)")
+                .hasArg().argName("mode")
+                .build()
+        );
+
+        options.addOption(option("hrespwm", "http-response-write-mode").desc(WRITE_MODES + " (default headers-only)")
                 .hasArg().argName("mode")
                 .build()
         );
@@ -144,7 +125,8 @@
 
             NcEventDestination dst = new NcEventDestination(output);
             dst.setAllowOverwrite(overwrite);
-            dst.setHttpWriteMode(resolveWriteMode(cl.getOptionValue("hwm")));
+            dst.setHttpRequestWriteMode(resolveWriteMode(cl.getOptionValue("hreqpwm"), HttpWriteMode.FULL_MESSAGE));
+            dst.setHttpResponseWriteMode(resolveWriteMode(cl.getOptionValue("hrespwm"), HttpWriteMode.HEADERS_ONLY));
 
             ConverterHttpClient client = new ConverterHttpClient(dst);
 
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java	Mon Dec 18 11:09:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java	Mon Dec 18 11:58:39 2017 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.reader.nc;
 
+import com.passus.commons.Assert;
 import com.passus.data.ByteBuff;
 import com.passus.data.DataSource;
 import com.passus.data.HeapByteBuff;
@@ -25,10 +26,18 @@
 
     private final NcDataHelper ncDataHelper = NcDataHelper.getInstance();
 
-    private HttpWriteMode mode = HttpWriteMode.defaultMode();
+    private HttpWriteMode reqWriteMode = HttpWriteMode.FULL_MESSAGE;
 
-    public void setHttpWriteMode(HttpWriteMode mode) {
-        this.mode = mode;
+    private HttpWriteMode respWriteMode = HttpWriteMode.FULL_MESSAGE;
+
+    public void setRequestWriteMode(HttpWriteMode mode) {
+        Assert.notNull(mode, "mode");
+        this.reqWriteMode = mode;
+    }
+
+    public void setResponseWriteMode(HttpWriteMode mode) {
+        Assert.notNull(mode, "mode");
+        this.respWriteMode = mode;
     }
 
     public int encodeHeaders(HttpHeaders headers, ByteBuff buff) {
@@ -168,13 +177,19 @@
         byte flags = 0;
         HttpRequest req = messages.getRequest();
         DataSource reqContent = null;
-        if (req != null) {
+        if (reqWriteMode.code() > HttpWriteMode.SKIP.code()
+                && req != null) {
             flags |= FLAG_REQUEST;
             encodeRequest(req, reqBuffer);
-            reqContent = req.getContent();
-            if (reqContent != null) {
-                reqContent.available();
-                ncDataHelper.writeLongVLC(reqBuffer, reqContent.available());
+
+            if (reqWriteMode == HttpWriteMode.FULL_MESSAGE) {
+                reqContent = req.getContent();
+                if (reqContent != null) {
+                    reqContent.available();
+                    ncDataHelper.writeLongVLC(reqBuffer, reqContent.available());
+                } else {
+                    ncDataHelper.writeLongVLC(reqBuffer, 0);
+                }
             } else {
                 ncDataHelper.writeLongVLC(reqBuffer, 0);
             }
@@ -182,16 +197,20 @@
 
         HttpResponse resp = messages.getResponse();
         DataSource respContent = null;
-        boolean encodeResponse
-                = (mode == HttpWriteMode.REQUEST_FULL_RESPONSE_FULL
-                || mode == HttpWriteMode.REQUEST_FULL_RESPONSE_HEADERS);
-        if (encodeResponse && resp != null) {
+        if (respWriteMode.code() > HttpWriteMode.SKIP.code()
+                && resp != null) {
             flags |= FLAG_RESPONSE;
+
             encodeResponse(resp, respBuffer);
-            respContent = resp.getContent();
-            if (respContent != null) {
-                respContent.available();
-                ncDataHelper.writeLongVLC(respBuffer, respContent.available());
+
+            if (respWriteMode == HttpWriteMode.FULL_MESSAGE) {
+                respContent = resp.getContent();
+                if (respContent != null) {
+                    respContent.available();
+                    ncDataHelper.writeLongVLC(respBuffer, respContent.available());
+                } else {
+                    ncDataHelper.writeLongVLC(respBuffer, 0);
+                }
             } else {
                 ncDataHelper.writeLongVLC(respBuffer, 0);
             }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpWriteMode.java	Mon Dec 18 11:09:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpWriteMode.java	Mon Dec 18 11:58:39 2017 +0100
@@ -5,12 +5,23 @@
  * @author mikolaj.podbielski
  */
 public enum HttpWriteMode {
-    REQUEST_FULL_RESPONSE_NO,
-    REQUEST_FULL_RESPONSE_HEADERS,
-    REQUEST_FULL_RESPONSE_FULL,
-    REQUEST_FULL_RESPONSE_IF_TAGGED;
+
+    SKIP(0),
+    HEADERS_ONLY(1),
+    FULL_MESSAGE(2);
+    /*FULL_MESSAGE_IF_TAGGED(3); - implementacja w przyszlosci*/
+
+    private final int code;
+
+    private HttpWriteMode(int code) {
+        this.code = code;
+    }
+
+    public int code() {
+        return code;
+    }
 
     public static HttpWriteMode defaultMode() {
-        return REQUEST_FULL_RESPONSE_FULL;
+        return FULL_MESSAGE;
     }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Mon Dec 18 11:09:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Mon Dec 18 11:58:39 2017 +0100
@@ -80,9 +80,14 @@
         this.allowOverwrite = allowOverwrite;
     }
 
-    public void setHttpWriteMode(HttpWriteMode mode) {
+    public void setHttpRequestWriteMode(HttpWriteMode mode) {
         this.mode = mode;
-        httpPayloadWriter.setHttpWriteMode(mode);
+        httpPayloadWriter.setRequestWriteMode(mode);
+    }
+
+    public void setHttpResponseWriteMode(HttpWriteMode mode) {
+        this.mode = mode;
+        httpPayloadWriter.setResponseWriteMode(mode);
     }
 
     @Override
@@ -141,8 +146,8 @@
         }
 
         boolean encodeResponse
-                = (mode == HttpWriteMode.REQUEST_FULL_RESPONSE_FULL
-                || mode == HttpWriteMode.REQUEST_FULL_RESPONSE_HEADERS);
+                = (mode == HttpWriteMode.FULL_MESSAGE
+                || mode == HttpWriteMode.HEADERS_ONLY);
         if (event.getResponse() != null && encodeResponse) {
             ByteBuff buff = new HeapByteBuff();
             responseEncoder.encode(event.getResponse(), buff);
--- a/stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java	Mon Dec 18 11:09:10 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java	Mon Dec 18 11:58:39 2017 +0100
@@ -4,7 +4,6 @@
 import com.passus.data.ByteBuff;
 import com.passus.data.ByteString;
 import com.passus.data.DataSourceUtils;
-import com.passus.data.HeapByteBuff;
 import com.passus.net.http.HttpHeaders;
 import com.passus.net.http.HttpMessage;
 import com.passus.net.http.HttpRequest;