KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > auth > AuthUtil


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.auth;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27
28 /**
29  * A collection of utility methods used by the <tt>com.methodhead.auth</tt>
30  * package.
31  */

32 public class AuthUtil {
33
34   // constructors /////////////////////////////////////////////////////////////
35

36   // constants ////////////////////////////////////////////////////////////////
37

38   // classes //////////////////////////////////////////////////////////////////
39

40   // methods //////////////////////////////////////////////////////////////////
41

42   /**
43    * Returns the URL (including query parameters) minus the scheme, host, and
44    * context path. This method probably be moved to a more general purpose
45    * class.
46    */

47   public static String JavaDoc getRelativeUrl(
48     HttpServletRequest JavaDoc request ) {
49
50     String JavaDoc baseUrl = null;
51
52     if ( ( request.getServerPort() == 80 ) ||
53          ( request.getServerPort() == 443 ) )
54       baseUrl =
55         request.getScheme() + "://" +
56         request.getServerName() +
57         request.getContextPath();
58     else
59       baseUrl =
60         request.getScheme() + "://" +
61         request.getServerName() + ":" + request.getServerPort() +
62         request.getContextPath();
63
64     StringBuffer JavaDoc buf = request.getRequestURL();
65
66     if ( request.getQueryString() != null ) {
67       buf.append( "?" );
68       buf.append( request.getQueryString() );
69     }
70
71     return buf.substring( baseUrl.length() );
72   }
73
74   /**
75    * Returns the current user in the session (the
76    * <tt>AuthGlobals.USER_KEY</tt> attribute).
77    */

78   public static AuthUser getUser(
79     HttpServletRequest JavaDoc request ) {
80
81     return
82       ( AuthUser )request.getSession().getAttribute( AuthGlobals.USER_KEY );
83   }
84
85   /**
86    * Sets the current user in the session (the
87    * <tt>AuthGlobals.USER_KEY</tt> attribute).
88    */

89   public static void setUser (
90     HttpServletRequest JavaDoc request,
91     AuthUser user ) {
92
93     request.getSession().setAttribute( AuthGlobals.USER_KEY, user );
94   }
95
96   /**
97    * Returns the forward named <tt>name</tt> or throws an exception
98    * with a message indicating the forward could not be found.
99    */

100   public static ActionForward findForward(
101     ActionMapping mapping,
102     String JavaDoc name )
103   throws
104     AuthException {
105
106     ActionForward forward = mapping.findForward( name );
107
108     if ( forward == null )
109       throw new AuthException( "Couldn't find \"" + name + "\" forward" );
110
111     return forward;
112   }
113
114   // properties ///////////////////////////////////////////////////////////////
115

116   // attributes ///////////////////////////////////////////////////////////////
117
}
118
Popular Tags