01: package org.kohsuke.javanet.approval;
02:
03: import dalma.endpoints.email.MimeMessageEx;
04:
05: import javax.mail.internet.MimeMessage;
06: import javax.mail.MessagingException;
07: import java.io.BufferedReader;
08: import java.io.StringReader;
09: import java.io.IOException;
10: import java.util.regex.Pattern;
11: import java.util.regex.Matcher;
12:
13: /**
14: * Represents an e-mail from java.net that notifies
15: * a creation of the new project.
16: *
17: * @author Kohsuke Kawaguchi
18: */
19: public class ProjectCreationEmail extends MimeMessageEx {
20: private final String user;
21: private final String project;
22: private String description;
23:
24: public ProjectCreationEmail(MimeMessage source)
25: throws MessagingException, IOException {
26: super (source);
27:
28: if (!getSubject().startsWith("New unapproved project: "))
29: throw new MessagingException(
30: "Not a project creation e-mail. The subject is "
31: + getSubject());
32:
33: BufferedReader r = new BufferedReader(new StringReader(
34: (String) getContent()));
35: String line = r.readLine();
36:
37: Matcher matcher = PARSER.matcher(line);
38: if (matcher.matches()) {
39: user = matcher.group(1);
40: project = matcher.group(2);
41: } else {
42: throw new MessagingException(
43: "The e-mail content didn't match the pattern: "
44: + line);
45: }
46:
47: while ((line = r.readLine()) != null) {
48: if (line.equals("Project description:")) {
49: StringBuffer buf = new StringBuffer();
50: while ((line = r.readLine()) != null) {
51: if (line
52: .equals("Please approve or disapprove this project at your convenience by going to:"))
53: break;
54: buf.append(line);
55: buf.append("\n");
56: }
57: description = buf.toString();
58: break;
59: }
60: }
61: }
62:
63: public String getUser() {
64: return user;
65: }
66:
67: public String getProject() {
68: return project;
69: }
70:
71: public String getDescription() {
72: return description;
73: }
74:
75: private static final Pattern PARSER = Pattern
76: .compile("([^ ]+) has started the ([^ ]+) project.");
77: }
|