logo       

[HtmlUnit] CVS Commit: gargoylesoftware/htmlunit: WebConnection is now an: msg#00052

java.htmlunit.devel

Subject: [HtmlUnit] CVS Commit: gargoylesoftware/htmlunit: WebConnection is now an

Log Message:
-----------
WebConnection is now an interface. Added WebConnectionWrapper to facilitate
custom implementations enhencing existing connections

Modified Files:
--------------
htmlunit/src/java/com/gargoylesoftware/htmlunit:
HttpWebConnection.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java)
WebConnection.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebConnection.java)
MockWebConnection.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/MockWebConnection.java)
htmlunit/src/java/com/gargoylesoftware/htmlunit/util:
DebuggingWebConnection.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java)

Added Files:
-----------
htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util:
WebConnectionWrapperTest.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/WebConnectionWrapperTest.java?rev=1.1&content-type=text/x-cvsweb-markup)
DebuggingWebConnectionTest.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnectionTest.java?rev=1.1&content-type=text/x-cvsweb-markup)
htmlunit/src/java/com/gargoylesoftware/htmlunit:
WebConnectionImpl.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebConnectionImpl.java?rev=1.1&content-type=text/x-cvsweb-markup)
htmlunit/src/java/com/gargoylesoftware/htmlunit/util:
WebConnectionWrapper.java

(http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/util/WebConnectionWrapper.java?rev=1.1&content-type=text/x-cvsweb-markup)

