Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2002-2023 Gargoyle Software Inc.
*
* Licensed 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
* https://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.htmlunit.util;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlunit.FormEncodingType;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebConnection;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
import org.htmlunit.WebResponseData;
import org.htmlunit.corejs.javascript.ContextAction;
import org.htmlunit.corejs.javascript.ContextFactory;
import org.htmlunit.corejs.javascript.Script;
/**
* Wrapper around a "real" WebConnection that will use the wrapped web connection
* to do the real job and save all received responses
* in the temp directory with an overview page.
*
* This may be useful at conception time to understand what is "browsed".
*
* Example:
*
* final WebClient client = new WebClient();
* final WebConnection connection = new DebuggingWebConnection(client.getWebConnection(), "myTest");
* client.setWebConnection(connection);
*
* In this example an overview page will be generated under the name myTest/index.html in the temp directory
* and all received responses will be saved into the myTest folder.
*
* This class is only intended as a help during the conception.
*
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Ronald Brill
*/
public class DebuggingWebConnection extends WebConnectionWrapper {
private static final Log LOG = LogFactory.getLog(DebuggingWebConnection.class);
private static final Pattern ESCAPE_QUOTE_PATTERN = Pattern.compile("'");
private int counter_;
private final WebConnection wrappedWebConnection_;
private final File javaScriptFile_;
private final File reportFolder_;
private boolean uncompressJavaScript_ = true;
/**
* Wraps a web connection to have a report generated of the received responses.
* @param webConnection the webConnection that do the real work
* @param dirName the name of the directory to create in the tmp folder to save received responses.
* If this folder already exists, it will be deleted first.
* @throws IOException in case of problems writing the files
*/
public DebuggingWebConnection(final WebConnection webConnection,
final String dirName) throws IOException {
super(webConnection);
wrappedWebConnection_ = webConnection;
final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
reportFolder_ = new File(tmpDir, dirName);
if (reportFolder_.exists()) {
FileUtils.forceDelete(reportFolder_);
}
FileUtils.forceMkdir(reportFolder_);
javaScriptFile_ = new File(reportFolder_, "hu.js");
createOverview();
}
/**
* Calls the wrapped webconnection and save the received response.
* {@inheritDoc}
*/
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
WebResponse response = wrappedWebConnection_.getResponse(request);
if (isUncompressJavaScript() && isJavaScript(response.getContentType())) {
response = uncompressJavaScript(response);
}
saveResponse(response, request);
return response;
}
/**
* Tries to uncompress the JavaScript code in the provided response.
* @param response the response to uncompress
* @return a new response with uncompressed JavaScript code or the original response in case of failure
*/
protected WebResponse uncompressJavaScript(final WebResponse response) {
final WebRequest request = response.getWebRequest();
final String scriptName = request.getUrl().toString();
final String scriptSource = response.getContentAsString();
// skip if it is already formatted? => TODO
final ContextFactory factory = new ContextFactory();
final ContextAction