Author: veithen
Date: Sat May 30 12:57:28 2009
New Revision: 780256

URL: http://svn.apache.org/viewvc?rev=780256&view=rev
Log:
Added some test cases and fixed some minor bugs.

Added:
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Dump.java
   (with props)
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorDirectTest.java
   (with props)
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorProxyTest.java
   (with props)
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorTestBase.java
   (with props)
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/TestHttpHandler.java
   (with props)
Modified:
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Connection.java
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Interceptor.java
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorListener.java
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Pipeline.java
    
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Tee.java

Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml 
(original)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml Sat 
May 30 12:57:28 2009
@@ -41,6 +41,24 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>jetty</groupId>
+            <artifactId>jetty</artifactId>
+            <version>5.1.10</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.0-beta2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>1.4</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Modified: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Connection.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Connection.java?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Connection.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Connection.java
 Sat May 30 12:57:28 2009
@@ -47,7 +47,11 @@
      * Field inSocket
      */
     private Socket inSocket = null;
-
+    
+    private OutputStream tmpOut2;
+    
+    private Tee requestTee;
+    
     /**
      * Field outSocket
      */
@@ -77,6 +81,15 @@
         inSocket = s;
     }
 
+    void connectToTarget(String host, int port) throws IOException {
+        if (requestResponseListener != null) {
+            requestResponseListener.setTarget(host, port);
+        }
+        outSocket = config.getSocketFactory().createSocket(host, port);
+        tmpOut2 = outSocket.getOutputStream();
+        requestTee.setOutputStream(tmpOut2);
+    }
+    
     /**
      * Method run
      */
@@ -87,46 +100,50 @@
             int HTTPProxyPort = config.getHttpProxyPort();
             final SocketFactory socketFactory = config.getSocketFactory();
             String targetHost = config.getTargetHost();
-            requestResponseListener = 
listener.createRequestResponseListener(inSocket.getInetAddress().getHostName());
+            if (listener != null) {
+                requestResponseListener = 
listener.createRequestResponseListener(inSocket.getInetAddress().getHostName());
+            }
             int targetPort = config.getTargetPort();
             InputStream tmpIn1 = inSocket.getInputStream();
             OutputStream tmpOut1 = inSocket.getOutputStream();
             
             Pipeline requestPipeline = new Pipeline();
+            requestTee = new Tee();
             HttpRequestFilter requestFilter = new HttpRequestFilter(false);
             requestPipeline.addFilter(requestFilter);
             if (config.isProxy()) {
                 requestFilter.addHandler(new HttpProxyServerHandler() {
                     protected void handleConnection(String host, int port) {
-                        requestResponseListener.setTarget(host, port);
                         try {
-                            outSocket = socketFactory.createSocket(host, port);
+                            connectToTarget(host, port);
                         } catch (IOException ex) {
                             throw new StreamException(ex);
                         }
                     }
                 });
             } else {
-                requestResponseListener.setTarget(targetHost, targetPort);
                 requestFilter.addHandler(new HttpHeaderRewriter("Host", 
targetHost + ":" + targetPort));
-                outSocket = socketFactory.createSocket(targetHost, targetPort);
+                connectToTarget(targetHost, targetPort);
             }
             // We log the request data at this stage. This means that the user 
will see the request
             // as if it had been sent directly from the client to the server 
