Author: veithen
Date: Tue Jan  6 10:42:29 2009
New Revision: 732047

URL: http://svn.apache.org/viewvc?rev=732047&view=rev
Log:
Refactoring and optimization of HttpRequestFilter.

Added:
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/AbstractHttpRequestHandler.java
   (with props)
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestHandler.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractConnection.java
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpHeaderRewriter.java
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyClientHandler.java
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyServerHandler.java
    
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java

Modified: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractConnection.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractConnection.java?rev=732047&r1=732046&r2=732047&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractConnection.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractConnection.java
 Tue Jan  6 10:42:29 2009
@@ -22,6 +22,7 @@
 import org.apache.ws.commons.tcpmon.core.filter.HttpHeaderRewriter;
 import org.apache.ws.commons.tcpmon.core.filter.HttpProxyClientHandler;
 import org.apache.ws.commons.tcpmon.core.filter.HttpProxyServerHandler;
+import org.apache.ws.commons.tcpmon.core.filter.HttpRequestFilter;
 import org.apache.ws.commons.tcpmon.core.filter.Pipeline;
 import org.apache.ws.commons.tcpmon.core.filter.RequestLineExtractor;
 import org.apache.ws.commons.tcpmon.core.filter.StreamException;
@@ -152,8 +153,10 @@
                     setRequest(requestLine);
                 }
             });
