? diff.list
Index: WebResponseObjectFactory.java
===================================================================
RCS file: /home/cvspublic/jakarta-cactus/framework/src/java/share-12-13-14/org/apache/cactus/internal/client/WebResponseObjectFactory.java,v
retrieving revision 1.1
diff -u -r1.1 WebResponseObjectFactory.java
--- WebResponseObjectFactory.java	22 May 2004 11:34:47 -0000	1.1
+++ WebResponseObjectFactory.java	27 Jun 2005 22:43:38 -0000
@@ -19,9 +19,13 @@
  */
 package org.apache.cactus.internal.client;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 
 import java.net.HttpURLConnection;
+import java.net.URL;
 import java.net.URLConnection;
 
 import org.apache.cactus.Request;
@@ -34,7 +38,7 @@
  * {@link org.apache.cactus.WebResponse} and HttpUnit 
  * <code>com.meterware.httpunit.WebResponse</code> response object creation.
  *
- * @version $Id: WebResponseObjectFactory.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
+ * @version $Id: WebResponseObjectFactory.java,v 1.1 2005/06/23 21:48:28 natalia Exp $
  */
 public class WebResponseObjectFactory implements ResponseObjectFactory
 {
@@ -66,6 +70,12 @@
         {
             responseObject = createHttpUnitWebResponse(this.connection);
 
+            // Is it a Html Unit WebResponse ?
+        }
+        else if (theClassName.equals("com.gargoylesoftware.htmlunit.WebResponse"))
+        {
+            responseObject = createHtmlUnitWebResponse(this.connection);
+
             // Is it a Cactus WebResponse ?
         }
         else if (theClassName.equals("org.apache.cactus.WebResponse"))
@@ -126,4 +136,45 @@
 
         return webResponse;
     }
+
+    /**
+     * Create a HtmlUnit <code>WebResponse</code> object by reflection (so
+     * that we don't need the HtmlUnit jar for users who are not using
+     * the HttpUnit endXXX() signature).
+     *
+     * @param theConnection the HTTP connection that was used when connecting
+     *        to the server side and which now contains the returned HTTP
+     *        response that we will pass to HttpUnit so that it can construct
+     *        a <code>com.gargoylesoftware.htmlunit.WebResponse</code> object.
+     * @return a HtmlUnit <code>WebResponse</code> object
+     * @exception ClientException if it failes to create a HttpClient
+     *            WebResponse object for any reason
+     */
+    private Object createHtmlUnitWebResponse(HttpURLConnection theConnection)
+        throws ClientException
+    {
+        Object webResponse;
+
+        try
+        {
+            Class responseClass = 
+                Class.forName("com.gargoylesoftware.htmlunit.StringWebResponse");
+            Constructor method = responseClass.getConstructor( 
+                new Class[] {String.class, URL.class});
+
+            InputStream input = theConnection.getInputStream();
+            byte[] buffer = new byte[input.available()];
+            input.read(buffer);
+            webResponse = method.newInstance(new Object[] {new String(buffer), theConnection.getURL()});
+        }
+        catch (Exception e)
+        {
+            throw new ClientException("Error calling "
+                + "[public static com.meterware.httpunit.WebResponse "
+                + "com.meterware.httpunit.WebResponse.newResponse("
+                + "java.net.URLConnection) throws java.io.IOException]", e);
+        }
+
+        return webResponse;
+    }
 }