(without TCPMon or a proxy
             // in between).
-            OutputStream requestOutputStream = 
requestResponseListener.getRequestOutputStream();
-            if (requestOutputStream != null) {
-                requestPipeline.addFilter(new Tee(requestOutputStream));
+            if (requestResponseListener != null) {
+                OutputStream requestOutputStream = 
requestResponseListener.getRequestOutputStream();
+                if (requestOutputStream != null) {
+                    requestPipeline.addFilter(new Tee(requestOutputStream));
+                }
             }
             if (HTTPProxyHost != null) {
                 requestFilter.addHandler(new 
HttpProxyClientHandler(targetHost, targetPort));
                 outSocket = socketFactory.createSocket(HTTPProxyHost, 
HTTPProxyPort);
             }
             config.applyRequestFilters(requestPipeline);
-            Tee requestTee = new Tee();
             requestPipeline.addFilter(requestTee);
             
-            
requestResponseListener.setState(RequestResponseListener.STATE_ACTIVE);
+            if (requestResponseListener != null) {
+                
requestResponseListener.setState(RequestResponseListener.STATE_ACTIVE);
+            }
             
             // If we act as a proxy, we first need to read the start of the 
request before
             // the outSocket is available.
@@ -134,17 +151,16 @@
                 requestPipeline.readFrom(tmpIn1);
             }
             
-            OutputStream tmpOut2 = outSocket.getOutputStream();
-            requestTee.setOutputStream(tmpOut2);
-            
             Pipeline responsePipeline = new Pipeline();
             config.applyResponseFilters(responsePipeline);
             if (tmpOut1 != null) {
                 responsePipeline.addFilter(new Tee(tmpOut1));
             }
-            OutputStream responseOutputStream = 
requestResponseListener.getResponseOutputStream();
-            if (responseOutputStream != null) {
-                responsePipeline.addFilter(new Tee(responseOutputStream));
+            if (requestResponseListener != null) {
+                OutputStream responseOutputStream = 
requestResponseListener.getResponseOutputStream();
+                if (responseOutputStream != null) {
+                    responsePipeline.addFilter(new Tee(responseOutputStream));
+                }
             }
             
             // this is the channel to the endpoint
@@ -158,7 +174,7 @@
             
             while ((rr1 != null) || (rr2 != null)) {
 
-                if (rr2 != null) {
+                if (rr2 != null && requestResponseListener != null) {
                     requestResponseListener.setElapsed(rr2.getElapsed());
                 }
                 
@@ -168,14 +184,14 @@
                 // looping forever since no one closed the 1st one.
                 
                 if ((null != rr1) && rr1.isDone()) {
-                    if (rr2 != null) {
+                    if (rr2 != null && requestResponseListener != null) {
                         
requestResponseListener.setState(RequestResponseListener.STATE_RESP);
                     }
                     rr1 = null;
                 }
 
                 if ((null != rr2) && rr2.isDone()) {
-                    if (rr1 != null) {
+                    if (rr1 != null && requestResponseListener != null) {
                         
requestResponseListener.setState(RequestResponseListener.STATE_REQ);
                     }
                     rr2 = null;
@@ -188,8 +204,9 @@
 
             active = false;
 
-            
requestResponseListener.setState(RequestResponseListener.STATE_DONE);
-
+            if (requestResponseListener != null) {
+                
requestResponseListener.setState(RequestResponseListener.STATE_DONE);
+            }
         } catch (Exception e) {
             if (requestResponseListener != null) {
                 
requestResponseListener.setState(RequestResponseListener.STATE_ERROR);

Added: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Dump.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Dump.java?rev=780256&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Dump.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Dump.java
 Sat May 30 12:57:28 2009
@@ -0,0 +1,57 @@
+/*
+ * 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.engine;
+
+import java.io.OutputStream;
+
+public class Dump implements InterceptorListener, RequestResponseListener {
+    private final OutputStream out;
+    
+    public Dump(OutputStream out) {
+        this.out = out;
+    }
+
+    public RequestResponseListener createRequestResponseListener(String 
fromHost) {
+        return this;
+    }
+
+    public void onServerSocketError(Throwable ex) {
+    }
+
+    public void onServerSocketStart() {
+    }
+
+    public OutputStream getRequestOutputStream() {
+        return out;
+    }
+
+    public OutputStream getResponseOutputStream() {
+        return out;
+    }
+
+    public void onError(Throwable ex) {
+    }
+
+    public void setElapsed(long elapsed) {
+    }
+
+    public void setState(int state) {
+    }
+
+    public void setTarget(String targetHost, int targetPort) {
+    }
+}

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

Modified: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Interceptor.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Interceptor.java?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Interceptor.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/Interceptor.java
 Sat May 30 12:57:28 2009
@@ -43,10 +43,10 @@
     private final Vector connections = new Vector();
 
     /**
-     * Constructor SocketWaiter
-     *
-     * @param l
-     * @param p
+     * Constructor.
+     * 
+     * @param config the interceptor configuration
+     * @param listener object listening for events from the interceptor; may 
be <code>null</code>
      */
     public Interceptor(InterceptorConfiguration config, InterceptorListener 
listener) {
         this.config = config;
@@ -59,7 +59,9 @@
      */
     public void run() {
         try {
-            listener.onServerSocketStart();
+            if (listener != null) {
+                listener.onServerSocketStart();
+            }
             sSocket = 
config.getServerSocketFactory().createServerSocket(config.getListenPort());
             for (; ;) {
                 Socket inSocket = sSocket.accept();
@@ -74,7 +76,7 @@
                 inSocket = null;
             }
         } catch (Exception exp) {
-            if (!"socket closed".equals(exp.getMessage())) {
+            if (listener != null && !"socket closed".equals(exp.getMessage())) 
{
                 listener.onServerSocketError(exp);
             }
         }

Modified: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorListener.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorListener.java?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorListener.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorListener.java
 Sat May 30 12:57:28 2009
@@ -19,5 +19,13 @@
 public interface InterceptorListener {
     void onServerSocketStart();
     void onServerSocketError(Throwable ex);
+    
+    /**
+     * Create a listener for a new request-response exchange.
+     * 
+     * @param fromHost
+     * @return the listener or <code>null</code> if the implementation is not 
interested
+     *         in receiving events for a request-response exchange
+     */
     RequestResponseListener createRequestResponseListener(String fromHost);
 }

Modified: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Pipeline.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/Pipeline.java?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Pipeline.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Pipeline.java
 Sat May 30 12:57:28 2009
@@ -65,17 +65,17 @@
 
         public void invoke(byte[] buffer, int offset, int length, boolean eos, 
boolean preserve) {
             do {
-                if (inLength > 0) {
+                if (inLength == 0) {
+                    setBuffer(buffer, offset, length, preserve);
+                    offset += length;
+                    length = 0;
+                } else if (length > 0) {
                     int c = fillBuffer(buffer, offset, length);
                     if (c == 0) {
-                        throw new StreamException("Pipeline buffer overflow");
+                        throw new StreamException("Pipeline buffer overflow 
caused by filter " + filter.getClass().getName() + " (" + filter + ")");
                     }
                     offset += c;
                     length -= c;
-                } else {
-                    setBuffer(buffer, offset, length, preserve);
-                    offset += length;
-                    length = 0;
                 }
                 this.lastBuffer = eos && length == 0;
                 filter.invoke(this);

Modified: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Tee.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/Tee.java?rev=780256&r1=780255&r2=780256&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Tee.java
 (original)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/Tee.java
 Sat May 30 12:57:28 2009
@@ -56,4 +56,8 @@
             stream.skipAll();
         }
     }
+
+    public String toString() {
+        return "[out=" + out + "]";
+    }
 }

Added: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorDirectTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorDirectTest.java?rev=780256&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorDirectTest.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorDirectTest.java
 Sat May 30 12:57:28 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.engine;
+
+public class InterceptorDirectTest extends InterceptorTestBase {
+    protected InterceptorConfiguration buildInterceptorConfiguration() {
+        InterceptorConfigurationBuilder builder = new 
InterceptorConfigurationBuilder();
+        builder.setTargetHost("localhost");
+        builder.setTargetPort(SERVER_PORT);
+        builder.setListenPort(INTERCEPTOR_PORT);
+        return builder.build();
+    }
+}

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

Added: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorProxyTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorProxyTest.java?rev=780256&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorProxyTest.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorProxyTest.java
 Sat May 30 12:57:28 2009
@@ -0,0 +1,26 @@
+/*
+ * 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.engine;
+
+public class InterceptorProxyTest extends InterceptorTestBase {
+    protected InterceptorConfiguration buildInterceptorConfiguration() {
+        InterceptorConfigurationBuilder builder = new 
InterceptorConfigurationBuilder();
+        builder.setProxy(true);
+        builder.setListenPort(INTERCEPTOR_PORT);
+        return builder.build();
+    }
+}

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

Added: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorTestBase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorTestBase.java?rev=780256&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorTestBase.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/InterceptorTestBase.java
 Sat May 30 12:57:28 2009
@@ -0,0 +1,91 @@
+/*
+ * 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.engine;
+
+import junit.framework.TestCase;
+
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.params.ConnRoutePNames;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.mortbay.http.HttpContext;
+import org.mortbay.http.SocketListener;
+import org.mortbay.jetty.Server;
+
+public abstract class InterceptorTestBase extends TestCase {
+    protected static final int INTERCEPTOR_PORT = 9001;
+    protected static final int SERVER_PORT = 9002;
+    
+    private Server server;
+    private InterceptorConfiguration config;
+    private Interceptor interceptor;
+    private HttpClient client;
+    private String baseUri;
+    
+    protected void setUp() throws Exception {
+        // Set up server
+        
+        server = new Server();
+        SocketListener listener = new SocketListener();
+        listener.setPort(SERVER_PORT);
+        server.addListener(listener);
+        HttpContext context = new HttpContext(server, "/*");
+        context.addHandler(new TestHttpHandler());
+        server.start();
+        
+        // Set up interceptor
+        
+        config = buildInterceptorConfiguration();
+        interceptor = new Interceptor(config, new Dump(System.out));
+        // Wait for the interceptor to accept connections
+        Thread.sleep(500); // TODO: there should be a better way
+        
+        // Set up client
+        
+        client = new DefaultHttpClient();
+        if (config.isProxy()) {
+            baseUri = "http://localhost:"; + SERVER_PORT;
+            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new 
HttpHost("localhost", INTERCEPTOR_PORT, "http"));
+        } else {
+            baseUri = "http://localhost:"; + INTERCEPTOR_PORT;
+        }
+    }
+
+    protected void tearDown() throws Exception {
+        interceptor.halt();
+        server.stop();
+    }
+
+    protected abstract InterceptorConfiguration 
buildInterceptorConfiguration();
+    
+    public void testGet() throws Exception {
+        HttpGet request = new HttpGet(baseUri + "/test");
+        HttpResponse response = client.execute(request);
+        assertEquals(200, response.getStatusLine().getStatusCode());
+    }
+    
+    public void testPost() throws Exception {
+        HttpPost request = new HttpPost(baseUri + "/echo");
+        request.setEntity(new StringEntity("test"));
+        HttpResponse response = client.execute(request);
+        assertEquals(200, response.getStatusLine().getStatusCode());
+    }
+}

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

Added: 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/TestHttpHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/TestHttpHandler.java?rev=780256&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/TestHttpHandler.java
 (added)
+++ 
webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/engine/TestHttpHandler.java
 Sat May 30 12:57:28 2009
@@ -0,0 +1,64 @@
+/*
+ * 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.engine;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.commons.io.IOUtils;
+import org.mortbay.http.HttpException;
+import org.mortbay.http.HttpRequest;
+import org.mortbay.http.HttpResponse;
+import org.mortbay.http.handler.AbstractHttpHandler;
+
+public class TestHttpHandler extends AbstractHttpHandler {
+    public void handle(String pathInContext, String pathParams,
+            HttpRequest request, HttpResponse response) throws HttpException,
+            IOException {
+        
+        if (pathInContext.startsWith("/")) {
+            try {
+                String name = pathInContext.substring(1);
+                Method method = getClass().getMethod(name, new Class[] { 
HttpRequest.class, HttpResponse.class });
+                method.invoke(this, new Object[] { request, response });
+                request.setHandled(true);
+            } catch (SecurityException ex) {
+                // Do nothing
+            } catch (NoSuchMethodException ex) {
+                // Do nothing
+            } catch (IllegalAccessException ex) {
+                // Do nothing
+            } catch (InvocationTargetException ex) {
+                // Do nothing
+            }
+        }
+    }
+    
+    public void test(HttpRequest request, HttpResponse response) throws 
IOException {
+        response.setContentType("text/plain");
+        Writer out = new OutputStreamWriter(response.getOutputStream());
+        out.write("test");
+        out.flush();
+    }
+    
+    public void echo(HttpRequest request, HttpResponse response) throws 
IOException {
+        IOUtils.copy(request.getInputStream(), response.getOutputStream());
+    }
+}

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


Reply via email to