001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.auth;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.struts.action.ActionForward;
026: import org.apache.struts.action.ActionMapping;
027:
028: /**
029: * A collection of utility methods used by the <tt>com.methodhead.auth</tt>
030: * package.
031: */
032: public class AuthUtil {
033:
034: // constructors /////////////////////////////////////////////////////////////
035:
036: // constants ////////////////////////////////////////////////////////////////
037:
038: // classes //////////////////////////////////////////////////////////////////
039:
040: // methods //////////////////////////////////////////////////////////////////
041:
042: /**
043: * Returns the URL (including query parameters) minus the scheme, host, and
044: * context path. This method probably be moved to a more general purpose
045: * class.
046: */
047: public static String getRelativeUrl(HttpServletRequest request) {
048:
049: String baseUrl = null;
050:
051: if ((request.getServerPort() == 80)
052: || (request.getServerPort() == 443))
053: baseUrl = request.getScheme() + "://"
054: + request.getServerName()
055: + request.getContextPath();
056: else
057: baseUrl = request.getScheme() + "://"
058: + request.getServerName() + ":"
059: + request.getServerPort()
060: + request.getContextPath();
061:
062: StringBuffer buf = request.getRequestURL();
063:
064: if (request.getQueryString() != null) {
065: buf.append("?");
066: buf.append(request.getQueryString());
067: }
068:
069: return buf.substring(baseUrl.length());
070: }
071:
072: /**
073: * Returns the current user in the session (the
074: * <tt>AuthGlobals.USER_KEY</tt> attribute).
075: */
076: public static AuthUser getUser(HttpServletRequest request) {
077:
078: return (AuthUser) request.getSession().getAttribute(
079: AuthGlobals.USER_KEY);
080: }
081:
082: /**
083: * Sets the current user in the session (the
084: * <tt>AuthGlobals.USER_KEY</tt> attribute).
085: */
086: public static void setUser(HttpServletRequest request, AuthUser user) {
087:
088: request.getSession().setAttribute(AuthGlobals.USER_KEY, user);
089: }
090:
091: /**
092: * Returns the forward named <tt>name</tt> or throws an exception
093: * with a message indicating the forward could not be found.
094: */
095: public static ActionForward findForward(ActionMapping mapping,
096: String name) throws AuthException {
097:
098: ActionForward forward = mapping.findForward(name);
099:
100: if (forward == null)
101: throw new AuthException("Couldn't find \"" + name
102: + "\" forward");
103:
104: return forward;
105: }
106:
107: // properties ///////////////////////////////////////////////////////////////
108:
109: // attributes ///////////////////////////////////////////////////////////////
110: }
|