changeset 487:c58400b7591e

RegexValueExtractor
author Devel 1
date Wed, 09 Aug 2017 16:27:29 +0200
parents b1e63257aefa
children 878dc1253eef
files stress-tester/src/main/java/com/passus/st/XmlValueValueExtractor.java stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java stress-tester/src/main/java/com/passus/st/client/http/extractor/RegexValueExtractor.java stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java stress-tester/src/test/java/com/passus/st/XmlValueValueExtractorTest.java stress-tester/src/test/java/com/passus/st/client/http/extractor/RegexValueExtractorTest.java stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java
diffstat 7 files changed, 178 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/XmlValueValueExtractor.java	Wed Aug 09 13:29:25 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-package com.passus.st;
-
-import com.passus.commons.Assert;
-import com.passus.filter.ValueExtractor;
-import java.io.StringReader;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathExpression;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.w3c.dom.Document;
-import org.xml.sax.InputSource;
-
-/**
- *
- * @author Mirosław Hawrot
- */
-public class XmlValueValueExtractor implements ValueExtractor {
-
-    public static final Logger LOGGER = LogManager.getLogger(XmlValueValueExtractor.class);
-
-    private final XPathExpression xPathExpr;
-
-    private DocumentBuilder builder;
-
-    public XmlValueValueExtractor(String expression) throws XPathExpressionException {
-        Assert.notNull(expression, "expression");
-        XPath xPath = XPathFactory.newInstance().newXPath();
-        xPathExpr = xPath.compile(expression);
-    }
-
-    public XmlValueValueExtractor(XPathExpression xPathExpr) {
-        Assert.notNull(xPathExpr, "xPathExpr");
-        this.xPathExpr = xPathExpr;
-    }
-
-    @Override
-    public Object extract(Object object) {
-        if (!(object instanceof String)) {
-            return null;
-        }
-
-        try {
-            if (builder == null) {
-                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
-                builder = builderFactory.newDocumentBuilder();
-            }
-        } catch (Exception e) {
-            if (LOGGER.isDebugEnabled()) {
-                LOGGER.debug(e.getMessage(), e);
-            }
-
-            return null;
-        }
-
-        String str = (String) object;
-        try (StringReader sr = new StringReader(str)) {
-            Document doc = builder.parse(new InputSource(sr));
-            return xPathExpr.evaluate(doc);
-        } catch (Exception e) {
-            if (LOGGER.isDebugEnabled()) {
-                LOGGER.debug(e.getMessage(), e);
-            }
-
-            return null;
-        }
-    }
-
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java	Wed Aug 09 16:27:29 2017 +0200
@@ -0,0 +1,10 @@
+package com.passus.st.client.http.extractor;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public interface ContentExtractor {
+
+    public String extract(String content);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/RegexValueExtractor.java	Wed Aug 09 16:27:29 2017 +0200
@@ -0,0 +1,34 @@
+package com.passus.st.client.http.extractor;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class RegexValueExtractor implements ContentExtractor {
+
+    private final Pattern pattern;
+
+    public RegexValueExtractor(String expression) {
+        pattern = Pattern.compile(expression);
+    }
+
+    @Override
+    public String extract(String object) {
+        String string = (String) object;
+        Matcher matcher = pattern.matcher(string);
+        if (matcher.find()) {
+            if (matcher.groupCount() >= 1) {
+                int start = matcher.start(1);
+                int end = matcher.end(1);
+                if (start != -1 && end != -1) {
+                    return string.substring(start, end);
+                }
+            }
+        }
+        return null;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java	Wed Aug 09 16:27:29 2017 +0200
@@ -0,0 +1,65 @@
+package com.passus.st.client.http.extractor;
+
+import com.passus.commons.Assert;
+import java.io.StringReader;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class XmlValueExtractor implements ContentExtractor {
+
+    public static final Logger LOGGER = LogManager.getLogger(XmlValueExtractor.class);
+
+    private final XPathExpression xPathExpr;
+
+    private DocumentBuilder builder;
+
+    public XmlValueExtractor(String expression) throws XPathExpressionException {
+        Assert.notNull(expression, "expression");
+        XPath xPath = XPathFactory.newInstance().newXPath();
+        xPathExpr = xPath.compile(expression);
+    }
+
+    public XmlValueExtractor(XPathExpression xPathExpr) {
+        Assert.notNull(xPathExpr, "xPathExpr");
+        this.xPathExpr = xPathExpr;
+    }
+
+    @Override
+    public String extract(String content) {
+        try {
+            if (builder == null) {
+                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+                builder = builderFactory.newDocumentBuilder();
+            }
+        } catch (Exception e) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug(e.getMessage(), e);
+            }
+
+            return null;
+        }
+
+        try (StringReader sr = new StringReader(content)) {
+            Document doc = builder.parse(new InputSource(sr));
+            return xPathExpr.evaluate(doc);
+        } catch (Exception e) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug(e.getMessage(), e);
+            }
+
+            return null;
+        }
+    }
+}
--- a/stress-tester/src/test/java/com/passus/st/XmlValueValueExtractorTest.java	Wed Aug 09 13:29:25 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-package com.passus.st;
-
-import static org.testng.AssertJUnit.assertEquals;
-import org.testng.annotations.Test;
-
-/**
- *
- * @author Mirosław Hawrot
- */
-public class XmlValueValueExtractorTest {
-
-    @Test
-    public void testExtract() throws Exception {
-        Log4jConfigurationFactory.enableFactory("debug");
-        String xml = "<?xml version=\"1.0\"?>"
-                + "<root>"
-                + " <node1>value1</node1>"
-                + " <node2>value2</node2>"
-                + " <node2>value3</node2>"
-                + " <node3><node4>value4</node4></node3>"
-                + "</root>";
-
-        XmlValueValueExtractor extractor = new XmlValueValueExtractor("/root/node1");
-        assertEquals("value1", extractor.extract(xml));
-
-        extractor = new XmlValueValueExtractor("/root/node2[2]");
-        assertEquals("value3", extractor.extract(xml));
-    }
-
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/RegexValueExtractorTest.java	Wed Aug 09 16:27:29 2017 +0200
@@ -0,0 +1,38 @@
+package com.passus.st.client.http.extractor;
+
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class RegexValueExtractorTest {
+
+    @Test
+    public void testExtract() {
+        String content = "<!DOCTYPE html>\n"
+                + "<html xmlns:ajxp>\n"
+                + "<head>\n"
+                + "	<title>Pydio</title>\n"
+                + "	<base href=\"/pydio/\"/>\n"
+                + "	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"
+                + "</head>\n"
+                + "<body style=\"overflow: hidden;\">\n"
+                + "	<script type=\"text/javascript\">\n"
+                + "	var startParameters = {\n"
+                + "		\"session_timeout\":1440,\n"
+                + "		\"usersEditable\":true,\n"
+                + "		\"SECURE_TOKEN\":\"a5UrXJGaMULhiejTuGeAIwlDN9gEvTUf\"\n"
+                + "	};\n"
+                + "	</script>\n"
+                + "	<div id=\"hidden_frames\" style=\"display:none;\"></div>\n"
+                + "</body>\n"
+                + "</html>";
+
+        RegexValueExtractor extractor = new RegexValueExtractor("\"SECURE_TOKEN\"[ ]*:[ ]*\"(.*?)\"");
+        String value = extractor.extract(content);
+        assertEquals("a5UrXJGaMULhiejTuGeAIwlDN9gEvTUf", value);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java	Wed Aug 09 16:27:29 2017 +0200
@@ -0,0 +1,31 @@
+package com.passus.st.client.http.extractor;
+
+import com.passus.st.Log4jConfigurationFactory;
+import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class XmlValueExtractorTest {
+
+    @Test
+    public void testExtract() throws Exception {
+        Log4jConfigurationFactory.enableFactory("debug");
+        String xml = "<?xml version=\"1.0\"?>"
+                + "<root>"
+                + " <node1>value1</node1>"
+                + " <node2>value2</node2>"
+                + " <node2>value3</node2>"
+                + " <node3><node4>value4</node4></node3>"
+                + "</root>";
+
+        XmlValueExtractor extractor = new XmlValueExtractor("/root/node1");
+        assertEquals("value1", extractor.extract(xml));
+
+        extractor = new XmlValueExtractor("/root/node2[2]");
+        assertEquals("value3", extractor.extract(xml));
+    }
+
+}