001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/security/control/GetUserInfoListener.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 package org.deegree.portal.standard.security.control;
037
038 import java.io.IOException;
039 import java.net.URL;
040
041 import javax.servlet.http.HttpServletRequest;
042 import javax.servlet.http.HttpSession;
043
044 import org.deegree.enterprise.control.ajax.AbstractListener;
045 import org.deegree.enterprise.control.ajax.ResponseHandler;
046 import org.deegree.enterprise.control.ajax.WebEvent;
047 import org.deegree.framework.log.ILogger;
048 import org.deegree.framework.log.LoggerFactory;
049 import org.deegree.framework.util.StringTools;
050 import org.deegree.framework.xml.NamespaceContext;
051 import org.deegree.framework.xml.XMLFragment;
052 import org.deegree.framework.xml.XMLParsingException;
053 import org.deegree.framework.xml.XMLTools;
054 import org.deegree.ogcbase.BaseURL;
055 import org.deegree.ogcbase.CommonNamespaces;
056 import org.deegree.ogcwebservices.OWSUtils;
057 import org.deegree.portal.Constants;
058 import org.deegree.portal.context.GeneralExtension;
059 import org.deegree.portal.context.ViewContext;
060 import org.xml.sax.SAXException;
061
062 /**
063 * TODO add class documentation here
064 *
065 * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
066 * @author last edited by: $Author: apoth $
067 *
068 * @version $Revision: 22505 $, $Date: 2010-02-11 11:33:00 +0100 (Do, 11. Feb 2010) $
069 */
070 public class GetUserInfoListener extends AbstractListener {
071
072 private static final ILogger LOG = LoggerFactory.getLogger( GetUserInfoListener.class );
073
074 private static final NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
075
076 /*
077 * (non-Javadoc)
078 *
079 * @see
080 * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
081 * , org.deegree.enterprise.control.ajax.ResponseHandler)
082 */
083 public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
084 throws IOException {
085
086 HttpSession session = event.getSession();
087 try {
088 UserBean user = readUserInformation( session );
089 responseHandler.writeAndClose( false, user );
090 } catch ( Exception e ) {
091 LOG.logError( e );
092 responseHandler.writeAndClose( "ERROR: can not read user informations" );
093 }
094 }
095
096 /**
097 * gets the user name assigned to the passed session ID from an authentication service. If no user is assigned to
098 * the session ID <tt>null</tt> will be returned. If the session is closed or expired an exception will be thrown
099 *
100 * @param session
101 * @return name of the user assigned to the passed session ID
102 * @throws XMLParsingException
103 * @throws SAXException
104 * @throws IOException
105 */
106 protected UserBean readUserInformation( HttpSession session )
107 throws XMLParsingException, IOException, SAXException {
108
109 String sessionId = null;
110 ViewContext vc = (ViewContext) session.getAttribute( Constants.CURRENTMAPCONTEXT );
111 if ( vc == null ) {
112 return null;
113 }
114 GeneralExtension ge = vc.getGeneral().getExtension();
115 UserBean user = null;
116
117 if ( sessionId != null && ge.getAuthentificationSettings() != null ) {
118 LOG.logDebug( "try getting user from WAS/sessionID" );
119 BaseURL baseUrl = ge.getAuthentificationSettings().getAuthentificationURL();
120 String url = OWSUtils.validateHTTPGetBaseURL( baseUrl.getOnlineResource().toExternalForm() );
121 StringBuffer sb = new StringBuffer( url );
122 sb.append( "request=DescribeUser&SESSIONID=" ).append( sessionId );
123
124 XMLFragment xml = new XMLFragment();
125 xml.load( new URL( sb.toString() ) );
126
127 String userName = XMLTools.getRequiredNodeAsString( xml.getRootElement(), "/User/UserName", nsContext );
128 String firstName = XMLTools.getRequiredNodeAsString( xml.getRootElement(), "/User/FirstName", nsContext );
129 String lastName = XMLTools.getRequiredNodeAsString( xml.getRootElement(), "/User/LastName", nsContext );
130 String mailAddress = XMLTools.getRequiredNodeAsString( xml.getRootElement(), "/User/EMailAddress",
131 nsContext );
132 user = new UserBean( userName, firstName, lastName, mailAddress );
133 } else {
134 LOG.logDebug( "try getting user from getUserPrincipal()" );
135 if ( ( (HttpServletRequest) getRequest() ).getUserPrincipal() != null ) {
136 String userName = ( (HttpServletRequest) getRequest() ).getUserPrincipal().getName();
137 if ( userName.indexOf( "\\" ) > 1 ) {
138 String[] us = StringTools.toArray( userName, "\\", false );
139 userName = us[us.length - 1];
140 }
141 }
142 }
143 LOG.logDebug( "userName: " + user.getUserName() );
144 return user;
145 }
146
147 // ////////////////////////////////////////////////////////////////////////////////
148 // inner classes
149 // ////////////////////////////////////////////////////////////////////////////////
150
151 public class UserBean {
152 private String userName = null;
153
154 private String firstName = null;
155
156 private String lastName = null;
157
158 private String mailAddress = null;
159
160 /**
161 * @param userName
162 * @param firstName
163 * @param lastName
164 * @param mailAddress
165 */
166 public UserBean( String userName, String firstName, String lastName, String mailAddress ) {
167 super();
168 this.userName = userName;
169 this.firstName = firstName;
170 this.lastName = lastName;
171 this.mailAddress = mailAddress;
172 }
173
174 /**
175 * @return the userName
176 */
177 public String getUserName() {
178 return userName;
179 }
180
181 /**
182 * @param userName
183 * the userName to set
184 */
185 public void setUserName( String userName ) {
186 this.userName = userName;
187 }
188
189 /**
190 * @return the firstName
191 */
192 public String getFirstName() {
193 return firstName;
194 }
195
196 /**
197 * @param firstName
198 * the firstName to set
199 */
200 public void setFirstName( String firstName ) {
201 this.firstName = firstName;
202 }
203
204 /**
205 * @return the lastName
206 */
207 public String getLastName() {
208 return lastName;
209 }
210
211 /**
212 * @param lastName
213 * the lastName to set
214 */
215 public void setLastName( String lastName ) {
216 this.lastName = lastName;
217 }
218
219 /**
220 * @return the mailAddress
221 */
222 public String getMailAddress() {
223 return mailAddress;
224 }
225
226 /**
227 * @param mailAddress
228 * the mailAddress to set
229 */
230 public void setMailAddress( String mailAddress ) {
231 this.mailAddress = mailAddress;
232 }
233
234 }
235
236 }