001 //$$Header: $$ 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 package org.deegree.enterprise.servlet; 038 039 import java.io.File; 040 import java.io.IOException; 041 import java.io.Serializable; 042 import java.util.ArrayList; 043 import java.util.Iterator; 044 import java.util.List; 045 import java.util.Map; 046 import java.util.UUID; 047 048 import javax.servlet.ServletException; 049 import javax.servlet.http.HttpServlet; 050 import javax.servlet.http.HttpServletRequest; 051 import javax.servlet.http.HttpServletResponse; 052 import javax.servlet.http.HttpSession; 053 054 import org.deegree.framework.log.ILogger; 055 import org.deegree.framework.log.LoggerFactory; 056 import org.deegree.framework.util.StringTools; 057 import org.deegree.framework.xml.XMLFragment; 058 import org.deegree.framework.xml.XSLTDocument; 059 import org.deegree.portal.owswatch.Constants; 060 import org.deegree.portal.owswatch.JSPagesReference; 061 import org.deegree.portal.owswatch.Messages; 062 import org.deegree.portal.owswatch.ServiceConfiguration; 063 import org.deegree.portal.owswatch.ServiceWatcher; 064 import org.deegree.portal.owswatch.ServiceWatcherFactory; 065 import org.deegree.portal.owswatch.configs.OwsWatchConfig; 066 import org.deegree.portal.owswatch.configs.User; 067 068 /** 069 * Used to authenticate the user in order to view the Protocol file 070 * 071 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a> 072 * @author last edited by: $Author: elmasry $ 073 * 074 * @version $Revision: 1.3 $, $Date: 2008-03-07 16:31:02 $ 075 */ 076 public class ProtocolServlet extends HttpServlet implements Serializable { 077 078 private static final ILogger LOG = LoggerFactory.getLogger( ProtocolServlet.class ); 079 080 private final String SESSIONID_KEY = Constants.SESSIONID_KEY; 081 082 private ServiceWatcher watcher = null; 083 084 private String webinfPath = null; 085 086 private String confFilePath = null; 087 088 private ServiceWatcherFactory factory = null; 089 090 private OwsWatchConfig conf = null; 091 092 /** 093 * 094 */ 095 private static final long serialVersionUID = -6509717095713986594L; 096 097 /* 098 * (non-Javadoc) 099 * 100 * @see javax.servlet.GenericServlet#init() 101 */ 102 @Override 103 public void init() 104 throws ServletException { 105 confFilePath = this.getServletContext().getRealPath( this.getInitParameter( "owsWatchConfiguration" ) ); 106 webinfPath = this.getServletContext().getRealPath( "WEB-INF/conf/owswatch" ); 107 if ( !webinfPath.endsWith( "/" ) ) { 108 webinfPath = webinfPath.concat( "/" ); 109 } 110 try { 111 factory = ServiceWatcherFactory.getInstance( confFilePath, webinfPath ); 112 watcher = factory.getServiceWatcherInstance(); 113 conf = factory.getConf(); 114 } catch ( Exception e ) { 115 LOG.logError( e.getLocalizedMessage() ); 116 return; 117 } 118 } 119 120 /* 121 * (non-Javadoc) 122 * 123 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, 124 * javax.servlet.http.HttpServletResponse) 125 */ 126 @Override 127 protected void doGet( HttpServletRequest request, HttpServletResponse response ) 128 throws ServletException, IOException { 129 PerformAction( request, response ); 130 } 131 132 /* 133 * (non-Javadoc) 134 * 135 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, 136 * javax.servlet.http.HttpServletResponse) 137 */ 138 @Override 139 protected void doPost( HttpServletRequest request, HttpServletResponse response ) 140 throws ServletException, IOException { 141 PerformAction( request, response ); 142 } 143 144 protected void PerformAction( HttpServletRequest request, HttpServletResponse response ) { 145 146 String action = request.getParameter( "action" ); 147 if ( action == null ) { 148 gotoErrorPage( request, response, "The action value is null", null, null ); 149 return; 150 } 151 if ( action.equals( "loginProtocol" ) ) { 152 handleLoginProtocol( request, response ); 153 } else if ( action.equals( "serviceProtocol" ) ) { 154 handleServiceProtocol( request, response ); 155 } else { 156 gotoErrorPage( request, response, StringTools.concat( 100, "action: ", action, 157 " is unknown to this servlet" ), null, null ); 158 } 159 } 160 161 /** 162 * Handle login for Protocol requests 163 * 164 * @param request 165 * @param response 166 */ 167 private boolean handleLoginProtocol( HttpServletRequest request, HttpServletResponse response ) { 168 169 String user = request.getParameter( "username" ); 170 String pwd = request.getParameter( "password" ); 171 try { 172 if ( factory.getConf().isAuthenticatedUser( user, pwd ) ) { 173 HttpSession session = request.getSession( true ); 174 // isLoggedin 175 String sessionId = UUID.randomUUID().toString(); 176 session.setAttribute( SESSIONID_KEY, sessionId ); 177 String serviceId = (String) session.getAttribute( "serviceId" ); 178 String nextpage = StringTools.concat( 200, "wprotocol?action=serviceProtocol&serviceId=", serviceId, 179 "&", SESSIONID_KEY, "=", sessionId ); 180 response.sendRedirect( nextpage ); 181 } else { 182 gotoErrorPage( request, response, Messages.getMessage( "INCORRECT_LOGIN" ), 183 Messages.getMessage( "MESSAGE_GOTO_MAIN" ), 184 JSPagesReference.getString( "OWSWatch.login" ) ); 185 } 186 } catch ( Exception e ) { 187 String errorMsg = StringTools.concat( 100, Messages.getMessage( "ERROR_LOGIN" ), "</br>", 188 e.getLocalizedMessage() ); 189 gotoErrorPage( request, response, errorMsg, Messages.getMessage( "MESSAGE_GOTO_MAIN" ), 190 JSPagesReference.getString( "OWSWatch.login" ) ); 191 return false; 192 } 193 return true; 194 } 195 196 /** 197 * forwards the Response to the error page 198 * 199 * @param request 200 * @param response 201 * @param error 202 * @param urlText 203 * @param url 204 */ 205 private void gotoErrorPage( HttpServletRequest request, HttpServletResponse response, String error, String urlText, 206 String url ) { 207 208 LOG.logError( error ); 209 HttpSession session = request.getSession( true ); 210 211 session.setAttribute( "message", StringTools.replace( error, "\n", "<br/>", true ) ); 212 if ( error == null ) { 213 error = "An unknown error has occured"; 214 } 215 if ( urlText == null ) { 216 urlText = ""; 217 } 218 session.setAttribute( "URLText", urlText ); 219 if ( url == null ) { 220 url = ""; 221 } 222 session.setAttribute( "URLAdd", url ); 223 try { 224 String nextpage = JSPagesReference.getString( "OWSWatch.error" ); 225 response.sendRedirect( nextpage ); 226 } catch ( Exception e ) { 227 LOG.logError( "The page could not be redirected to the error page" ); 228 } 229 } 230 231 /** 232 * sends the protocol of a serviceMonitor identified by its protIndex (got from request.getParameter()) object has 233 * as html file to the browser 234 * 235 */ 236 private boolean handleServiceProtocol( HttpServletRequest request, HttpServletResponse response ) { 237 238 if ( watcher == null || !isLoggedIn( request ) ) { 239 String serviceId = request.getParameter( "serviceId" ); 240 if ( serviceId == null ) { 241 return handleLogout( request, response ); 242 } 243 String sessionId = (String) request.getSession().getAttribute( SESSIONID_KEY ); 244 request.getSession().setAttribute( "serviceId", serviceId ); 245 if ( sessionId == null ) { 246 String next = JSPagesReference.getString( "OWSWatch.protocolLogin" ); 247 // If the user is not logged in, this is to check that the user didn't just logged 248 // in for another protocol, so that the user does not have to login everytime he 249 // clicks a protocol link 250 try { 251 response.sendRedirect( next ); 252 return true; 253 } catch ( IOException e ) { 254 gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", next ), null, null ); 255 } 256 } 257 } 258 int serviceId = Integer.parseInt( request.getParameter( "serviceId" ) ); 259 260 ServiceConfiguration serviceConfiguration = watcher.getService( serviceId ); 261 if ( serviceConfiguration == null ) { 262 gotoErrorPage( request, response, Messages.getMessage( "ERROR_NULL_OBJ", "ServiceConfiguration" ), 263 Messages.getMessage( "MESSAGE_GOTO_MAIN" ), 264 JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) ); 265 return false; 266 } 267 268 String xmlURI = watcher.getServiceLogs().get( serviceConfiguration ).getProtocolURI(); 269 File xmlFile = new File( xmlURI ); 270 String xslURI = getProtocolURL().concat( JSPagesReference.getString( "OWSWatch.protocolXSLScript" ) ); 271 File xslFile = new File( xslURI ); 272 XSLTDocument sheet = new XSLTDocument(); 273 274 XMLFragment input = new XMLFragment(); 275 276 XMLFragment result = null; 277 278 try { 279 input.load( xmlFile.toURL() ); 280 sheet.load( xslFile.toURL() ); 281 result = sheet.transform( input ); 282 } catch ( Exception e ) { 283 gotoErrorPage( request, response, Messages.getMessage( "ERROR_LOADING_XML_FILE", "handleServiceProtocol()", 284 xmlFile.getAbsolutePath() ), 285 Messages.getMessage( "MESSAGE_GOTO_MAIN" ), 286 JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) ); 287 return false; 288 } 289 String s = result.getAsString(); 290 291 request.setAttribute( "TABLE", s ); 292 String idx = String.valueOf( serviceId ); 293 request.setAttribute( "newWinProtocol", idx ); 294 String next = null; 295 try { 296 next = JSPagesReference.getString( "OWSWatch.protocolJSP" ); 297 getServletConfig().getServletContext().getRequestDispatcher( next ).forward( request, response ); 298 } catch ( Exception e ) { 299 gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", next ), 300 Messages.getMessage( "MESSAGE_GOTO_MAIN" ), 301 JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) ); 302 return false; 303 } 304 return true; 305 } 306 307 /** 308 * Logs the user out 309 * 310 * @return true if logout successfully, false otherwise 311 */ 312 private boolean handleLogout( HttpServletRequest request, HttpServletResponse response ) { 313 314 HttpSession session = request.getSession( true ); 315 session.setAttribute( "isLoggedin", false ); 316 session.removeAttribute( SESSIONID_KEY ); 317 318 String nextPage = JSPagesReference.getString( "OWSWatch.login" ); 319 try { 320 response.sendRedirect( nextPage ); 321 } catch ( Exception e ) { 322 gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextPage ), null, null ); 323 return false; 324 } 325 326 return true; 327 } 328 329 /** 330 * Verifies that this user is loggedin through comparing the session ID from the request paarameter with that saved 331 * in the session 332 * 333 * @param request 334 * @return true if the user is loggedin, false otherwise 335 */ 336 protected boolean isLoggedIn( HttpServletRequest request ) { 337 HttpSession session = request.getSession( true ); 338 String requestSession = request.getParameter( SESSIONID_KEY ); 339 String sessionId = (String) session.getAttribute( SESSIONID_KEY ); 340 if ( requestSession == null || sessionId == null || !requestSession.equals( sessionId ) ) { 341 return false; 342 } 343 return true; 344 } 345 346 /** 347 * @return the Location of the protocol of this Service 348 */ 349 public String getProtocolURL() { 350 String protDirePath = factory.getProtDirPath(); 351 return protDirePath.endsWith( "/" ) ? protDirePath : protDirePath.concat( "/" ); 352 } 353 }