1 38 package com.gargoylesoftware.htmlunit.util; 39 40 import java.io.File ; 41 import java.io.FileWriter ; 42 import java.io.IOException ; 43 import java.net.URL ; 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 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 reportBaseName_; 80 private final File javaScriptFile_; 81 82 89 public DebuggingWebConnection(final WebConnection webConnection, 90 final String reportBaseName) throws IOException { 91 92 super(webConnection.getWebClient()); 93 94 wrappedWebConnection_ = webConnection; 95 reportBaseName_ = reportBaseName; 96 javaScriptFile_ = File.createTempFile(reportBaseName_, ".js"); 97 createOverview(); 98 } 99 100 101 105 public WebResponse getResponse(final WebRequestSettings webRequestSettings) throws IOException { 106 final WebResponse response = wrappedWebConnection_.getResponse(webRequestSettings); 107 saveResponse(response, webRequestSettings.getSubmitMethod()); 108 return response; 109 } 110 111 115 public HttpState getStateForUrl(final URL url) { 116 return wrappedWebConnection_.getStateForUrl(url); 117 } 118 119 120 126 private void saveResponse(final WebResponse response, final SubmitMethod submitMethod) 127 throws IOException { 128 counter_++; 129 final String 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 f = File.createTempFile(reportBaseName_ + counter_ + "-", extension); 140 final String 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 jsFileWriter = new FileWriter (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 158 private void createOverview() throws IOException { 159 160 FileUtils.writeStringToFile(javaScriptFile_, "var tab = [];\n", "ISO-8859-1"); 161 162 final File summary = new File (javaScriptFile_.getParentFile(), reportBaseName_ + ".html"); 163 final String 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 |