001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wmps/DefaultRequestManager.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.ogcwebservices.wmps; 037 038 import java.sql.Connection; 039 040 import javax.mail.internet.AddressException; 041 import javax.mail.internet.InternetAddress; 042 043 import org.deegree.framework.log.ILogger; 044 import org.deegree.framework.log.LoggerFactory; 045 import org.deegree.framework.mail.EMailMessage; 046 import org.deegree.framework.mail.MailHelper; 047 import org.deegree.framework.mail.MailMessage; 048 import org.deegree.framework.mail.SendMailException; 049 import org.deegree.framework.xml.NamespaceContext; 050 import org.deegree.framework.xml.XMLFragment; 051 import org.deegree.framework.xml.XMLParsingException; 052 import org.deegree.framework.xml.XMLTools; 053 import org.deegree.i18n.Messages; 054 import org.deegree.ogcbase.CommonNamespaces; 055 import org.deegree.ogcwebservices.OGCWebServiceException; 056 import org.deegree.ogcwebservices.wmps.configuration.CacheDatabase; 057 import org.deegree.ogcwebservices.wmps.configuration.WMPSConfiguration; 058 import org.deegree.ogcwebservices.wmps.operation.PrintMap; 059 import org.deegree.ogcwebservices.wmps.operation.PrintMapResponse; 060 import org.deegree.ogcwebservices.wmps.operation.PrintMapResponseDocument; 061 import org.w3c.dom.Element; 062 063 /** 064 * Default Handler to save the PrintMap requests to the 'HSQLDB' and send email after processing the request. 065 * 066 * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a> 067 * @author last edited by: $Author:wanhoff$ 068 * 069 * @version $Revision: 22068 $, $Date:20.03.2007$ 070 */ 071 public class DefaultRequestManager implements RequestManager { 072 073 private static final ILogger LOG = LoggerFactory.getLogger( DefaultRequestManager.class ); 074 075 protected static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext(); 076 077 private WMPSConfiguration configuration; 078 079 //private final String MIMETYPE = "text/html"; 080 081 private PrintMap request; 082 083 /** 084 * Creates a new DefaultRequestManager instance. 085 * 086 * @param configuration 087 * @param request 088 * request to perform 089 */ 090 public DefaultRequestManager( WMPSConfiguration configuration, PrintMap request ) { 091 this.configuration = configuration; 092 this.request = request; 093 } 094 095 /** 096 * returns the configuration used by the handler 097 * 098 * @return WMPSConfiguration 099 */ 100 public WMPSConfiguration getConfiguration() { 101 return this.configuration; 102 103 } 104 105 /** 106 * returns the request used by the handler 107 * 108 * @return PrintMap request 109 */ 110 public PrintMap getRequest() { 111 return this.request; 112 113 } 114 115 /** 116 * Opens a connection to a database based on the properties file in the resources directory and saves the current 117 * PrintMap request in the table for later access. 118 * 119 * @throws OGCWebServiceException 120 */ 121 public synchronized void saveRequestToDB() 122 throws OGCWebServiceException { 123 124 try { 125 CacheDatabase cacheDatabase = this.configuration.getDeegreeParams().getCacheDatabase(); 126 WMPSDatabase dbConnection = new WMPSDatabase( cacheDatabase ); 127 Connection connection = dbConnection.acquireConnection(); 128 dbConnection.insertData( connection, this.request ); 129 dbConnection.releaseConnection( connection ); 130 } catch ( Exception e ) { 131 LOG.logError( e.getMessage(), e ); 132 throw new OGCWebServiceException( Messages.getMessage( "WMPS_ERROR_CREATE_WMPSDB" ) ); 133 } 134 135 } 136 137 /** 138 * Send an intial response back to the user, depending on whether the request has been successfull saved in the DB 139 * or not. The email address from the request is used to send the reply. 140 * 141 * @param message 142 * @return PrintMapResponseDocument 143 * @throws OGCWebServiceException 144 */ 145 public PrintMapResponseDocument createInitialResponse( String message ) 146 throws OGCWebServiceException { 147 148 // before the print operation is finished stage. 149 PrintMapResponse initialResponse = new PrintMapResponse( this.request.getId(), this.request.getEmailAddress(), 150 this.request.getTimestamp(), 151 this.request.getTimestamp(), message, "" ); 152 153 PrintMapResponseDocument document; 154 try { 155 document = XMLFactory.export( initialResponse ); 156 157 } catch ( XMLParsingException e ) { 158 LOG.logError( e.getMessage(), e ); 159 throw new OGCWebServiceException( Messages.getMessage( "WMPS_ERROR_CREATE_RESPONSE2" ) ); 160 } 161 162 return document; 163 } 164 165 /** 166 * Send an Email to the address provided in the PrintMap request. 167 * 168 * @param response 169 * @throws OGCWebServiceException 170 */ 171 public void sendEmail( PrintMapResponseDocument response ) 172 throws OGCWebServiceException { 173 174 XMLFragment doc = new XMLFragment( response.getRootElement() ); 175 Element root = doc.getRootElement(); 176 String id = root.getAttribute( "id" ); 177 String toEmailAddress = null; 178 String timestamp = null; 179 String message = null; 180 // String processingTime = null; 181 try { 182 String xPath = "deegreewmps:EmailAddress"; 183 toEmailAddress = XMLTools.getRequiredNodeAsString( root, xPath, nsContext ); 184 if ( !isValidEmailAddress( toEmailAddress ) ) { 185 throw new PrintMapServiceException( Messages.getMessage( "WMPS_INCORRECT_MAIL_ADDRESS", toEmailAddress ) ); 186 } 187 timestamp = XMLTools.getRequiredNodeAsString( root, "deegreewmps:Timestamp", nsContext ); 188 message = XMLTools.getRequiredNodeAsString( root, "deegreewmps:Message", nsContext ); 189 xPath = "deegreewmps:ExpectedProcessingTime"; 190 // TODO 191 // processingTime = XMLTools.getNodeAsString( root, xPath, nsContext, null ); 192 } catch ( XMLParsingException e ) { 193 LOG.logError( e.getMessage(), e ); 194 throw new OGCWebServiceException( Messages.getMessage( "WMPS_PARSING_RESPONSE" ) ); 195 } 196 String fromEmailAddress = this.configuration.getDeegreeParams().getPrintMapParam().getAdminMailAddress(); 197 198 if ( !isValidEmailAddress( fromEmailAddress ) ) { 199 throw new PrintMapServiceException( Messages.getMessage( "WMPS_INCORRECT_MAIL_ADDRESS", fromEmailAddress ) ); 200 } 201 String subject = "PrintMap Notification: " + id + ' ' + timestamp; 202 203 MailMessage email = new EMailMessage( fromEmailAddress, toEmailAddress, subject, message ); 204 String mailHost = this.configuration.getDeegreeParams().getPrintMapParam().getMailHost(); 205 String mailHostUser = this.configuration.getDeegreeParams().getPrintMapParam().getMailHostUser(); 206 String mailHostPassword = this.configuration.getDeegreeParams().getPrintMapParam().getMailHostPassword(); 207 try { 208 if ( mailHostUser != null ) { 209 MailHelper.createAndSendMail( email, mailHost, mailHostUser, mailHostPassword ); 210 } else { 211 MailHelper.createAndSendMail( email, mailHost ); 212 } 213 } catch ( SendMailException e ) { 214 LOG.logError( e.getMessage(), e ); 215 String msg = Messages.getMessage( "WMPS_ERROR_SEND_EMAIL", toEmailAddress, mailHost ); 216 throw new OGCWebServiceException( msg ); 217 } 218 219 } 220 221 /** 222 * Check if the email address is valid and has a valid name and domain string. 223 * 224 * @param aEmailAddress 225 * @return boolean 226 */ 227 private boolean hasNameAndDomain( String aEmailAddress ) { 228 229 String[] tokens = aEmailAddress.split( "@" ); 230 return tokens.length == 2 && ( ( tokens[0] != null ) || ( tokens[0] != "" ) ) 231 && ( ( tokens[1] != null ) || ( tokens[1] != "" ) ); 232 } 233 234 /** 235 * Check email add validity. 236 * 237 * @param aEmailAddress 238 * @return boolean 239 */ 240 private boolean isValidEmailAddress( String aEmailAddress ) { 241 242 String status = "VALID"; 243 if ( aEmailAddress == null ) 244 status = "NOTVALID"; 245 246 try { 247 new InternetAddress( aEmailAddress ); 248 if ( !hasNameAndDomain( aEmailAddress ) ) { 249 status = "NOTVALID"; 250 } 251 } catch ( AddressException ex ) { 252 status = "NOTVALID " + ex.getMessage(); 253 } 254 255 return status.startsWith( "VALID" ); 256 } 257 258 /** 259 * Export the PrintMap service final response to a PrintMapResponseDocument. 260 * 261 * @param message 262 * @param exception 263 * @return PrintMapResponseDocument 264 * @throws OGCWebServiceException 265 */ 266 public PrintMapResponseDocument createFinalResponse( String message, String exception ) 267 throws OGCWebServiceException { 268 269 PrintMapResponse finalResponse = new PrintMapResponse( this.request.getId(), this.request.getEmailAddress(), 270 this.request.getTimestamp(), 271 this.request.getTimestamp(), message, exception ); 272 273 PrintMapResponseDocument document; 274 try { 275 document = XMLFactory.export( finalResponse ); 276 } catch ( XMLParsingException e ) { 277 LOG.logError( e.getMessage(), e ); 278 throw new OGCWebServiceException( Messages.getMessage( "WMPS_ERROR_CREATE_RESPONSE1" ) ); 279 } 280 281 return document; 282 } 283 }