/*
* Copyright (c) 2002-2008 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
* http://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 com.gargoylesoftware.htmlunit.util;
import java.io.IOException;
import java.net.URL;
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;
/**
* Extension of {@link WebConnectionWrapper} providing facility methods to deliver something else than
* what the wrapped connection would deliver.
*
* @version $Revision$
* @author Marc Guillemot
*/
public abstract class FalsifyingWebConnection extends WebConnectionWrapper {
/**
* Constructs a WebConnection object wrapping provided WebConnection.
* @param webConnection the webConnection that does the real work
* @throws IllegalArgumentException if the connection is null
*/
public FalsifyingWebConnection(final WebConnection webConnection) throws IllegalArgumentException {
super(webConnection);
}
/**
* Constructs an instance and places itself as connection of the WebClient.
* @param webClient the WebClient which WebConnection should be wrapped
* @throws IllegalArgumentException if the WebClient is null
*/
public FalsifyingWebConnection(final WebClient webClient) throws IllegalArgumentException {
super(webClient);
}
/**
* Delivers the content for an alternate URL as if it comes from the requested URL.
* @param webRequestSettings the original web request settings
* @param url the URL from which the content should be retrieved
* @return the response
* @throws IOException if a problem occurred
*/
protected WebResponse deliverFromAlternateUrl(final WebRequestSettings webRequestSettings, final URL url)
throws IOException {
final URL originalUrl = webRequestSettings.getUrl();
webRequestSettings.setUrl(url);
final WebResponse resp = super.getResponse(webRequestSettings);
return new WebResponseWrapper(resp) {
@Override
public URL getUrl() {
return originalUrl;
}
};
}
/**
* Builds a WebResponse with new content, preserving all other information.
* @param webResponse the web response to adapt
* @param newContent the new content to place in the response
* @return a web response with the new content
* @throws IOException if an encoding problem occurred
*/
protected WebResponse replaceContent(final WebResponse webResponse, final String newContent) throws IOException {
final byte[] body = newContent.getBytes(webResponse.getContentCharSet());
final WebResponseData wrd = new WebResponseData(body, webResponse.getStatusCode(),
webResponse.getStatusMessage(), webResponse.getResponseHeaders());
return new WebResponseImpl(wrd, webResponse.getUrl(), webResponse.getRequestMethod(),
webResponse.getLoadTimeInMilliSeconds());
}
}