PageRenderTime 29ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/AuthenticRoast/AuthenticRoast-Extras/src/main/java/name/aikesommer/authenticator/LogoutManager.java

http://authenticroast.googlecode.com/
Java | 106 lines | 57 code | 11 blank | 38 comment | 10 complexity | 87cd6526db419c7cb89480784e79ece4 MD5 | raw file
  1/**
  2 *    Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
  3 *
  4 *    This file is part of AuthenticRoast.
  5 *
  6 *    This library is free software; you can redistribute it and/or
  7 *    modify it under the terms of the GNU Lesser General Public
  8 *    License as published by the Free Software Foundation; either
  9 *    version 3 of the License, or (at your option) any later version.
 10 *
 11 *    This library is distributed in the hope that it will be useful,
 12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 *    Lesser General Public License for more details.
 15 *
 16 *    You should have received a copy of the GNU Lesser General
 17 *    Public License along with this library; if not, write to the
 18 *    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 19 *    Boston, MA 02110-1301 USA
 20 *
 21 *    You can reach the author and get more information about this
 22 *    project at: http://aikesommer.name/
 23 */
 24package name.aikesommer.authenticator;
 25
 26import java.io.UnsupportedEncodingException;
 27import java.net.URLEncoder;
 28import java.util.Map;
 29import java.util.Set;
 30import name.aikesommer.authenticator.AuthenticationRequest.ManageAction;
 31import name.aikesommer.authenticator.AuthenticationRequest.Status;
 32
 33/**
 34 * A very simple authenticator to allow for logouts.
 35 * Just have a link to "j_security_exit" as logout-link and this authenticator
 36 * should pick it up when clicked.
 37 * 
 38 * @author Aike J Sommer
 39 */
 40public class LogoutManager extends PluggableAuthenticator {
 41
 42    public static final String LOGOUT_ACTION = "/j_security_exit";
 43
 44    /**
 45     * Overwrite this to specify a different path to direct to after logging
 46     * the user out.
 47     */
 48    protected String getNextPath(AuthenticationManager manager, AuthenticationRequest request) {
 49        if (request.getHttpServletRequest().getParameter("_to") != null) {
 50            String to = request.getHttpServletRequest().getParameter("_to");
 51            String url = null;
 52            for (Map.Entry<String, String[]> entry : (Set<Map.Entry<String, String[]>>) request.
 53                    getHttpServletRequest().getParameterMap().entrySet()) {
 54                if (entry.getKey().startsWith("_p_") && entry.getKey().length() > 3 && entry.getValue().length > 0) {
 55                    String name;
 56                    String value;
 57                    try {
 58                        name = URLEncoder.encode(entry.getKey().substring(3), "UTF-8");
 59                        value = URLEncoder.encode(entry.getValue()[0], "UTF-8");
 60                    } catch (UnsupportedEncodingException ex) {
 61                        throw new RuntimeException(ex);
 62                    }
 63
 64                    String param = name + "=" + value;
 65                    url = url == null ? (to + "?" + param) : (url + "&" + param);
 66                }
 67            }
 68            if (url == null) {
 69                url = to;
 70            }
 71            return url;
 72        }
 73        return "/";
 74    }
 75
 76    /**
 77     * Overwrite this to perform anything necessary with your session-data. This
 78     * is called before the session is destroyed.
 79     */
 80    protected void onLogout(AuthenticationManager manager, AuthenticationRequest request) {
 81    }
 82
 83    @Override
 84    public Status tryAuthenticate(AuthenticationManager manager, AuthenticationRequest request) {
 85        return Status.None;
 86    }
 87
 88    @Override
 89    public Status authenticate(AuthenticationManager manager, AuthenticationRequest request) {
 90        return Status.None;
 91    }
 92
 93    @Override
 94    public ManageAction manage(AuthenticationManager manager, AuthenticationRequest request) {
 95        String path = request.getRequestPath();
 96        boolean logoutAction = path.endsWith(LOGOUT_ACTION);
 97
 98        if (logoutAction) {
 99            onLogout(manager, request);
100            manager.forward(request, getNextPath(manager, request));
101            return ManageAction.Clear;
102        }
103
104        return ManageAction.None;
105    }
106}