PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/AuthenticRoast/AuthenticRoast-Impl/src/main/java/name/aikesommer/authenticator/RequestHandler.java

http://authenticroast.googlecode.com/
Java | 114 lines | 67 code | 17 blank | 30 comment | 14 complexity | 9df43b3aee7761c68ef635cbd09c24f8 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.util.ArrayList;
 27import java.util.List;
 28import java.util.Map;
 29
 30/**
 31 * This class takes care of storing and restoring the original request, that
 32 * led to the authentication being started.
 33 * It is used internally only.
 34 *
 35 * @author Aike J Sommer
 36 */
 37public class RequestHandler {
 38
 39    private static final String REQUEST_CONTEXT_NOTE = RequestHandler.class.getName() + ".REQUEST_CONTEXT";
 40    private static final String REQUEST_PATH_NOTE = RequestHandler.class.getName() + ".REQUEST_PATH";
 41    private static final String REQUEST_QUERY_NOTE = RequestHandler.class.getName() + ".REQUEST_QUERY";
 42    private static final String REQUEST_ADD_QUERY_NOTE = RequestHandler.class.getName() + ".REQUEST_ADD_QUERY";
 43
 44    private Map<String, Object> session(AuthenticationRequest request) {
 45        return request.getAuthenticationMap();
 46    }
 47
 48    public void saveRequest(ModifiableRequest request) {
 49        session(request).put(REQUEST_CONTEXT_NOTE, request.getOriginalContext().getContextPath());
 50        session(request).put(REQUEST_PATH_NOTE, request.getRequestPath());
 51        session(request).put(REQUEST_QUERY_NOTE, request.getHttpServletRequest().getQueryString());
 52    }
 53
 54    public void saveRequest(ModifiableRequest request, String path) {
 55        session(request).put(REQUEST_CONTEXT_NOTE, request.getOriginalContext().getContextPath());
 56        session(request).put(REQUEST_PATH_NOTE, path);
 57        session(request).put(REQUEST_QUERY_NOTE, "");
 58    }
 59
 60    public void clearRequest(AuthenticationRequest request) {
 61        session(request).remove(REQUEST_CONTEXT_NOTE);
 62        session(request).remove(REQUEST_PATH_NOTE);
 63        session(request).remove(REQUEST_QUERY_NOTE);
 64    }
 65
 66    public String getContextForRequest(AuthenticationRequest request) {
 67        String context = (String) session(request).get(REQUEST_CONTEXT_NOTE);
 68        return context;
 69    }
 70
 71    public String getPathForRequest(AuthenticationRequest request) {
 72        String path = (String) session(request).get(REQUEST_PATH_NOTE);
 73        String query = (String) session(request).get(REQUEST_QUERY_NOTE);
 74
 75        List<String> addQuery = (List<String>) session(request).get(REQUEST_ADD_QUERY_NOTE);
 76        if (addQuery != null) {
 77            for (String q : addQuery) {
 78                query = query == null ? q : (query + "&" + q);
 79            }
 80        }
 81
 82        if (path != null) {
 83            return path + (query == null ? "" : ("?" + query));
 84        }
 85        return null;
 86    }
 87
 88    public boolean matchesRequest(ModifiableRequest request) {
 89        String originalPath = (String) session(request).get(REQUEST_PATH_NOTE);
 90        String path = request.getRequestPath();
 91        String originalContext = (String) session(request).get(REQUEST_CONTEXT_NOTE);
 92        String context = request.getOriginalContext().getContextPath();
 93
 94        if (originalPath != null && originalContext != null) {
 95            return path.equals(originalPath) && context.equals(originalContext);
 96        }
 97
 98        return false;
 99    }
100
101    public void restoreRequest(AuthenticationRequest request) {
102        clearRequest(request);
103    }
104
105    public void addQueryString(AuthenticationRequest request, String queryString) {
106        List<String> addQuery = (List) session(request).get(REQUEST_ADD_QUERY_NOTE);
107        if (addQuery == null) {
108            addQuery = new ArrayList<String>();
109            session(request).put(REQUEST_ADD_QUERY_NOTE, addQuery);
110        }
111        addQuery.add(queryString);
112    }
113
114}