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