Revision Data
-------------
Index: WebConnection.java
===================================================================
RCS file:
/cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebConnection.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -Lsrc/java/com/gargoylesoftware/htmlunit/WebConnection.java
-Lsrc/java/com/gargoylesoftware/htmlunit/WebConnection.java -u -d -r1.16 -r1.17
--- src/java/com/gargoylesoftware/htmlunit/WebConnection.java
+++ src/java/com/gargoylesoftware/htmlunit/WebConnection.java
@@ -48,24 +48,11 @@
* retrieval/submission.
*
* @version $Revision$
- * @author <a href="mailto:mbowler@xxxxxxxxxxxxxxxxxxxx";>Mike Bowler</a>
+ * @author <a href="mailto:mbowler@xxxxxxxxxxxxxxxxxxxx";>Mike Bowler</a>
* @author Daniel Gredler
* @author Marc Guillemot
*/
-public abstract class WebConnection {
-
- private final WebClient webClient_;
-
- /**
- * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
- *
- * Creates a new web connection instance.
- * @param webClient The WebClient that is using this connection.
- */
- public WebConnection( final WebClient webClient ) {
- webClient_ = webClient;
- }
-
+public interface WebConnection {
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
*
@@ -74,7 +61,7 @@
* @return The response to the request defined by the specified request
settings.
* @exception IOException If an IO error occurs.
*/
- public abstract WebResponse getResponse(final WebRequestSettings
webRequestSettings) throws IOException;
+ WebResponse getResponse(final WebRequestSettings webRequestSettings)
throws IOException;

/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
@@ -82,9 +69,8 @@
* Return the web client.
* @return The web client.
*/
- public final WebClient getWebClient() {
- return webClient_;
- }
+ WebClient getWebClient();
+

/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
@@ -92,6 +78,6 @@
* Return the {@link HttpState} that is being used.
* @return the state.
*/
- public abstract HttpState getState();
+ HttpState getState();

}
Index: MockWebConnection.java
===================================================================
RCS file:
/cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/MockWebConnection.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -Lsrc/java/com/gargoylesoftware/htmlunit/MockWebConnection.java
-Lsrc/java/com/gargoylesoftware/htmlunit/MockWebConnection.java -u -d -r1.25
-r1.26
--- src/java/com/gargoylesoftware/htmlunit/MockWebConnection.java
+++ src/java/com/gargoylesoftware/htmlunit/MockWebConnection.java
@@ -59,7 +59,7 @@
* @author Marc Guillemot
* @author Brad Clarke
*/
-public class MockWebConnection extends WebConnection {
+public class MockWebConnection extends WebConnectionImpl {
private final Map responseMap_ = new HashMap(10);
private WebResponseData defaultResponse_;

Index: HttpWebConnection.java
===================================================================
RCS file:
/cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java,v
retrieving revision 1.55
retrieving revision 1.56
diff -Lsrc/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
-Lsrc/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java -u -d -r1.55
-r1.56
--- src/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
+++ src/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
@@ -83,7 +83,7 @@
* @author Marc Guillemot
* @author Brad Clarke
*/
-public class HttpWebConnection extends WebConnection {
+public class HttpWebConnection extends WebConnectionImpl {
private HttpClient httpClient_;

private String virtualHost_ = null;
--- /dev/null
+++ src/java/com/gargoylesoftware/htmlunit/WebConnectionImpl.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2002-2006 Gargoyle Software Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The end-user documentation included with the redistribution, if any, must
+ * include the following acknowledgment:
+ *
+ * "This product includes software developed by Gargoyle Software Inc.
+ * (http://www.GargoyleSoftware.com/)."
+ *
+ * Alternately, this acknowledgment may appear in the software itself, if
+ * and wherever such third-party acknowledgments normally appear.
+ * 4. The name "Gargoyle Software" must not be used to endorse or promote
+ * products derived from this software without prior written permission.
+ * For written permission, please contact info@xxxxxxxxxxxxxxxxxxxxx
+ * 5. Products derived from this software may not be called "HtmlUnit", nor may
+ * "HtmlUnit" appear in their name, without prior written permission of
+ * Gargoyle Software Inc.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
+ * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.gargoylesoftware.htmlunit;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpState;
+
+/**
+ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE
AT YOUR OWN RISK.</span><br/>
+ *
+ * An object that handles the actual communication portion of page
+ * retrieval/submission.
+ *
+ * @version $Revision: 1.1 $
+ * @author <a href="mailto:mbowler@xxxxxxxxxxxxxxxxxxxx";>Mike Bowler</a>
+ * @author Daniel Gredler
+ * @author Marc Guillemot
+ */
+abstract class WebConnectionImpl implements WebConnection {
+
+ private final WebClient webClient_;
+
+ /**
+ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
+ *
+ * Creates a new web connection instance.
+ * @param webClient The WebClient that is using this connection.
+ */
+ public WebConnectionImpl( final WebClient webClient ) {
+ webClient_ = webClient;
+ }
+
+ /**
+ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
+ *
+ * Submits a request and retrieves a response.
+ * @param webRequestSettings Settings to make the request with.
+ * @return The response to the request defined by the specified request
settings.
+ * @exception IOException If an IO error occurs.
+ */
+ public abstract WebResponse getResponse(final WebRequestSettings
webRequestSettings) throws IOException;
+
+ /**
+ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
+ *
+ * Return the web client.
+ * @return The web client.
+ */
+ public final WebClient getWebClient() {
+ return webClient_;
+ }
+
+ /**
+ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME -
USE AT YOUR OWN RISK.</span><br/>
+ *
+ * Return the {@link HttpState} that is being used.
+ * @return the state.
+ */
+ public abstract HttpState getState();
+
+}
--- /dev/null
+++
src/test/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnectionTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2002-2006 Gargoyle Software Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The end-user documentation included with the redistribution, if any, must
+ * include the following acknowledgment:
+ *
+ * "This product includes software developed by Gargoyle Software Inc.
+ * (http://www.GargoyleSoftware.com/)."
+ *
+ * Alternately, this acknowledgment may appear in the software itself, if
+ * and wherever such third-party acknowledgments normally appear.
+ * 4. The name "Gargoyle Software" must not be used to endorse or promote
+ * products derived from this software without prior written permission.
+ * For written permission, please contact info@xxxxxxxxxxxxxxxxxxxxx
+ * 5. Products derived from this software may not be called "HtmlUnit", nor may
+ * "HtmlUnit" appear in their name, without prior written permission of
+ * Gargoyle Software Inc.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
+ * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.gargoylesoftware.htmlunit.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.httpclient.NameValuePair;
+
+import com.gargoylesoftware.htmlunit.WebTestCase;
+
+/**
+ * Tests for {@link DebuggingWebConnection}.
+ *
+ * @version $Revision: 1.1 $
+ * @author Marc Guillemot
+ */
+public class DebuggingWebConnectionTest extends WebTestCase {
+
+ /**
+ * Creates a new instance.
+ * @param name The name of the new instance.
+ */
+ public DebuggingWebConnectionTest(final String name) {
+ super( name );
+ }
+
+ /**
+ * @throws Exception If the test fails.
+ */
+ public void testNameValueListToJsMap() throws Exception {
+ assertEquals("{}", DebuggingWebConnection.nameValueListToJsMap(null));
+ assertEquals("{}",
DebuggingWebConnection.nameValueListToJsMap(Collections.EMPTY_LIST));
+
+ List list = Collections.singletonList(new NameValuePair("name",
"value"));
+ assertEquals("{\"name\": \"value\"}",
+ DebuggingWebConnection.nameValueListToJsMap(list));
+
+ list = Collections.singletonList(new NameValuePair("na me", "value"));
+ assertEquals("{\"na me\": \"value\"}",
+ DebuggingWebConnection.nameValueListToJsMap(list));
+
+ list = new ArrayList();
+ list.add(new NameValuePair("na me", "value1"));
+ list.add(new NameValuePair("key", "value 2"));
+ list.add(new NameValuePair("key 2", "value 3"));
+ final String expected = "{'na me': 'value1', 'key': 'value 2', 'key
2': 'value 3'}".replaceAll("'", "\"");
+ assertEquals(expected,
DebuggingWebConnection.nameValueListToJsMap(list));
+ }
+
+}
--- /dev/null
+++
src/test/java/com/gargoylesoftware/htmlunit/util/WebConnectionWrapperTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2002-2006 Gargoyle Software Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The end-user documentation included with the redistribution, if any, must
+ * include the following acknowledgment:
+ *
+ * "This product includes software developed by Gargoyle Software Inc.
+ * (http://www.GargoyleSoftware.com/)."
+ *
+ * Alternately, this acknowledgment may appear in the software itself, if
+ * and wherever such third-party acknowledgments normally appear.
+ * 4. The name "Gargoyle Software" must not be used to endorse or promote
+ * products derived from this software without prior written permission.
+ * For written permission, please contact info@xxxxxxxxxxxxxxxxxxxxx
+ * 5. Products derived from this software may not be called "HtmlUnit", nor may
+ * "HtmlUnit" appear in their name, without prior written permission of
+ * Gargoyle Software Inc.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
+ * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.gargoylesoftware.htmlunit.util;
+
+import java.util.Collections;
+
+import org.apache.commons.httpclient.HttpState;
+
+import com.gargoylesoftware.htmlunit.SubmitMethod;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebConnection;
+import com.gargoylesoftware.htmlunit.WebRequestSettings;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.WebResponseData;
+import com.gargoylesoftware.htmlunit.WebResponseImpl;
+import com.gargoylesoftware.htmlunit.WebTestCase;
+
+/**
+ * Tests for {@link WebConnectionWrapper}.
+ *
+ * @version $Revision: 1.1 $
+ * @author Marc Guillemot
+ */
+public class WebConnectionWrapperTest extends WebTestCase {
+
+ /**
+ * Creates a new instance.
+ * @param name The name of the new instance.
+ */
+ public WebConnectionWrapperTest(final String name) {
+ super( name );
+ }
+
+ /**
+ * @throws Exception If the test fails.
+ */
+ public void testWrapper() throws Exception {
+ final HttpState state = new HttpState();
+ final WebResponseData data = new WebResponseData(new byte[]{}, 200,
"", Collections.EMPTY_LIST);
+ final WebResponse response = new WebResponseImpl(data, URL_FIRST,
SubmitMethod.GET, 0);
+ final WebClient webClient = new WebClient();
+ final WebRequestSettings settings = new WebRequestSettings(URL_FIRST);
+ final String[] lastMethodCalled = {""};
+
+ final WebConnection realConnection = new WebConnection()
+ {
+ public WebResponse getResponse(final WebRequestSettings
webRequestSettings) {
+ assertSame(settings, webRequestSettings);
+ lastMethodCalled[0] = "getResponse";
+ return response;
+ }
+ public HttpState getState() {
+ lastMethodCalled[0] = "getState";
+ return state;
+ }
+ public WebClient getWebClient() {
+ lastMethodCalled[0] = "getWebClient";
+ return webClient;
+ }
+ };
+
+ final WebConnectionWrapper wrapper = new
WebConnectionWrapper(realConnection);
+
+ assertSame(response, wrapper.getResponse(settings));
+ assertEquals("getResponse", lastMethodCalled[0]);
+ assertSame(state, wrapper.getState());
+ assertEquals("getState", lastMethodCalled[0]);
+ assertSame(webClient, wrapper.getWebClient());
+ assertEquals("getWebClient", lastMethodCalled[0]);
+ }
+
+}
Index: DebuggingWebConnection.java
===================================================================
RCS file:
/cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -Lsrc/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java
-Lsrc/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java -u -d
-r1.8 -r1.9
--- src/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java
+++ src/java/com/gargoylesoftware/htmlunit/util/DebuggingWebConnection.java
@@ -40,13 +40,14 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;

-import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

-import com.gargoylesoftware.htmlunit.SubmitMethod;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
@@ -70,7 +71,7 @@
* @version $Revision$
* @author Marc Guillemot
*/
-public class DebuggingWebConnection extends WebConnection {
+public class DebuggingWebConnection extends WebConnectionWrapper {
private static final Log LOG =
LogFactory.getLog(DebuggingWebConnection.class);

private int counter_ = 0;
@@ -88,7 +89,7 @@
public DebuggingWebConnection(final WebConnection webConnection,
final String reportBaseName) throws IOException {

- super(webConnection.getWebClient());
+ super(webConnection);

wrappedWebConnection_ = webConnection;
reportBaseName_ = reportBaseName;
@@ -102,26 +103,17 @@
*/
public WebResponse getResponse(final WebRequestSettings
webRequestSettings) throws IOException {
final WebResponse response =
wrappedWebConnection_.getResponse(webRequestSettings);
- saveResponse(response, webRequestSettings.getSubmitMethod());
+ saveResponse(response, webRequestSettings);
return response;
}

/**
- * Calls the wrapped webConnection.
- * {@inheritDoc}
- */
- public HttpState getState() {
- return wrappedWebConnection_.getState();
- }
-
-
- /**
* Save the response content in the temp dir and add it to the summary page
* @param response the response to save
- * @param submitMethod the method used to get the response
+ * @param settings the settings used to get the response
* @throws IOException if a problem occurs writing the file
*/
- private void saveResponse(final WebResponse response, final SubmitMethod
submitMethod)
+ protected void saveResponse(final WebResponse response, final
WebRequestSettings settings)
throws IOException {
counter_++;
final String extension;
@@ -140,16 +132,40 @@
LOG.info("Created file " + f.getAbsolutePath()
+ " for response " + counter_ + ": " + response.getUrl());

+ final StringBuffer buffer = new StringBuffer();
+ buffer.append("tab[tab.length] = {code: " + response.getStatusCode() +
", ");
+ buffer.append("fileName: '" + f.getName() + "', ");
+ buffer.append("contentType: '" + response.getContentType() + "', ");
+ buffer.append("method: '" + settings.getSubmitMethod().getName() + "',
");
+ buffer.append("url: '" + response.getUrl() + ",");
+ buffer.append("headers: " +
nameValueListToJsMap(response.getResponseHeaders()));
+ buffer.append("'};\n");
final FileWriter jsFileWriter = new FileWriter(javaScriptFile_, true);
- jsFileWriter.write("tab[tab.length] = {code: " +
response.getStatusCode() + ", "
- + "fileName: '" + f.getName() + "', "
- + "contentType: '" + response.getContentType() + "', "
- + "method: '" + submitMethod.getName() + "', "
- + "url: '" + response.getUrl() + "'}\n");
+ jsFileWriter.write(buffer.toString());
+
jsFileWriter.close();
}

/**
+ * Produces a String that will produce a JS map like "{'key1': 'value1',
'key 2': 'value2'}"
+ * @param headers a list of {@link NameValuePair}
+ * @return the JS String
+ */
+ static String nameValueListToJsMap(final List headers) {
+ if (headers == null || headers.isEmpty()) {
+ return "{}";
+ }
+ final StringBuffer buffer = new StringBuffer("{");
+ for (final Iterator iter=headers.iterator(); iter.hasNext();) {
+ final NameValuePair header = (NameValuePair) iter.next();
+ buffer.append("\"" + header.getName() + "\": \"" +
header.getValue() + "\", ");
+ }
+ buffer.delete(buffer.length()-2, buffer.length());
+ buffer.append("}");
+ return buffer.toString();
+ }
+
+ /**
* Creates the summary file and the javascript file that will be updated
for each received response
* @throws IOException if a problem occurs writing the file
*/
--- /dev/null
+++ src/java/com/gargoylesoftware/htmlunit/util/WebConnectionWrapper.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2002-2006 Gargoyle Software Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The end-user documentation included with the redistribution, if any, must
+ * include the following acknowledgment:
+ *
+ * "This product includes software developed by Gargoyle Software Inc.
+ * (http://www.GargoyleSoftware.com/)."
+ *
+ * Alternately, this acknowledgment may appear in the software itself, if
+ * and wherever such third-party acknowledgments normally appear.
+ * 4. The name "Gargoyle Software" must not be used to endorse or promote
+ * products derived from this software without prior written permission.
+ * For written permission, please contact info@xxxxxxxxxxxxxxxxxxxxx
+ * 5. Products derived from this software may not be called "HtmlUnit", nor may
+ * "HtmlUnit" appear in their name, without prior written permission of
+ * Gargoyle Software Inc.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
+ * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.gargoylesoftware.htmlunit.util;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpState;
+
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebConnection;
+import com.gargoylesoftware.htmlunit.WebRequestSettings;
+import com.gargoylesoftware.htmlunit.WebResponse;
+
+/**
+ * Provides a convenient implementation of the WebConneciton interface that
can be subclassed by developers
+ * wishing to adapt a particular WebConnection.
+ * This class implements the Wrapper or Decorator pattern. Methods default to
calling through to the wrapped
+ * web connection object.
+ * @version $Revision: 1.1 $
+ * @author Marc Guillemot
+ */
+public class WebConnectionWrapper implements WebConnection {
+ private final WebConnection wrappedWebConnection_;
+
+ /**
+ * Constructs a WebConnection object wrapping provided WebConnection.
+ * @param webConnection the webConnection that does the real work
+ * @throws IllegalArgumentException if the connection is <code>null</code>
+ */
+ public WebConnectionWrapper(final WebConnection webConnection) throws
IllegalArgumentException {
+ if (webConnection == null) {
+ throw new IllegalArgumentException("Wrapped connection can't be
null");
+ }
+ wrappedWebConnection_ = webConnection;
+ }
+
+ /**
+ * The default behavior of this method is to return getResponse() on the
wrapped connection object.
+ * @see WebConnection#getResponse
+ */
+ public WebResponse getResponse(final WebRequestSettings
webRequestSettings) throws IOException {
+ return wrappedWebConnection_.getResponse(webRequestSettings);
+ }
+
+ /**
+ * The default behavior of this method is to return getState() on the
wrapped connection object.
+ * @see WebConnection#getState()
+ */
+ public HttpState getState() {
+ return wrappedWebConnection_.getState();
+ }
+
+ /**
+ * The default behavior of this method is to return getWebClient() on the
wrapped connection object.
+ * @see WebConnection#getWebClient()
+ */
+ public WebClient getWebClient() {
+ return wrappedWebConnection_.getWebClient();
+ }
+
+}


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise