001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: AbstractLogout.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.elements;
009:
010: import com.uwyn.rife.authentication.RememberManager;
011: import com.uwyn.rife.authentication.SessionManager;
012: import com.uwyn.rife.authentication.elements.exceptions.UndefinedLogoutRememberManagerException;
013: import com.uwyn.rife.authentication.exceptions.RememberManagerException;
014: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
015: import com.uwyn.rife.engine.Element;
016: import com.uwyn.rife.engine.exceptions.EngineException;
017: import com.uwyn.rife.engine.exceptions.PropertyRequiredException;
018: import javax.servlet.http.Cookie;
019:
020: public abstract class AbstractLogout extends Element {
021: protected SessionManager mSessionManager = null;
022: protected RememberManager mRememberManager = null;
023:
024: protected AbstractLogout() {
025: }
026:
027: protected void setSessionManager(SessionManager sessionValidator) {
028: assert sessionValidator != null;
029:
030: mSessionManager = sessionValidator;
031: }
032:
033: public SessionManager getSessionManager() {
034: return mSessionManager;
035: }
036:
037: public void setRememberManager(RememberManager rememberManager) {
038: mRememberManager = rememberManager;
039: }
040:
041: public RememberManager getRememberManager() {
042: return mRememberManager;
043: }
044:
045: protected void performLogout() {
046: if (!hasProperty("authvar_name")) {
047: throw new PropertyRequiredException(getDeclarationName(),
048: "authvar_name");
049: }
050: if (!hasProperty("remembervar_name")) {
051: throw new PropertyRequiredException(getDeclarationName(),
052: "remembervar_name");
053: }
054:
055: String authvar_name = getPropertyString("authvar_name");
056: boolean has_authvar_input = getElementInfo()
057: .containsInputPossibility(authvar_name);
058: boolean has_authvar_cookie = getElementInfo()
059: .containsIncookiePossibility(authvar_name);
060:
061: String authid = null;
062: if (has_authvar_cookie) {
063: Cookie cookie = getCookie(authvar_name);
064: if (cookie != null) {
065: authid = cookie.getValue();
066: }
067: }
068: if (has_authvar_input
069: && (null == authid || 0 == authid.length())) {
070: authid = getInput(authvar_name);
071: }
072:
073: if (authid != null) {
074: try {
075: mSessionManager.eraseSession(authid);
076: } catch (SessionManagerException e) {
077: throw new EngineException(e);
078: }
079:
080: // clear remember id cookie for the user
081: String remembervar_name = getPropertyString("remembervar_name");
082: if (getElementInfo().containsIncookiePossibility(
083: remembervar_name)
084: && hasCookie(remembervar_name)) {
085: if (null == mRememberManager) {
086: throw new UndefinedLogoutRememberManagerException();
087: }
088:
089: try {
090: mRememberManager
091: .eraseRememberId(getCookieValue(remembervar_name));
092: } catch (RememberManagerException e) {
093: throw new EngineException(e);
094: }
095:
096: Cookie cookie = getCookie(remembervar_name);
097: cookie.setMaxAge(-1);
098: cookie.setPath("/");
099: cookie.setValue("");
100: setCookie(cookie);
101: }
102:
103: // clear child trigger value
104: if (has_authvar_cookie && hasCookie(authvar_name)) {
105: Cookie cookie = getCookie(authvar_name);
106: cookie.setMaxAge(-1);
107: cookie.setPath("/");
108: cookie.setValue("");
109: setCookie(cookie);
110: }
111: if (has_authvar_input) {
112: clearOutput(authvar_name);
113: }
114: }
115:
116: removeRequestAttribute(Identified.IDENTITY_ATTRIBUTE_NAME);
117: }
118: }
|