Author: sebb
Date: Sat Oct 13 18:13:52 2007
New Revision: 584466

URL: http://svn.apache.org/viewvc?rev=584466&view=rev
Log:
Optional loopback protocol for HttpClient sampler

Added:
    
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
   (with props)
    
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
   (with props)
Modified:
    
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java?rev=584466&r1=584465&r2=584466&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
 Sat Oct 13 18:13:52 2007
@@ -71,8 +71,9 @@
 import org.apache.jmeter.protocol.http.control.CookieManager;
 import org.apache.jmeter.protocol.http.control.HeaderManager;
 import org.apache.jmeter.protocol.http.util.EncoderCache;
-import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
 import org.apache.jmeter.protocol.http.util.HTTPArgument;
+import org.apache.jmeter.protocol.http.util.LoopbackHttpClientSocketFactory;
+import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.util.JMeterUtils;
@@ -196,6 +197,10 @@
         log.info("Local host = "+localHost);
         
         setDefaultParams();
+        
+        if (JMeterUtils.getPropDefault("httpclient.loopback",false)){// 
$NON-NLS-1$
+               LoopbackHttpClientSocketFactory.setup();
+        }
        }
 
     // Set default parameters as needed

Added: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java?rev=584466&view=auto
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
 (added)
+++ 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
 Sat Oct 13 18:13:52 2007
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jmeter.protocol.http.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/*
+ * Socket that reads back from the output
+ */
+public class LoopbackHTTPSocket extends Socket {
+
+       // get access to buffer
+       static class LoopbackOutputStream extends ByteArrayOutputStream{
+               byte [] getBuffer() {
+                       return buf;
+               }
+       }
+
+       // wrap read() methods to track output buffer
+       static class LoopBackInputStream extends ByteArrayInputStream{
+               LoopbackOutputStream os;
+               public synchronized int read() {
+                       buf=os.getBuffer();   // make sure buffer details
+                       count=buf.length; // track the output
+                       return super.read();
+               }
+               public synchronized int read(byte[] b, int off, int len) {
+                       buf=os.getBuffer();
+                       count=buf.length;
+                       return super.read(b, off, len);
+               }
+               
+               public LoopBackInputStream(LoopbackOutputStream _os) {
+                       super(_os.getBuffer());
+                       os=_os;
+               }               
+       }
+       
+       private final LoopbackOutputStream os;
+       
+       private LoopbackHTTPSocket() throws IOException{
+               os=new LoopbackOutputStream();
+               // Preload the output so that can be read back as HTTP
+               os.write("HTTP/1.1 200 OK\nContent-Type: 
text/plain\n\n".getBytes());
+       }
+       
+    public LoopbackHTTPSocket(String host, int port, InetAddress localAddress, 
int localPort, int timeout) throws IOException {
+       this();
+       }
+
+    public LoopbackHTTPSocket(String host, int port, InetAddress localAddr, 
int localPort) throws IOException {
+       this();
+    }
+
+    public LoopbackHTTPSocket(String host, int port) throws 
UnknownHostException, IOException {
+       this();
+    }
+
+       // Override so we can intercept the stream
+    public OutputStream getOutputStream() throws IOException {
+        return os;
+    }
+    
+    // Override so we can intercept the stream
+    public InputStream getInputStream() throws IOException {
+        return new LoopBackInputStream(os);
+    }
+}
\ No newline at end of file

Propchange: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHTTPSocket.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java?rev=584466&view=auto
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
 (added)
+++ 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
 Sat Oct 13 18:13:52 2007
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jmeter.protocol.http.util;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+import java.net.UnknownHostException;
+
+import org.apache.commons.httpclient.ConnectTimeoutException;
+import org.apache.commons.httpclient.params.HttpConnectionParams;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
+
+/**
+ * HttpClient protocol factory to generate Loopback HTTP sockets
+ */
+
+public class LoopbackHttpClientSocketFactory implements ProtocolSocketFactory {
+
+    public LoopbackHttpClientSocketFactory() {
+        super();
+    }
+
+    public Socket createSocket(String host, int port, InetAddress clientHost,
+            int clientPort) throws IOException, UnknownHostException {
+        return new LoopbackHTTPSocket(host,port,clientHost,clientPort);
+    }
+
+    public Socket createSocket(String host, int port) throws IOException,
+            UnknownHostException {
+        return new LoopbackHTTPSocket(host,port);
+    }
+
+    public Socket createSocket(String host, int port, InetAddress 
localAddress, int localPort,
+               HttpConnectionParams params) 
+    throws IOException, UnknownHostException, ConnectTimeoutException {
+        int timeout = params.getConnectionTimeout();
+        if (timeout == 0) {
+               return new LoopbackHTTPSocket(host,port,localAddress,localPort);
+        } else {
+               return new LoopbackHTTPSocket(host,port,localAddress,localPort, 
timeout);               
+        }
+    }
+
+    /**
+     * Convenience method to set up the necessary HttpClient protocol and URL 
handler.
+     * 
+     * Only works for HttpClient, because it's not possible (or at least very 
difficult)
+     * to provide a different socket factory for the HttpURLConnection class.
+     */
+    public static void setup(){
+        final String LOOPBACK = "loopback"; // $NON-NLS-1$
+        
+        // This ensures tha HttpClient knows about the protocol
+               Protocol.registerProtocol(LOOPBACK, new Protocol(LOOPBACK,new 
LoopbackHttpClientSocketFactory(),1));
+        
+               // Now allow the URL handling to work.
+        URLStreamHandlerFactory ushf = new URLStreamHandlerFactory(){
+                       public URLStreamHandler createURLStreamHandler(String 
protocol) {
+                               if (protocol.equalsIgnoreCase(LOOPBACK)){
+                                       return new URLStreamHandler(){
+                                               protected URLConnection 
openConnection(URL u) throws IOException {
+                                                       return null;// not 
needed for HttpClient
+                                               }
+                                       };
+                               }
+                               return null;
+                       }
+        };
+        
+        java.net.URL.setURLStreamHandlerFactory(ushf); 
+    }
+}

Propchange: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/LoopbackHttpClientSocketFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=584466&r1=584465&r2=584466&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Sat Oct 13 18:13:52 2007
@@ -56,6 +56,7 @@
 <li>"Save Selection As" added to main menu; now checks only item is 
selected</li>
 <li>Test Plan now has Paste menu item (paste was already supported via ^V)</li>
 <li>If the default delimiter does not work when loading a CSV file, guess the 
delimiter by analysing the header line.</li>
+<li>Add optional "loopback" protocol for HttpClient sampler</li>
 </ul>
 
 <h4>Non-functional Improvements</h4>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to