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