001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/control/RequestUser.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2008 by:
006    EXSE, Department of Geography, University of Bonn
007    http://www.giub.uni-bonn.de/deegree/
008    lat/lon GmbH
009    http://www.lat-lon.de
010    
011    This library is free software; you can redistribute it and/or
012    modify it under the terms of the GNU Lesser General Public
013    License as published by the Free Software Foundation; either
014    version 2.1 of the License, or (at your option) any later version.
015    
016    This library is distributed in the hope that it will be useful,
017    but WITHOUT ANY WARRANTY; without even the implied warranty of
018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019    Lesser General Public License for more details.
020    
021    You should have received a copy of the GNU Lesser General Public
022    License along with this library; if not, write to the Free Software
023    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025    Contact:
026    
027    Andreas Poth
028    lat/lon GmbH
029    Aennchenstr. 19
030    53115 Bonn
031    Germany
032    E-Mail: poth@lat-lon.de
033    
034    Prof. Dr. Klaus Greve
035    Department of Geography
036    University of Bonn
037    Meckenheimer Allee 166
038    53115 Bonn
039    Germany
040    E-Mail: greve@giub.uni-bonn.de
041    
042                     
043     ---------------------------------------------------------------------------*/
044    
045    // $Id: RequestUser.java 9338 2007-12-27 12:31:31Z apoth $
046    package org.deegree.enterprise.control;
047    
048    // JDK 1.3
049    import java.lang.reflect.InvocationTargetException;
050    import java.lang.reflect.Method;
051    import java.util.Properties;
052    
053    import javax.servlet.http.HttpServletRequest;
054    
055    
056    /**
057     * Encapsulates all client information.<P>
058     *
059     * @author  <a href="mailto:friebe@gmx.net">Torsten Friebe</a>
060     * @author  <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     *
062     * @version $Revision: 9338 $ $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $
063     */
064    class RequestUser {
065        private Properties userData;
066    
067        /**
068         * Creates an empty object.
069         */
070        protected RequestUser() {
071            this.userData = new Properties();
072        }
073    
074        /**
075         * Creates a request user object with client information retrieved out
076         * of the request object.
077         *
078         * @param   request   the request object containing user and client data
079         */
080        public RequestUser( HttpServletRequest request ) {
081            this.userData = new Properties();
082            this.parseRequest( request );
083        }
084    
085        /**
086         * Remote user
087         */
088        public String getRemoteUser() {
089            return this.userData.getProperty( "RemoteUser" );
090        }
091    
092        /**
093         * Remote address
094         */
095        public String getRemoteAddr() {
096            return this.userData.getProperty( "RemoteAddr" );
097        }
098    
099        /**
100         * Remote host
101         */
102        public String getRemoteHost() {
103            return this.userData.getProperty( "RemoteHost" );
104        }
105    
106        /**
107         * Authorization scheme
108         */
109        public String getAuthType() {
110            return this.userData.getProperty( "AuthType" );
111        }
112    
113        /**
114         * Authenticated user
115         */
116        public String getUserPrincipal() {
117            Object _obj = userData.get( "UserPrincipal" );
118    
119            if ( _obj instanceof java.security.Principal ) {
120                java.security.Principal _principal = (java.security.Principal)_obj;
121                return _principal.getName();
122            } else if ( _obj instanceof String ) {
123                return (String)_obj;
124            }
125    
126            return _obj.toString();
127        }
128    
129        /**
130         * Parse request object for user specific attributes.
131         */
132        protected void parseRequest( HttpServletRequest request ) {
133            try {
134                this.userData.setProperty( "RemoteUser", 
135                                           (String)getRequestValue( request, "getRemoteUser", 
136                                                                    "[unknown]" ) );
137    
138                this.userData.setProperty( "RemoteAddr", 
139                                           (String)getRequestValue( request, "getRemoteAddr", 
140                                                                    "[unknown]" ) );
141    
142                this.userData.setProperty( "RemoteHost", 
143                                           (String)getRequestValue( request, "getRemoteHost", 
144                                                                    "[unknown]" ) );
145    
146                this.userData.setProperty( "AuthType", 
147                                           (String)getRequestValue( request, "getAuthType", "[unknown]" ) );
148    
149                this.userData.put( "UserPrincipal", 
150                                   getRequestValue( request, "getUserPrincipal", "[unknown]" ) );
151            } catch ( Exception ex ) {
152                ex.printStackTrace();
153            }
154        }
155    
156        /**
157         *
158         *
159         * @param request 
160         * @param methodName 
161         * @param defaultValue 
162         *
163         * @return 
164         *
165         * @throws NoSuchMethodException 
166         * @throws InvocationTargetException 
167         * @throws IllegalAccessException 
168         */
169        protected Object getRequestValue( HttpServletRequest request, String methodName, 
170                                          Object defaultValue ) throws NoSuchMethodException, 
171                                                                       InvocationTargetException, 
172                                                                       IllegalAccessException {
173            if ( ( request != null ) && ( methodName != null ) && !methodName.equals( "" ) ) {
174                //      System.err.println( "looking for :" + methodName );
175                // use refection for method
176                Method _objmethod = request.getClass().getMethod( methodName, new Class[]{});
177    
178                //      System.err.println( "got :" + _objmethod.getName()  );
179                // get the result of the method invocation
180                Object _result = _objmethod.invoke( request, new Object[] {} );
181    
182                //      System.err.println( "returns :" + _result  );
183                if ( _result != null ) {
184                    return _result;
185                } 
186                return defaultValue;
187            }
188    
189            return null;
190        }
191    }