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