1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26
27 import javax.mail.MessagingException;
28 import javax.mail.internet.MimeMessage;
29
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.james.Constants;
33 import org.apache.james.api.user.UsersRepository;
34 import org.apache.james.api.user.UsersStore;
35 import org.apache.james.api.vut.ErrorMappingException;
36 import org.apache.james.api.vut.VirtualUserTable;
37 import org.apache.mailet.MailAddress;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class UsersRepositoryAliasingForwarding extends AbstractVirtualUserTableMailet {
55
56
57
58
59
60 private UsersRepository usersRepository;
61
62
63
64
65
66
67 public String getMailetInfo() {
68 return "Local User Aliasing and Forwarding Mailet";
69 }
70
71
72
73
74
75
76
77
78
79
80 public Collection processMail(MailAddress sender, MailAddress recipient,
81 MimeMessage message) throws MessagingException {
82 if (recipient == null) {
83 throw new IllegalArgumentException(
84 "Recipient for mail to be spooled cannot be null.");
85 }
86 if (message == null) {
87 throw new IllegalArgumentException(
88 "Mail message to be spooled cannot be null.");
89 }
90
91 if (usersRepository instanceof VirtualUserTable) {
92 Collection mappings;
93 try {
94 mappings = ((VirtualUserTable) usersRepository).getMappings(recipient.getUser(), recipient.getHost());
95 } catch (ErrorMappingException e) {
96 StringBuffer errorBuffer = new StringBuffer(128)
97 .append("A problem as occoured trying to alias and forward user ")
98 .append(recipient)
99 .append(": ")
100 .append(e.getMessage());
101 throw new MessagingException(errorBuffer.toString());
102 }
103
104 if (mappings != null) {
105 return handleMappings(mappings, sender, recipient, message);
106 }
107 } else {
108 StringBuffer errorBuffer = new StringBuffer(128)
109 .append("Warning: the repository ")
110 .append(usersRepository.getClass().getName())
111 .append(" does not implement VirtualUserTable interface).");
112 getMailetContext().log(errorBuffer.toString());
113 }
114 String realName = usersRepository.getRealName(recipient.getUser());
115 if (realName != null) {
116 ArrayList ret = new ArrayList();
117 ret.add(new MailAddress(realName, recipient.getHost()));
118 return ret;
119 } else {
120 ArrayList ret = new ArrayList();
121 ret.add(recipient);
122 return ret;
123 }
124 }
125
126
127
128
129 public void init() throws MessagingException {
130 super.init();
131 ServiceManager compMgr = (ServiceManager) getMailetContext()
132 .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
133
134 try {
135 String userRep = getInitParameter("usersRepository");
136 if (userRep == null || userRep.length() == 0) {
137 try {
138 usersRepository = (UsersRepository) compMgr
139 .lookup(UsersRepository.ROLE);
140 } catch (ServiceException e) {
141 log("Failed to retrieve UsersRepository component:"
142 + e.getMessage());
143 }
144 } else {
145 UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE);
146 usersRepository = usersStore.getRepository(userRep);
147 }
148
149 } catch (ServiceException cnfe) {
150 log("Failed to retrieve UsersStore component:" + cnfe.getMessage());
151 }
152
153 }
154
155 }