001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/control/ajax/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 20969 2009-11-23 15:51:29Z apoth $
046    package org.deegree.enterprise.control.ajax;
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     * Encapsulates all client information.
057     * <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: 20969 $ $Date: 2009-11-23 16:51:29 +0100 (Mo, 23 Nov 2009) $
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 of the request object.
076         * 
077         * @param request
078         *            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         * 
087         * @return Remote user
088         */
089        public String getRemoteUser() {
090            return this.userData.getProperty( "RemoteUser" );
091        }
092    
093        /**
094         * @return Remote address
095         */
096        public String getRemoteAddr() {
097            return this.userData.getProperty( "RemoteAddr" );
098        }
099    
100        /**
101         * @return Remote host
102         */
103        public String getRemoteHost() {
104            return this.userData.getProperty( "RemoteHost" );
105        }
106    
107        /**
108         * @return Authorization scheme
109         */
110        public String getAuthType() {
111            return this.userData.getProperty( "AuthType" );
112        }
113    
114        /**
115         * @return the user principal
116         */
117        public String getUserPrincipal() {
118            Object _obj = userData.get( "UserPrincipal" );
119    
120            if ( _obj instanceof java.security.Principal ) {
121                java.security.Principal _principal = (java.security.Principal) _obj;
122                return _principal.getName();
123            } else if ( _obj instanceof String ) {
124                return (String) _obj;
125            }
126    
127            return _obj.toString();
128        }
129    
130        /**
131         * Parse request object for user specific attributes.
132         * 
133         * @param request
134         *            to parse
135         */
136        protected void parseRequest( HttpServletRequest request ) {
137            try {
138                this.userData.setProperty( "RemoteUser", (String) getRequestValue( request, "getRemoteUser", "[unknown]" ) );
139    
140                this.userData.setProperty( "RemoteAddr", (String) getRequestValue( request, "getRemoteAddr", "[unknown]" ) );
141    
142                this.userData.setProperty( "RemoteHost", (String) getRequestValue( request, "getRemoteHost", "[unknown]" ) );
143    
144                this.userData.setProperty( "AuthType", (String) getRequestValue( request, "getAuthType", "[unknown]" ) );
145    
146                this.userData.put( "UserPrincipal", getRequestValue( request, "getUserPrincipal", "[unknown]" ) );
147            } catch ( Exception ex ) {
148                ex.printStackTrace();
149            }
150        }
151    
152        /**
153         * 
154         * 
155         * @param request
156         * @param methodName
157         * @param defaultValue
158         * 
159         * @return the value of a request or if the request is null, the methodName is null or empty, <code>null</code>
160         *         will be returned.
161         * 
162         * @throws NoSuchMethodException
163         * @throws InvocationTargetException
164         * @throws IllegalAccessException
165         */
166        protected Object getRequestValue( HttpServletRequest request, String methodName, Object defaultValue )
167                                throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
168            if ( ( request != null ) && ( methodName != null ) && !methodName.equals( "" ) ) {
169                // System.err.println( "looking for :" + methodName );
170                // use refection for method
171                Method _objmethod = request.getClass().getMethod( methodName, new Class[] {} );
172    
173                // System.err.println( "got :" + _objmethod.getName() );
174                // get the result of the method invocation
175                Object _result = _objmethod.invoke( request, new Object[] {} );
176    
177                // System.err.println( "returns :" + _result );
178                if ( _result != null ) {
179                    return _result;
180                }
181                return defaultValue;
182            }
183    
184            return null;
185        }
186    }