001: package org.kohsuke.javanet.approval;
002:
003: import com.meterware.httpunit.WebResponse;
004: import dalma.endpoints.email.MimeMessageEx;
005: import org.dom4j.Element;
006: import org.dom4j.Node;
007: import org.dom4j.io.SAXReader;
008: import org.kohsuke.jnt.JNProject;
009: import org.kohsuke.jnt.ProcessingException;
010: import org.kohsuke.jnt.JavaNet;
011:
012: import javax.mail.Message.RecipientType;
013: import javax.mail.MessagingException;
014: import javax.mail.Session;
015: import javax.mail.Address;
016: import javax.mail.internet.InternetAddress;
017: import javax.mail.internet.MimeMessage;
018: import java.io.IOException;
019: import java.io.StringReader;
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * Represents the community specific setting
027: * at <tt>https://communityleads.dev.java.net/process/project-approval-automation<i>community</i>.xml</tt>.
028: *
029: * @author Kohsuke Kawaguchi
030: */
031: public final class CommunityConfig {
032: private final Element root;
033: private final JNProject community;
034:
035: public CommunityConfig(JNProject community) throws IOException {
036: this .community = community;
037: URL address = new URL(
038: "https://communityleads.dev.java.net/process/project-approval-automation/"
039: + community.getName() + ".xml");
040: try {
041: WebResponse r = community.getConnection().getConversation()
042: .getResponse(address.toExternalForm());
043: root = new SAXReader().read(new StringReader(r.getText()))
044: .getRootElement();
045: } catch (Exception e) {
046: IOException ioe = new IOException("Failed to read "
047: + address);
048: ioe.initCause(e);
049: throw ioe;
050: }
051: }
052:
053: public JNProject getIncubator() throws ProcessingException {
054: Node n = root.selectSingleNode("incubator");
055: JavaNet cn = community.getConnection();
056: if (n == null)
057: // defaults to <community>-incubator
058: return cn.getProject(community.getName() + "-incubator");
059: else
060: return cn.getProject(n.getText().trim());
061: }
062:
063: /**
064: * Returns the e-mail address that should receive routing notifications.
065: */
066: public List<String> getNewProjectNotifications() {
067: List<Element> list = root
068: .selectNodes("new-project/notify/address");
069: List<String> r = new ArrayList<String>();
070: for (Element e : list) {
071: r.add(e.getTextTrim());
072: }
073: return r;
074: }
075:
076: public MimeMessage createWelcomeMessage(Session session, Map macros)
077: throws MessagingException, IOException {
078: Element welcome = (Element) root
079: .selectSingleNode("new-project/welcome");
080: if (welcome != null)
081: return createMessage(welcome, session, macros);
082: else
083: return null;
084: }
085:
086: private MimeMessage createMessage(Element element, Session session,
087: Map macros) throws MessagingException, IOException {
088: MimeMessageEx msg = new MimeMessageEx(session);
089:
090: msg.setSubject(Util.replace(element.elementTextTrim("subject"),
091: macros));
092:
093: for (Element e : (List<Element>) element.elements("to")) {
094: msg.addRecipient(RecipientType.TO, new InternetAddress(e
095: .getTextTrim()));
096: }
097: for (Element e : (List<Element>) element.elements("cc")) {
098: msg.addRecipient(RecipientType.CC, new InternetAddress(e
099: .getTextTrim()));
100: }
101: for (Element e : (List<Element>) element.elements("bcc")) {
102: msg.addRecipient(RecipientType.BCC, new InternetAddress(e
103: .getTextTrim()));
104: }
105:
106: Element e = element.element("from");
107: if (e != null) {
108: msg.setFrom(new InternetAddress(e.getTextTrim()));
109: }
110:
111: e = element.element("replyTo");
112: if (e != null) {
113: msg.setReplyTo(new Address[] { new InternetAddress(e
114: .getTextTrim()) });
115: }
116:
117: msg.setContent(Util
118: .replace(element.elementText("body"), macros),
119: "text/plain");
120:
121: return msg;
122: }
123: }
|