1 17 18 19 20 package org.apache.lenya.cms.mail; 21 22 23 import java.io.File ; 24 import java.net.URL ; 25 import java.util.Arrays ; 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 ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.Text ; 37 38 39 66 public class MailTask extends AbstractTask { 67 private static Category log = Category.getInstance(MailTask.class); 68 69 public static final String ELEMENT_TO = "to"; 70 public static final String ELEMENT_CC = "cc"; 71 public static final String ELEMENT_BCC = "bcc"; 72 public static final String ELEMENT_SUBJECT = "subject"; 73 public static final String ELEMENT_BODY = "body"; 74 public static final String ELEMENT_FROM = "from"; 75 public static final String ELEMENT_SERVER = "server"; 76 public static final String PARAMETER_URI = "uri"; 77 public static final String NAMESPACE_URI = "http://apache.org/cocoon/lenya/mail/1.0"; 78 79 84 public void execute(String contextPath) { 85 log.debug("\n---------------------------" + "\n- Sending mail" + 86 "\n---------------------------"); 87 try { 88 Parameters taskParameters = new Parameters(); 89 90 String uri = getParameters().getParameter(PARAMETER_URI, ""); 91 log.debug("\nURI: " + uri); 92 93 if (!uri.equals("")) { 94 if (!uri.startsWith("http://") && !uri.startsWith("ftp://") && 96 !uri.startsWith("file://")) { 97 String absoluteUri = "http://127.0.0.1"; 98 String 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 document = DocumentHelper.readDocument(new URL (uri)); 109 Element root = document.getDocumentElement(); 110 111 NamespaceHelper helper = new NamespaceHelper(NAMESPACE_URI, "mail", document); 112 113 String [] keys = { 114 ELEMENT_SERVER, ELEMENT_FROM, ELEMENT_TO, ELEMENT_CC, ELEMENT_BCC, 115 ELEMENT_SUBJECT, ELEMENT_BODY 116 }; 117 118 Element [] elements = helper.getChildren(root); 119 120 for (int i = 0; i < elements.length; i++) { 121 if (elements[i].getChildNodes().getLength() > 0) { 122 Node firstChild = elements[i].getChildNodes().item(0); 123 124 if (firstChild instanceof Text ) { 125 Text text = (Text ) firstChild; 126 String 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 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 e) { 146 log.error("Sending mail failed: ", e); 147 throw new RuntimeException (e); 148 } 149 } 150 151 162 public void sendMail(String host, String from, String to, String cc, String bcc, 163 String subject, String body) { 164 SMTP smtp = new SMTP(); 165 smtp.send(from, to, cc, bcc, subject, body); 166 } 167 168 179 } 180 | Popular Tags |