001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.authentication;
017:
018: import java.io.IOException;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import org.acegisecurity.ui.logout.LogoutFilter;
022: import org.acegisecurity.ui.logout.LogoutHandler;
023: import org.apache.commons.lang.StringUtils;
024: import org.jamwiki.Environment;
025: import org.jamwiki.WikiBase;
026: import org.jamwiki.model.VirtualWiki;
027: import org.jamwiki.utils.WikiLogger;
028: import org.jamwiki.utils.WikiUtil;
029:
030: /**
031: * This class is a hack implemented to work around the fact that the default
032: * Acegi classes can only redirect to a single, hard-coded URL. Due to the
033: * fact that JAMWiki may have multiple virtual wikis this class overrides some
034: * of the default Acegi behavior to allow additional flexibility. Hopefully
035: * future versions of Acegi will add additional flexibility and this class
036: * can be removed.
037: */
038: public class JAMWikiLogoutFilter extends LogoutFilter {
039:
040: /** Standard logger. */
041: private static final WikiLogger logger = WikiLogger
042: .getLogger(JAMWikiLogoutFilter.class.getName());
043:
044: /**
045: *
046: */
047: public JAMWikiLogoutFilter(String logoutSuccessUrl,
048: LogoutHandler[] handlers) {
049: super (logoutSuccessUrl, handlers);
050: }
051:
052: /**
053: * FIXME - override the parent method to determine if processing should
054: * occur. Needed due to the fact that different virtual wikis may be
055: * used.
056: */
057: protected boolean requiresLogout(HttpServletRequest request,
058: HttpServletResponse response) {
059: String uri = request.getRequestURI();
060: // FIXME - move the "strip after semicolon" code to WikiUtil
061: int pathParamIndex = uri.indexOf(';');
062: if (pathParamIndex > 0) {
063: // strip everything after the first semi-colon
064: uri = uri.substring(0, pathParamIndex);
065: }
066: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
067: return uri.endsWith(request.getContextPath() + "/"
068: + virtualWiki + this .getFilterProcessesUrl());
069: }
070:
071: /**
072: * Allow subclasses to modify the redirection message.
073: *
074: * @param request the request
075: * @param response the response
076: * @param url the URL to redirect to
077: * @throws IOException in the event of any failure
078: */
079: protected void sendRedirect(HttpServletRequest request,
080: HttpServletResponse response, String url)
081: throws IOException {
082: String targetUrl = url;
083: if ("/DEFAULT_VIRTUAL_WIKI".equals(url)) {
084: // ugly, but a hard-coded constant seems to be the only way to
085: // allow a dynamic url value
086: String virtualWikiName = WikiUtil
087: .getVirtualWikiFromURI(request);
088: if (StringUtils.isBlank(virtualWikiName)) {
089: virtualWikiName = WikiBase.DEFAULT_VWIKI;
090: }
091: String topicName = Environment
092: .getValue(Environment.PROP_BASE_DEFAULT_TOPIC);
093: try {
094: VirtualWiki virtualWiki = WikiBase.getDataHandler()
095: .lookupVirtualWiki(virtualWikiName);
096: topicName = virtualWiki.getDefaultTopicName();
097: } catch (Exception e) {
098: logger
099: .warning(
100: "Unable to retrieve default topic for virtual wiki",
101: e);
102: }
103: targetUrl = request.getContextPath() + "/"
104: + virtualWikiName + "/" + topicName;
105: } else if (url != null && !url.startsWith("http://")
106: && !url.startsWith("https://")) {
107: String virtualWiki = WikiUtil
108: .getVirtualWikiFromURI(request);
109: targetUrl = request.getContextPath() + "/" + virtualWiki
110: + url;
111: }
112: response.sendRedirect(response.encodeRedirectURL(targetUrl));
113: }
114: }
|