KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > mail > MailTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: MailTask.java 109972 2004-12-06 13:54:50Z edith $ */
19
20 package org.apache.lenya.cms.mail;
21
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.lenya.cms.task.AbstractTask;
29 import org.apache.lenya.net.SMTP;
30 import org.apache.lenya.xml.DocumentHelper;
31 import org.apache.lenya.xml.NamespaceHelper;
32 import org.apache.log4j.Category;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.Text JavaDoc;
37
38
39 /**
40  * A task that sends an e-mail. Each parameter can either be provided as a task parameter or
41  * extracted from an XML document. If the parameter "uri" starts with a <code>http://</code> or
42  * <code>ftp://</code> prefix, the absolute URI is used. If not, the URI is interpreted as
43  * relative to the local publication. <br/
44  * ><br/
45  * > The task parameters are:<br/
46  * ><code><strong>uri</strong></code>: the URI to get the XML file from<br/
47  * ><code><strong>server</strong></code>: the SMTP server<br/
48  * ><code><strong>from</strong></code>:<br/
49  * ><code><strong>to</strong></code>:<br/
50  * ><code><strong>cc</strong></code>:<br/
51  * ><code><strong>bcc</strong></code>:<br/
52  * ><code><strong>subject</strong></code>:<br/
53  * ><code><strong>body</strong></code>:<br/
54  * ><br/
55  * > All parameters are optional. If the uri parameter is provided, the document is fetched from
56  * the URI and the parameters are extracted. Task parameters have a higher priority than elements
57  * of the document. <br/
58  * ><br/
59  * > The document has the following form:<br/
60  * ><br/
61  * ><code> &lt;mail:mail xmlns:mail="http://apache.org/cocoon/lenya/mail/1.0"&gt;<br/
62  * > &#160;&#160;&lt;mail:server&gt;mail.yourhost.com&lt;/mail:server&gt;<br/
63  * > &#160;&#160;...<br/
64  * > &lt;/mail:mail&gt;<br/></code>
65  */

66 public class MailTask extends AbstractTask {
67     private static Category log = Category.getInstance(MailTask.class);
68     
69     public static final String JavaDoc ELEMENT_TO = "to";
70     public static final String JavaDoc ELEMENT_CC = "cc";
71     public static final String JavaDoc ELEMENT_BCC = "bcc";
72     public static final String JavaDoc ELEMENT_SUBJECT = "subject";
73     public static final String JavaDoc ELEMENT_BODY = "body";
74     public static final String JavaDoc ELEMENT_FROM = "from";
75     public static final String JavaDoc ELEMENT_SERVER = "server";
76     public static final String JavaDoc PARAMETER_URI = "uri";
77     public static final String JavaDoc NAMESPACE_URI = "http://apache.org/cocoon/lenya/mail/1.0";
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @param contextPath DOCUMENT ME!
83      */

84     public void execute(String JavaDoc contextPath) {
85         log.debug("\n---------------------------" + "\n- Sending mail" +
86             "\n---------------------------");
87         try {
88             Parameters taskParameters = new Parameters();
89
90             String JavaDoc uri = getParameters().getParameter(PARAMETER_URI, "");
91             log.debug("\nURI: " + uri);
92
93             if (!uri.equals("")) {
94                 // generate absolute URI from relative URI
95
if (!uri.startsWith("http://") && !uri.startsWith("ftp://") &&
96                         !uri.startsWith("file://")) {
97                     String JavaDoc absoluteUri = "http://127.0.0.1";
98                     String JavaDoc serverPort = getParameters().getParameter(PARAMETER_SERVER_PORT, "");
99
100                     if (!serverPort.equals("")) {
101                         absoluteUri += (":" + Integer.parseInt(serverPort));
102                     }
103
104                     absoluteUri += (getParameters().getParameter(PARAMETER_CONTEXT_PREFIX) + File.separator +
105                     getParameters().getParameter(PARAMETER_PUBLICATION_ID) + uri);
106                     uri = absoluteUri;
107                 }
108                 Document JavaDoc document = DocumentHelper.readDocument(new URL JavaDoc(uri));
109                 Element JavaDoc root = document.getDocumentElement();
110
111                 NamespaceHelper helper = new NamespaceHelper(NAMESPACE_URI, "mail", document);
112
113                 String JavaDoc[] keys = {
114                     ELEMENT_SERVER, ELEMENT_FROM, ELEMENT_TO, ELEMENT_CC, ELEMENT_BCC,
115                     ELEMENT_SUBJECT, ELEMENT_BODY
116                 };
117
118                 Element JavaDoc[] elements = helper.getChildren(root);
119
120                 for (int i = 0; i < elements.length; i++) {
121                     if (elements[i].getChildNodes().getLength() > 0) {
122                         Node JavaDoc firstChild = elements[i].getChildNodes().item(0);
123
124                         if (firstChild instanceof Text JavaDoc) {
125                             Text JavaDoc text = (Text JavaDoc) firstChild;
126                             String JavaDoc key = elements[i].getLocalName();
127
128                             if (Arrays.asList(keys).contains(key)) {
129                                 taskParameters.setParameter(key, text.getNodeValue());
130                             }
131                         }
132                     }
133                 }
134             }
135
136             // task parameters have a higher priority than XML elements
137
taskParameters = taskParameters.merge(getParameters());
138
139             sendMail(taskParameters.getParameter(ELEMENT_SERVER),
140                 taskParameters.getParameter(ELEMENT_FROM), taskParameters.getParameter(ELEMENT_TO),
141                 taskParameters.getParameter(ELEMENT_CC, ""),
142                 taskParameters.getParameter(ELEMENT_BCC, ""),
143                 taskParameters.getParameter(ELEMENT_SUBJECT, ""),
144                 taskParameters.getParameter(ELEMENT_BODY, ""));
145         } catch (Exception JavaDoc e) {
146             log.error("Sending mail failed: ", e);
147             throw new RuntimeException JavaDoc (e);
148         }
149     }
150
151     /**
152      * DOCUMENT ME!
153      *
154      * @param host DOCUMENT ME!
155      * @param from DOCUMENT ME!
156      * @param to DOCUMENT ME!
157      * @param cc DOCUMENT ME!
158      * @param bcc DOCUMENT ME!
159      * @param subject DOCUMENT ME!
160      * @param body DOCUMENT ME!
161      */

162     public void sendMail(String JavaDoc host, String JavaDoc from, String JavaDoc to, String JavaDoc cc, String JavaDoc bcc,
163         String JavaDoc subject, String JavaDoc body) {
164         SMTP smtp = new SMTP();
165         smtp.send(from, to, cc, bcc, subject, body);
166     }
167
168     /**
169      * DOCUMENT ME!
170      *
171      * @param host DOCUMENT ME!
172      * @param from DOCUMENT ME!
173      * @param to DOCUMENT ME!
174      * @param cc DOCUMENT ME!
175      * @param bcc DOCUMENT ME!
176      * @param subject DOCUMENT ME!
177      * @param body DOCUMENT ME!
178      */

179 }
180
Popular Tags