001: package org.kohsuke.javanet.devrolehandler;
002:
003: import dalma.ReplyIterator;
004: import dalma.Workflow;
005: import dalma.endpoints.email.EmailEndPoint;
006: import dalma.endpoints.email.MimeMessageEx;
007:
008: import javax.mail.MessagingException;
009: import java.io.BufferedReader;
010: import java.io.ByteArrayInputStream;
011: import java.io.ByteArrayOutputStream;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.InputStreamReader;
015: import java.io.OutputStreamWriter;
016: import java.io.PrintWriter;
017: import java.util.Calendar;
018: import java.util.GregorianCalendar;
019: import java.util.logging.Level;
020:
021: import org.kohsuke.jnt.JavaNet;
022: import org.kohsuke.jnt.ProcessingException;
023:
024: /**
025: * A sample workflow.
026: */
027: public class ConversationImpl extends Workflow {
028: private final String projectName;
029: private final String role;
030: private final String userName;
031:
032: private final EmailEndPoint endpoint;
033:
034: public ConversationImpl(EmailEndPoint endpoint, String projectName,
035: String role, String userName) {
036: this .projectName = projectName;
037: this .role = role;
038: this .userName = userName;
039: this .endpoint = endpoint;
040: }
041:
042: @Override
043: public void run() {
044: try {
045: InputStream template = getClass().getResourceAsStream(
046: "mail-templates.txt");
047: MimeMessageEx msg = new MimeMessageEx(
048: endpoint.getSession(), replace(template));
049:
050: // expire in one week
051: Calendar cal = new GregorianCalendar();
052: cal.add(Calendar.DATE, 7);
053:
054: // send a clarification message and wait for the owner to eventually respond
055:
056: ReplyIterator<MimeMessageEx> itr = endpoint
057: .waitForMultipleReplies(msg, cal.getTime());
058: while (itr.hasNext()) {
059: MimeMessageEx reply = itr.next();
060: try {
061: String body = reply.getMainContent();
062: if (body.startsWith("##APPROVE")) {
063: getLogger().info(
064: "Approving a request based on e-mail from "
065: + reply.getFrom());
066: approve();
067: return;
068: }
069: if (body.startsWith("##DENY")) {
070: getLogger().info(
071: "Denying a request based on e-mail from "
072: + reply.getFrom());
073: deny(body);
074: return;
075: }
076: } catch (MessagingException e) {
077: getLogger().log(Level.WARNING,
078: "Failed to parse a reply", e);
079: } catch (IOException e) {
080: getLogger().log(Level.WARNING,
081: "Failed to parse a reply", e);
082: }
083: }
084: } catch (Exception e) {
085: getLogger().log(Level.WARNING, e.getMessage(), e);
086: }
087: }
088:
089: /**
090: * Denies a request
091: */
092: private void deny(String msg) throws ProcessingException {
093: JavaNet jn = JavaNet.connect();
094:
095: jn.getProject(projectName).getMembership().declineRole(
096: jn.getUser(userName), role, msg);
097: }
098:
099: /**
100: * Approves a request.
101: */
102: private void approve() throws ProcessingException {
103: JavaNet jn = JavaNet.connect();
104:
105: jn.getProject(projectName).getMembership().grantRole(
106: jn.getUser(userName), role);
107: }
108:
109: /**
110: * Replace keywords in the input stream.
111: *
112: * <p>
113: * The current implementation doesn't handle encoding correctly.
114: */
115: private InputStream replace(InputStream in) throws IOException {
116: BufferedReader r = new BufferedReader(new InputStreamReader(in));
117: ByteArrayOutputStream baos = new ByteArrayOutputStream();
118: PrintWriter w = new PrintWriter(new OutputStreamWriter(baos));
119:
120: String line;
121:
122: while ((line = r.readLine()) != null) {
123: line = line.replace("${project}", projectName);
124: line = line.replace("${role}", role);
125: line = line.replace("${user}", userName);
126: w.println(line);
127: }
128: r.close();
129: w.close();
130: return new ByteArrayInputStream(baos.toByteArray());
131: }
132:
133: }
|