1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.transport.mailets;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24
25 import javax.annotation.Resource;
26 import javax.mail.MessagingException;
27 import javax.mail.internet.MimeMessage;
28
29 import org.apache.james.api.vut.ErrorMappingException;
30 import org.apache.james.api.vut.VirtualUserTableStore;
31 import org.apache.mailet.MailAddress;
32 import org.apache.mailet.MailetException;
33
34
35
36
37
38
39
40
41
42
43
44 public class VirtualUserTable extends AbstractVirtualUserTableMailet {
45 private org.apache.james.api.vut.VirtualUserTable vut;
46
47 private VirtualUserTableStore vutStore;
48
49
50
51
52
53
54 public final org.apache.james.api.vut.VirtualUserTable getVut() {
55 return vut;
56 }
57
58
59
60
61
62 @Resource(name="defaultvirtualusertable")
63 public final void setVut(org.apache.james.api.vut.VirtualUserTable vut) {
64 this.vut = vut;
65 }
66
67
68
69
70
71 public final VirtualUserTableStore getVutStore() {
72 return vutStore;
73 }
74
75
76
77
78
79 @Resource(name="virtualusertable-store")
80 public final void setVutStore(VirtualUserTableStore vutStore) {
81 this.vutStore = vutStore;
82 }
83
84
85
86
87 public void init() throws MessagingException {
88 super.init();
89
90 if (vut == null && vutStore == null) {
91 throw new MailetException("Not initialised. Please ensure that the mailet container supports either" +
92 " setter or constructor injection. ");
93 }
94
95 String vutName = getInitParameter("virtualusertable");
96 if (vutName == null || vutName.length() == 0) {
97 if (vut == null) {
98 throw new MailetException("When 'virtualusertable' is unset, a virtual user table must be " +
99 "provided by the container.");
100 }
101 } else if (vutStore == null) {
102 throw new MailetException("When 'virtualusertable' is set, a virtual user table store must be " +
103 "provided by the container.");
104 } else {
105 vut = vutStore.getTable(vutName);
106 }
107 }
108
109
110
111
112 public Collection processMail(MailAddress sender, MailAddress recipient, MimeMessage message) throws MessagingException {
113 try {
114 Collection mappings = vut.getMappings(recipient.getUser(), recipient.getHost());
115
116 if (mappings != null) {
117 return handleMappings(mappings, sender, recipient, message);
118 }
119 } catch (ErrorMappingException e) {
120 StringBuffer errorBuffer = new StringBuffer(128)
121 .append("A problem as occoured trying to alias and forward user ")
122 .append(recipient)
123 .append(": ")
124 .append(e.getMessage());
125 throw new MessagingException(errorBuffer.toString());
126 }
127
128 Collection rcpts = new ArrayList();
129 rcpts.add(recipient);
130 return rcpts;
131 }
132
133
134
135
136 public String getMailetInfo() {
137 return "VirtualUserTable Mailet";
138 }
139
140
141 }