org.jboss.jsfunit.framework
Class FormAuthenticationStrategy

java.lang.Object
  extended by org.jboss.jsfunit.framework.SimpleInitialRequestStrategy
      extended by org.jboss.jsfunit.framework.FormAuthenticationStrategy
All Implemented Interfaces:
InitialRequestStrategy

public class FormAuthenticationStrategy
extends SimpleInitialRequestStrategy

Performs FORM authentication for JEE container-managed security. Note that the form-login-page and the form-error-page specified in web.xml don't have to be JSF pages.

This class needs to know the user name and password to log in. It also needs to know the name of a submit button or other component that can submit the form containing the login credentials. Because this might not be a JSF page, this class finds the "submit" component using the name attribute.

For non-JEE login, there is a second constructor that allows username and password components to have non-standard names.

Since:
1.0
Author:
Stan Silvert

Constructor Summary
FormAuthenticationStrategy(String userName, String password, String submitComponent)
          Create a new FormAuthenticationStrategy for JEE container-managed security.
FormAuthenticationStrategy(String userName, String password, String submitComponent, String userNameComponent, String passwordComponent)
          Create a new FormAuthenticationStrategy for non-JEE logins.
 
Method Summary
protected  com.gargoylesoftware.htmlunit.Page clickSubmitComponent(com.gargoylesoftware.htmlunit.html.HtmlPage page)
          Click the component needed to submit the form.
 com.gargoylesoftware.htmlunit.Page doInitialRequest(WebClientSpec wcSpec)
          Perform the initial request and provide FORM authentication credentials when challenged.
protected  com.gargoylesoftware.htmlunit.html.HtmlElement getElement(com.gargoylesoftware.htmlunit.html.HtmlPage page, String elementName)
          Find an element by its name attribute.
protected  void setValue(com.gargoylesoftware.htmlunit.html.HtmlPage page, String elementName, String value)
          Find an element by its name attribute and set its value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FormAuthenticationStrategy

public FormAuthenticationStrategy(String userName,
                                  String password,
                                  String submitComponent)
Create a new FormAuthenticationStrategy for JEE container-managed security. User name and password fields default to the JEE/Servlet naming standard of j_username and j_password.

Parameters:
userName - The user name.
password - The password.
submitComponent - The value of the name attribute for the submit component.

FormAuthenticationStrategy

public FormAuthenticationStrategy(String userName,
                                  String password,
                                  String submitComponent,
                                  String userNameComponent,
                                  String passwordComponent)
Create a new FormAuthenticationStrategy for non-JEE logins. Typically, this constructor is overridden in a subclass.

Parameters:
userName - The user name.
password - The password.
submitComponent - The value of the name attribute for the submit component.
userNameComponent - The value of the name attribute for the user name input.
passwordComponent - The value of the name attribute for the password input.
Method Detail

doInitialRequest

public com.gargoylesoftware.htmlunit.Page doInitialRequest(WebClientSpec wcSpec)
                                                    throws IOException
Perform the initial request and provide FORM authentication credentials when challenged.

Specified by:
doInitialRequest in interface InitialRequestStrategy
Overrides:
doInitialRequest in class SimpleInitialRequestStrategy
Parameters:
wcSpec - The WebClient specification.
Returns:
The requested page if authentication passed. Otherwise, return the error page specified in web.xml.
Throws:
IOException - if HtmlUnit encounters an error.

clickSubmitComponent

protected com.gargoylesoftware.htmlunit.Page clickSubmitComponent(com.gargoylesoftware.htmlunit.html.HtmlPage page)
                                                           throws IOException
Click the component needed to submit the form.

Parameters:
page - The page where credentials are entered.
Returns:
The resulting Page
Throws:
IOException - If the form can not be submitted.

setValue

protected void setValue(com.gargoylesoftware.htmlunit.html.HtmlPage page,
                        String elementName,
                        String value)
Find an element by its name attribute and set its value.

Parameters:
page - The page.
elementName - The value of the name attribute.
value - The value to set.

getElement

protected com.gargoylesoftware.htmlunit.html.HtmlElement getElement(com.gargoylesoftware.htmlunit.html.HtmlPage page,
                                                                    String elementName)
Find an element by its name attribute.

Parameters:
page - The page.
elementName - The value of the name attribute.
Returns:
The element found.
Throws:
IllegalArgumentException - if the element is not found or if more than one element has that value for the name attribute.


Copyright © 2007-2010 JBoss, a division of Red Hat, Inc.. All Rights Reserved.