KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > util > DebuggingWebConnection


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.util;
39
40 import java.io.File JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.net.URL JavaDoc;
44
45 import org.org.apache.commons.httpclient.HttpState;
46 import org.apache.commons.io.FileUtils;
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 import com.gargoylesoftware.htmlunit.SubmitMethod;
51 import com.gargoylesoftware.htmlunit.WebConnection;
52 import com.gargoylesoftware.htmlunit.WebRequestSettings;
53 import com.gargoylesoftware.htmlunit.WebResponse;
54
55 /**
56  * Wrapper around a "real" WebConnection that will use the wrapped web connection
57  * to do the real job and save all received responses
58  * in the temp directory with an overview page.<br>
59  * <br>
60  * This may be usefull at conception time to understand what is "browsed".<br>
61  * <br>
62  * Example:
63  * <pre>
64  * final WebClient client = new WebClient();
65  * final WebConnection connection = new DebuggingWebConnection(client.getWebConnection(), "myTest");
66  * client.setWebConnection(connection);
67  * </pre>
68  * In this example an overview page will be generated under the name myTest.html in the temp directory.<br>
69  * <br>
70  * <em>This class is only intended as an help during the conception.</em>
71  * @version $Revision: 100 $
72  * @author Marc Guillemot
73  */

74 public class DebuggingWebConnection extends WebConnection {
75     private static final Log LOG = LogFactory.getLog(DebuggingWebConnection.class);
76
77     private int counter_ = 0;
78     private final WebConnection wrappedWebConnection_;
79     private final String JavaDoc reportBaseName_;
80     private final File JavaDoc javaScriptFile_;
81
82     /**
83      * Wraps a web connection to have a report generated of the received responses.
84      * @param webConnection the webConnection that do the real work
85      * @param reportBaseName the base name to use for the generated files.
86      * The report will be reportBaseName + ".html" in the temp file.
87      * @throws IOException in case of problems writing the files.
88      */

89     public DebuggingWebConnection(final WebConnection webConnection,
90             final String JavaDoc reportBaseName) throws IOException JavaDoc {
91
92         super(webConnection.getWebClient());
93
94         wrappedWebConnection_ = webConnection;
95         reportBaseName_ = reportBaseName;
96         javaScriptFile_ = File.createTempFile(reportBaseName_, ".js");
97         createOverview();
98     }
99
100
101     /**
102      * Calls the wrapped webconnection and save the received response.
103      * @see com.gargoylesoftware.htmlunit.WebConnection#getResponse(WebRequestSettings)
104      */

105     public WebResponse getResponse(final WebRequestSettings webRequestSettings) throws IOException JavaDoc {
106         final WebResponse response = wrappedWebConnection_.getResponse(webRequestSettings);
107         saveResponse(response, webRequestSettings.getSubmitMethod());
108         return response;
109     }
110
111     /**
112      * Calls the wrapped webConnection
113      * @see com.gargoylesoftware.htmlunit.WebConnection#getStateForUrl(java.net.URL)
114      */

115     public HttpState getStateForUrl(final URL JavaDoc url) {
116         return wrappedWebConnection_.getStateForUrl(url);
117     }
118
119
120     /**
121      * Save the response content in the temp dir and add it to the summary page
122      * @param response the response to save
123      * @param submitMethod the method used to get the response
124      * @throws IOException if a problem occurs writing the file
125      */

126     private void saveResponse(final WebResponse response, final SubmitMethod submitMethod)
127         throws IOException JavaDoc {
128         counter_++;
129         final String JavaDoc extension;
130         if ("application/x-javascript".equals(response.getContentType())) {
131             extension = ".js";
132         }
133         else if ("text/html".equals(response.getContentType())) {
134             extension = ".html";
135         }
136         else {
137             extension = ".txt";
138         }
139         final File JavaDoc f = File.createTempFile(reportBaseName_ + counter_ + "-", extension);
140         final String JavaDoc content = response.getContentAsString();
141         FileUtils.writeStringToFile(f, content, response.getContentCharSet());
142         LOG.info("Created file " + f.getAbsolutePath()
143                 + " for response " + counter_ + ": " + response.getUrl());
144
145         final FileWriter JavaDoc jsFileWriter = new FileWriter JavaDoc(javaScriptFile_, true);
146         jsFileWriter.write("tab[tab.length] = {code: " + response.getStatusCode() + ", "
147                 + "fileName: '" + f.getName() + "', "
148                 + "contentType: '" + response.getContentType() + "', "
149                 + "method: '" + submitMethod.getName() + "', "
150                 + "url: '" + response.getUrl() + "'}\n");
151         jsFileWriter.close();
152     }
153
154     /**
155      * Creates the summary file and the javascript file that will be updated for each received response
156      * @throws IOException if a problem occurs writing the file
157      */

158     private void createOverview() throws IOException JavaDoc {
159
160         FileUtils.writeStringToFile(javaScriptFile_, "var tab = [];\n", "ISO-8859-1");
161
162         final File JavaDoc summary = new File JavaDoc(javaScriptFile_.getParentFile(), reportBaseName_ + ".html");
163         final String JavaDoc content = "<html><head><title>Summary for " + reportBaseName_ + "</title>\n"
164             + "<h1>Received responses</h1>\n"
165             + "<script SRC='" + javaScriptFile_.getName() + "' type='text/javascript'></script>\n"
166             + "</head>\n"
167             + "<body>"
168             + "<ol>\n"
169             + "<script>\n"
170             + "for (var i=0; i<tab.length; ++i) {\n"
171             + " var curRes = tab[i];\n"
172             + " document.writeln('<li>'"
173             + " + curRes.code + ' ' + curRes.method + ' ' "
174             + " + '<a HREF=\"' + curRes.fileName + '\" target=_blank>' + curRes.url + '</a> "
175             + " (' + curRes.contentType + ')</li>');\n"
176             + "}\n"
177             + "</script>\n"
178             + "</ol>"
179             + "</body></html>";
180
181         FileUtils.writeStringToFile(summary, content, "ISO-8859-1");
182         LOG.info("Summary will be in " + summary.getAbsolutePath());
183     }
184 }
185
Popular Tags