1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jsieve.mailet;
20
21 import javax.mail.MessagingException;
22 import javax.mail.internet.MimeMessage;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.jsieve.mail.Action;
26 import org.apache.jsieve.mail.ActionFileInto;
27 import org.apache.mailet.Mail;
28 import org.apache.mailet.MailAddress;
29
30 public class FileIntoAction implements MailAction {
31
32 private static final char HIERARCHY_DELIMITER = '.';
33
34 public void execute(Action action, Mail mail, ActionContext context) throws MessagingException {
35 if (action instanceof ActionFileInto) {
36 final ActionFileInto fileIntoAction = (ActionFileInto) action;
37 execute(fileIntoAction, mail, context);
38 }
39 }
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public void execute(ActionFileInto anAction, Mail aMail, final ActionContext context) throws MessagingException
67 {
68 String destinationMailbox = anAction.getDestination();
69 MailAddress recipient;
70 boolean delivered = false;
71 try
72 {
73 recipient = ActionUtils.getSoleRecipient(aMail);
74 MimeMessage localMessage = createMimeMessage(aMail, recipient);
75
76 if (!(destinationMailbox.length() > 0
77 && destinationMailbox.charAt(0) == HIERARCHY_DELIMITER)) {
78 destinationMailbox = HIERARCHY_DELIMITER + destinationMailbox;
79 }
80
81 final String mailbox = destinationMailbox.replace(HIERARCHY_DELIMITER, '/');
82 final String host;
83 if (mailbox.charAt(0) == '/') {
84 host = "@localhost";
85 } else {
86 host = "@localhost/";
87 }
88 final String url = "mailbox://" + recipient.getUser() + host + mailbox;
89
90 context.post(url, localMessage);
91 delivered = true;
92 }
93 catch (MessagingException ex)
94 {
95 final Log log = context.getLog();
96 if (log.isDebugEnabled()) {
97 log.debug("Error while storing mail into. "+destinationMailbox, ex);
98 }
99 throw ex;
100 }
101 finally
102 {
103
104 aMail.setState(Mail.GHOST);
105 }
106 if (delivered)
107 {
108 final Log log = context.getLog();
109 if (log.isDebugEnabled()) {
110 log.debug("Filed Message ID: "
111 + aMail.getMessage().getMessageID()
112 + " into destination: \""
113 + destinationMailbox + "\"");
114 }
115 }
116 }
117
118 private static MimeMessage createMimeMessage(Mail aMail, MailAddress recipient) throws MessagingException {
119
120
121 MimeMessage localMessage = new MimeMessage(aMail.getMessage())
122 {
123 protected void updateHeaders() throws MessagingException
124 {
125 if (getMessageID() == null)
126 super.updateHeaders();
127 else
128 modified = false;
129 }
130 };
131 localMessage.addHeader("Delivered-To", recipient.toString());
132
133 localMessage.saveChanges();
134 return localMessage;
135 }
136 }