HtmlUnitSessionTag.java from Jameleon at Krugle
Show HtmlUnitSessionTag.java syntax highlighted
/*
Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.jameleon.SessionTag;
import net.sf.jameleon.function.ContextHelper;
import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitDelegate;
import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitHelper;
import net.sf.jameleon.plugin.htmlunit.util.TrustEverythingSSLProtocolSocketFactory;
import net.sf.jameleon.util.Configurator;
import net.sf.jameleon.util.StateStorer;
import org.apache.commons.httpclient.protocol.Protocol;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
/**
* A Session tag for the HtmlUnit plug-in.
*
* An example of its use might:
*
* <pre><source>
* <testcase xmlns="jelly:jameleon">
* <htmlunit-session baseUrl="http://www.google.com" beginSession="true">
* <htmlunit-validate
* functionId="Check that title is Google."
* title="Google"/>
* </htmlunit-session>
* </testcase>
* </source></pre>
*
* @jameleon.function name="htmlunit-session"
*/
public class HtmlUnitSessionTag extends SessionTag implements HtmlUnitDelegate{
public static final String BASE_URL = "baseUrl";
public static final String BEGIN_AT = "beginAt";
protected WebClient session;
protected HtmlUnitHelper htmlHelper;
/**
* Enable or disable validity checking of SSL certificates.
* If "false", invalid SSL certs will be accepted. If not set, the default is "true".
* @jameleon.attribute contextName="htmlUnitEnableSslCertCheck"
*/
protected Boolean enableSslCertCheck;
/**
* The url to use when starting the browser.
* This can be the entire URL including the path or not, but it must start with the
* protocol (e.g. http://some.domain.com)
* @jameleon.attribute contextName="baseUrl"
*/
protected String baseUrl;
/**
* If set, this would be the path after the protocol and/or domain.
* (e.g. /some/path/to/a/file.html)
* @jameleon.attribute contextName="beginAt"
*/
protected String beginAt;
/**
* The port to use to connect to https. Usually leaving it as the default should do.
* @jameleon.attribute contextName="htmlUnitEnableSslCertCheckPort" default="443"
*/
protected Integer enableSslCertCheckPort;
public void setUpSession(){
htmlHelper = new HtmlUnitHelper(this);
session = new WebClient();
if (!isEnableSslCertCheck()) {
Protocol easyhttps = new Protocol("https", new TrustEverythingSSLProtocolSocketFactory(), getEnableSslCertCheckPort());
Protocol.registerProtocol("https", easyhttps);
}
}
public void tearDownSession(){
session = null;
}
/**
* Deregister this tag as a storable
*/
protected void deregisterStorable(){
StateStorer.getInstance().removeStorable(this);
}
/**
* Gets the url to request
*
* @return The URL to use in startApplication.
*/
protected String getRequestUrl(){
String url = baseUrl;
if (beginAt != null) {
url += beginAt;
}
return url;
}
/**
* Gets the current WebClient or the handle on the session
*
* @return the current browser.
*/
public WebClient getSession(){
return session;
}
/**
* Return the port on which to register the enableSslCertCheck on.
*/
public int getEnableSslCertCheckPort() {
return ContextHelper.getValueAsIntWithConfig(context, "htmlUnitEnableSslCertCheckPort", "htmlUnitEnableSslCertCheckPort", enableSslCertCheckPort.intValue());
}
/**
* Query whether SSL cert validity checking is enabled.
* @return - If true, an exception will be thrown when an invalid SSL cert is encountered.
* If false, invalid SSL certs will be accepted.
*/
public boolean isEnableSslCertCheck() {
if (enableSslCertCheck == null) {
String enableS = Configurator.getInstance().getValue("htmlUnitEnableSslCertCheck", "true");
enableSslCertCheck = new Boolean(enableS);
}
return enableSslCertCheck.booleanValue();
}
/**
* Register this tag as a storable
*/
protected void registerStorable(){
StateStorer.getInstance().addStorable(this);
}
/**
* Loads the URL given by <b>beginAt</b> and <b>baseUrl</b>.
*/
public void startApplication(){
registerStorable();
try{
URL url = new URL(getRequestUrl());
session.getPage(url);
}catch (MalformedURLException mue){
throw new RuntimeException("The provide url: '"+getRequestUrl()+"' is invalid", mue);
}catch (IOException ioe){
throw new RuntimeException("Can not connect to '"+getRequestUrl()+"'.", ioe);
}finally{
deregisterStorable();
}
}
public void store(String fName, int event) throws IOException{
htmlHelper.store(fName);
}
/**
* Gets a handle on the current web window.
*
* @return WebWindow
*/
public WebWindow getCurrentWebWindow(){
WebWindow currentWindow = null;
if (getSession() != null) {
currentWindow = getSession().getCurrentWindow();
}
return currentWindow;
}
/**
* Gets a handle on the WebClient instance used in this session
*
* @return WebClient
*/
public WebClient getWebClient(){
return session;
}
/**
* Gets a handle on the WebClient instance used in this session
*
* @return WebClient
*/
public HtmlForm getWorkingForm(){
return null;
}
}
See more files for this project here