changeset 875:1bb2a1f9b982

Http listener plugins: stdout, dumper.
author Devel 2
date Fri, 26 Jan 2018 14:50:03 +0100
parents a4f8de13c0c9
children 40674a682e7f
files stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java stress-tester/src/main/java/com/passus/st/client/http/StdoutHttpClientListener.java
diffstat 2 files changed, 87 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java	Fri Jan 26 13:21:08 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java	Fri Jan 26 14:50:03 2018 +0100
@@ -1,46 +1,66 @@
 package com.passus.st.client.http;
 
+import com.passus.commons.annotations.Plugin;
 import com.passus.commons.utils.FormatUtils;
+import com.passus.config.Configurable;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.net.http.HttpMessage;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
+import com.passus.st.plugin.PluginConstants;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
 
 /**
- *
  * @author mikolaj.podbielski
  */
-public class DumperHttpClientListener implements HttpClientListener {
+@NodeDefinitionCreate(DumperHttpClientListener.DumperHttpClientListenerNodeDefCreator.class)
+@Plugin(name = DumperHttpClientListener.TYPE, category = PluginConstants.CATEGORY_HTTP_CLIENT_LISTENER)
+public class DumperHttpClientListener implements HttpClientListener, Configurable {
+
+    public static final String TYPE = "dumper";
 
     private static final Logger LOGGER = LogManager.getLogger(DumperHttpClientListener.class);
 
+    private static final String DEFAULT_PREFIX = "";
+    private static final String DEFAULT_SUFFIX = ".txt";
+
     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 = {
-        "reqOrig", "req", "respOrig", "resp"
+            "reqOrig", "req", "respOrig", "resp"
     };
 
     private final HttpMessageWriter writer = new HttpMessageWriter();
 
-    private final File directory;
-    private final String prefix;
-    private final String suffix;
+    private File directory;
+    private String prefix;
+    private String suffix;
 
     private int indexWidth = 6;
     private boolean requestsOnly;
 
     private final AtomicInteger index = new AtomicInteger();
 
+    public DumperHttpClientListener() {
+    }
+
     public DumperHttpClientListener(File parent) {
-        this(parent, "", ".txt");
+        this(parent, DEFAULT_PREFIX, DEFAULT_SUFFIX);
     }
 
     public DumperHttpClientListener(File parent, String prefix, String suffix) {
@@ -53,6 +73,33 @@
         return directory;
     }
 
+    public void setDirectory(File directory) {
+        this.directory = directory;
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+
+    public void setPrefix(String prefix) {
+        if (prefix == null) {
+            prefix = "";
+        }
+        this.prefix = prefix;
+    }
+
+    public String getSuffix() {
+        return suffix;
+    }
+
+    public void setSuffix(String suffix) {
+        if (suffix == null) {
+            suffix = "";
+        }
+
+        this.suffix = suffix;
+    }
+
     public void setIndexWidth(int indexWidth) {
         this.indexWidth = indexWidth;
     }
@@ -66,15 +113,24 @@
     }
 
     @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        setDirectory(new File(config.getString("directory")));
+        prefix = config.getString("prefix", DEFAULT_PREFIX);
+        suffix = config.getString("suffix", DEFAULT_SUFFIX);
+        setDecodeContent(config.getBoolean("decodeContent", true));
+        setRequestsOnly(config.getBoolean("requestsOnly", false));
+    }
+
+    @Override
     public void responseReceived(HttpRequest request, HttpResponse response, HttpFlowContext context) {
         String indexString = FormatUtils.padStart(String.valueOf(index.incrementAndGet()), indexWidth, '0');
         String id = request.getId();
 
-        HttpResponse origReponse = context.origReponse();
+        HttpResponse origResp = context.origReponse();
         writer.writeSilently(request, getFile(MSG_REQ_PROC, request, id, indexString));
         if (!requestsOnly) {
             writer.writeSilently(response, getFile(MSG_RESP_REAL, response, id, indexString));
-            writer.writeSilently(origReponse, getFile(MSG_RESP_ORIG, origReponse, id, indexString));
+            writer.writeSilently(origResp, getFile(MSG_RESP_ORIG, origResp, id, indexString));
         }
     }
 
@@ -101,4 +157,18 @@
         String dir = sdf.format(date);
         return new File(parent, dir);
     }
+
+    public static class DumperHttpClientListenerNodeDefCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            return mapDef(
+                    tupleDef("directory", valueDef()),
+                    tupleDef("prefix", valueDef()).setRequired(false),
+                    tupleDef("suffix", valueDef()).setRequired(false),
+                    tupleDef("decodeContent", valueDefBool()).setRequired(false),
+                    tupleDef("requestsOnly", valueDefBool()).setRequired(false)
+            );
+        }
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/StdoutHttpClientListener.java	Fri Jan 26 13:21:08 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/StdoutHttpClientListener.java	Fri Jan 26 14:50:03 2018 +0100
@@ -1,14 +1,19 @@
 package com.passus.st.client.http;
 
+import com.passus.commons.annotations.Plugin;
+import com.passus.st.plugin.PluginConstants;
+
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 
 /**
- *
  * @author Mirosław Hawrot
  */
+@Plugin(name = StdoutHttpClientListener.TYPE, category = PluginConstants.CATEGORY_HTTP_CLIENT_LISTENER)
 public class StdoutHttpClientListener extends WriterHttpClientListener {
 
+    public static final String TYPE = "stdout";
+
     public StdoutHttpClientListener() {
         super(new PrintWriter(new OutputStreamWriter(System.out)));
     }