FileDocCategorySizeDatePackage
AuthenticateAction.javaAPI DocExample5893Tue Jun 08 11:26:42 BST 2004com.mycompany.messages

AuthenticateAction

public class AuthenticateAction extends Object
This class performs authentication in the Project Billboard application.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
private String
username
private String
password
private boolean
remember
private boolean
rememberSet
private EmployeeRegistryBean
empReg
private String
origURL
Constructors Summary
Methods Summary
public java.lang.Stringauthenticate()
Autheticates a user with help from the EmployeeRegistryBean. If the user can be authenticated, the "validUser" session attribute is set to an instance of the EmployeeBean, to serve as an authentication token in this application.

Cookies with the user name and password are set or reset as specified by the "remember" request parameter.


	String result = null;
	FacesContext context = FacesContext.getCurrentInstance();

	EmployeeBean emp = empReg.authenticate(username, password);
	if (emp != null) {
	    Map sessionMap = 
		context.getExternalContext().getSessionMap();
	    sessionMap.put("validUser", emp);
	    setLoginCookies(context, remember, username, password);
                
	    // Next page is the originally requested URL or main
	    if (origURL != null && origURL.length() != 0) {
		String newPath = context.getApplication().getViewHandler().
		    getActionURL(context, origURL);
		try {
		    context.getExternalContext().redirect(newPath);
		}
		catch (IOException e) {}
		context.responseComplete();
		result = "newViewSet";
	    }
	    else {
		result = "success";
	    }
	}
	else {
	    // Invalid login.
	    FacesMessage msg = 
		new FacesMessage(FacesMessage.SEVERITY_ERROR,
				 "Invalid username or password", null);
	    context.addMessage(null, msg);
	    result = "failure";
	}
	return result;
    
public java.lang.StringgetOrigURL()
Returns the originally requested URL, or null if none is set.

	// Need to do this because the access control filter uses a
	// different parameter name than the login form
	if (origURL == null) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    Map param = 
		context.getExternalContext().getRequestParameterMap();
	    String s = (String) param.get("origURL");
	    if (s != null) {
		origURL = s;
	    }
	}
	return origURL;
    
public java.lang.StringgetPassword()
Returns the current password, or the value of a "password" cookie if no username is set.

	if (password == null) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    Map cookies = context.getExternalContext().getRequestCookieMap();
	    Cookie c = (Cookie) cookies.get("password");
	    if (c != null) {
		password = c.getValue();
	    }
	}
	return password;
    
public booleangetRemember()
Returns the "remember" property value, or "true" if this property isn't set and a "password" cookie has a value.

	if (!rememberSet) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    Map cookies = context.getExternalContext().getRequestCookieMap();
	    Cookie c = (Cookie) cookies.get("password");
	    if (c != null) {
		remember = true;
	    }
	}
	return remember;
    
public java.lang.StringgetUsername()
Returns the current username, or the value of a "username" cookie if no username is set.

	if (username == null) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    Map cookies = context.getExternalContext().getRequestCookieMap();
	    Cookie c = (Cookie) cookies.get("username");
	    if (c != null) {
		username = c.getValue();
	    }
	}
	return username;
    
private voidsetLoginCookies(javax.faces.context.FacesContext context, boolean remember, java.lang.String username, java.lang.String password)
Set or "delete" the login cookies, depending on the value of the "remember" parameter.


	HttpServletRequest request = 
	    (HttpServletRequest) context.getExternalContext().getRequest();
	HttpServletResponse response = 
	    (HttpServletResponse) context.getExternalContext().getResponse();
	Cookie usernameCookie = new Cookie("username", username);
	Cookie passwordCookie = new Cookie("password", password);
	// Cookie age in seconds: 30 days * 24 hours * 60 minutes * 60 seconds
	int maxAge = 30 * 24 * 60 * 60;
	if (!remember) {
	    // maxAge = 0 to delete the cookie
	    maxAge = 0;
	}
	usernameCookie.setMaxAge(maxAge);
	passwordCookie.setMaxAge(maxAge);
	usernameCookie.setPath(request.getContextPath());
	passwordCookie.setPath(request.getContextPath());
	response.addCookie(usernameCookie);
	response.addCookie(passwordCookie);
    
public voidsetOrigURL(java.lang.String origURL)
Sets the originally requested URL.

	this.origURL = origURL;
    
public voidsetPassword(java.lang.String password)
Sets the password.

	this.password = password;
    
public voidsetRegistry(EmployeeRegistryBean empReg)
Sets the registry holding user information.

	this.empReg = empReg;
    
public voidsetRemember(boolean remember)
Sets the "remember" property value.

	this.remember = remember;
	rememberSet = true;
    
public voidsetUsername(java.lang.String username)
Sets the username.

	this.username = username;