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