You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2009/06/01 15:32:46 UTC

svn commit: r780646 - in /webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src: main/java/org/apache/ws/commons/tcpmon/core/filter/ test/java/org/apache/ws/commons/tcpmon/core/filter/

Author: veithen
Date: Mon Jun  1 13:32:46 2009
New Revision: 780646

URL: http://svn.apache.org/viewvc?rev=780646&view=rev
Log:
Added a ReplaceFilter implementation.

Added:
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java   (with props)
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java   (with props)
Modified:
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java

Added: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java?rev=780646&view=auto
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java (added)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java Mon Jun  1 13:32:46 2009
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ws.commons.tcpmon.core.filter;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Filter that replaces content in a stream.
+ */
+public class ReplaceFilter implements StreamFilter {
+    private final byte[][] patterns;
+    private final byte[][] replacements;
+    
+    public ReplaceFilter(byte[][] patterns, byte[][] replacements) {
+        this.patterns = patterns;
+        this.replacements = replacements;
+    }
+    
+    public ReplaceFilter(byte[] pattern, byte[] replacement) {
+        this(new byte[][] { pattern }, new byte[][] { replacement });
+    }
+    
+    public ReplaceFilter(String pattern, String replacement, String charsetName) throws UnsupportedEncodingException {
+        this(pattern.getBytes(charsetName), replacement.getBytes(charsetName));
+    }
+
+    public void invoke(Stream stream) {
+        int p;
+        while ((p = StreamUtil.search(stream, patterns)) != -1) {
+            stream.discard(patterns[p].length);
+            byte[] replacement = replacements[p];
+            stream.insert(replacement, 0, replacement.length);
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java?rev=780646&r1=780645&r2=780646&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java (original)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java Mon Jun  1 13:32:46 2009
@@ -65,6 +65,16 @@
         stream.insert(b, 0, b.length);
     }
     
+    /**
+     * Search the stream for occurrences of given patterns.
+     * If an occurrence is found, the method will skip all content in the stream
+     * before the position where the pattern was found. Otherwise it will skip
+     * as much content as possible.
+     * 
+     * @param stream the stream to search
+     * @param patterns the patterns to search for
+     * @return the index of the pattern that matches, or -1 if no occurrence has been found
+     */
     public static int search(Stream stream, byte[][] patterns) {
         int[] matchLengths = new int[patterns.length];
         for (int i=0; i<stream.available(); i++) {

Added: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java?rev=780646&view=auto
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java (added)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java Mon Jun  1 13:32:46 2009
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ws.commons.tcpmon.core.filter;
+
+import junit.framework.TestCase;
+
+public class ReplaceFilterTest extends TestCase {
+    public void test() throws Exception {
+        assertEquals("test-replacement-test-replacement-test",
+                TestUtil.filter(new ReplaceFilter("pattern", "replacement", "ascii"),
+                                "test-pattern-test-pattern-test"));
+    }
+}

Propchange: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/ReplaceFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native