+            HttpRequestFilter requestFilter = new HttpRequestFilter();
+            requestPipeline.addFilter(requestFilter);
             if (config.isProxy()) {
-                requestPipeline.addFilter(new HttpProxyServerHandler() {
+                requestFilter.addHandler(new HttpProxyServerHandler() {
                     protected void handleConnection(String host, int port) {
                         try {
                             outSocket = new Socket(host, port);
@@ -163,10 +166,10 @@
                     }
                 });
             } else if (HTTPProxyHost != null) {
-                requestPipeline.addFilter(new 
HttpProxyClientHandler(targetHost, targetPort));
+                requestFilter.addHandler(new 
HttpProxyClientHandler(targetHost, targetPort));
                 outSocket = new Socket(HTTPProxyHost, HTTPProxyPort);
             } else {
-                requestPipeline.addFilter(new HttpHeaderRewriter("Host", 
targetHost + ":" + targetPort));
+                requestFilter.addHandler(new HttpHeaderRewriter("Host", 
targetHost + ":" + targetPort));
                 outSocket = new Socket(targetHost, targetPort);
             }
             requestPipeline.addFilter(config.getSlowLink());

Added: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/AbstractHttpRequestHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/AbstractHttpRequestHandler.java?rev=732047&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/AbstractHttpRequestHandler.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/AbstractHttpRequestHandler.java
 Tue Jan  6 10:42:29 2009
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+/**
+ * Abstract implementation of {...@link HttpRequestHandler} with default 
behavior.
+ */
+public abstract class AbstractHttpRequestHandler implements HttpRequestHandler 
{
+    public String processRequest(String request) {
+        return request;
+    }
+
+    public String processHeader(String name, String value) {
+        return value;
+    }
+}

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

Modified: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpHeaderRewriter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpHeaderRewriter.java?rev=732047&r1=732046&r2=732047&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpHeaderRewriter.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpHeaderRewriter.java
 Tue Jan  6 10:42:29 2009
@@ -17,9 +17,9 @@
 package org.apache.ws.commons.tcpmon.core.filter;
 
 /**
- * Filter that replaces the value of a given HTTP header.
+ * Handler that replaces the value of a given HTTP header.
  */
-public class HttpHeaderRewriter extends HttpRequestFilter {
+public class HttpHeaderRewriter extends AbstractHttpRequestHandler {
     private final String headerName;
     private final String newValue;
     
@@ -28,7 +28,7 @@
         this.newValue = newValue;
     }
 
-    protected String processHeader(String name, String value) {
+    public String processHeader(String name, String value) {
         return headerName.equals(name) ? newValue : value;
     }
 }

Modified: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyClientHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyClientHandler.java?rev=732047&r1=732046&r2=732047&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyClientHandler.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyClientHandler.java
 Tue Jan  6 10:42:29 2009
@@ -17,9 +17,9 @@
 package org.apache.ws.commons.tcpmon.core.filter;
 
 /**
- * Filter that rewrites a plain HTTP request to an HTTP proxy request.
+ * Handler that rewrites a plain HTTP request to an HTTP proxy request.
  */
-public class HttpProxyClientHandler extends HttpRequestFilter {
+public class HttpProxyClientHandler extends AbstractHttpRequestHandler {
     private final String targetHost;
     private final int targetPort;
     
@@ -28,7 +28,7 @@
         this.targetPort = targetPort;
     }
     
-    protected String processRequest(String request) {
+    public String processRequest(String request) {
         String[] parts = request.split(" ");
         return parts[0] + " http://"; + targetHost + ":" + targetPort + 
parts[1] + " " + parts[2];
     }

Modified: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyServerHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyServerHandler.java?rev=732047&r1=732046&r2=732047&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyServerHandler.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpProxyServerHandler.java
 Tue Jan  6 10:42:29 2009
@@ -20,10 +20,10 @@
 import java.net.URL;
 
 /**
- * Filter that rewrites an HTTP proxy request to a plain HTTP request.
+ * Handler that rewrites an HTTP proxy request to a plain HTTP request.
  */
-public abstract class HttpProxyServerHandler extends HttpRequestFilter {
-    protected String processRequest(String request) {
+public abstract class HttpProxyServerHandler extends 
AbstractHttpRequestHandler {
+    public String processRequest(String request) {
         String[] parts = request.split(" ");
         URL url;
         try {

Modified: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java?rev=732047&r1=732046&r2=732047&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java
 Tue Jan  6 10:42:29 2009
@@ -17,17 +17,26 @@
 package org.apache.ws.commons.tcpmon.core.filter;
 
 import java.io.UnsupportedEncodingException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 
 /**
- * Abstract filter that allows HTTP request rewriting.
+ * Filter that parses HTTP requests and invokes a set of {...@link 
HTTPRequestHandler}
+ * implementation.
  */
-public abstract class HttpRequestFilter implements StreamFilter {
+public class HttpRequestFilter implements StreamFilter {
     private static final int STATE_REQUEST = 0;
     private static final int STATE_HEADER = 1;
     private static final int STATE_DONE = 2;
     
+    private List handlers = new LinkedList();
     private int state = STATE_REQUEST;
-    
+
+    public void addHandler(HttpRequestHandler handler) {
+        handlers.add(handler);
+    }
+
     public void invoke(Stream stream) {
         while (stream.available() > 0) {
             switch (state) {
@@ -102,11 +111,20 @@
         stream.insert(b, 0, b.length);
     }
     
-    protected String processRequest(String request) {
+    private String processRequest(String request) {
+        for (Iterator it = handlers.iterator(); it.hasNext(); ) {
+            request = ((HttpRequestHandler)it.next()).processRequest(request);
+        }
         return request;
     }
     
-    protected String processHeader(String name, String value) {
+    private String processHeader(String name, String value) {
+        for (Iterator it = handlers.iterator(); it.hasNext(); ) {
+            value = ((HttpRequestHandler)it.next()).processHeader(name, value);
+            if (value == null) {
+                break;
+            }
+        }
         return value;
     }
 }

Added: 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestHandler.java?rev=732047&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestHandler.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestHandler.java
 Tue Jan  6 10:42:29 2009
@@ -0,0 +1,25 @@
+/*
+ * 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;
+
+/**
+ * Interface implemented by handlers invoked by {...@link HttpRequestFilter}.
+ */
+public interface HttpRequestHandler {
+    String processRequest(String request);
+    String processHeader(String name, String value);
+}

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


Reply via email to