/
AbstractRecipientRewriteTable.java
523 lines (448 loc) · 20.3 KB
/
AbstractRecipientRewriteTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.rrt.lib;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.internet.ParseException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.domainlist.api.DomainList;
import org.apache.james.domainlist.api.DomainListException;
import org.apache.james.lifecycle.api.Configurable;
import org.apache.james.lifecycle.api.LogEnabled;
import org.apache.james.rrt.api.RecipientRewriteTable;
import org.apache.james.rrt.api.RecipientRewriteTableException;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import com.google.common.annotations.VisibleForTesting;
/**
*
*/
public abstract class AbstractRecipientRewriteTable implements RecipientRewriteTable, LogEnabled, Configurable {
// The maximum mappings which will process before throwing exception
private int mappingLimit = 10;
private boolean recursive = true;
private Logger logger;
private DomainList domainList;
@Inject
@Resource
public void setDomainList(@Named("domainlist") DomainList domainList) {
this.domainList = domainList;
}
/**
* @see org.apache.james.lifecycle.api.Configurable#configure(HierarchicalConfiguration)
*/
public void configure(HierarchicalConfiguration config) throws ConfigurationException {
setRecursiveMapping(config.getBoolean("recursiveMapping", true));
try {
setMappingLimit(config.getInt("mappingLimit", 10));
} catch (IllegalArgumentException e) {
throw new ConfigurationException(e.getMessage());
}
doConfigure(config);
}
public void setLog(Logger logger) {
this.logger = logger;
}
/**
* Override to handle config
*
* @param conf
* @throws ConfigurationException
*/
protected void doConfigure(HierarchicalConfiguration conf) throws ConfigurationException {
}
public void setRecursiveMapping(boolean recursive) {
this.recursive = recursive;
}
/**
* Set the mappingLimit
*
* @param mappingLimit
* the mappingLimit
* @throws IllegalArgumentException
* get thrown if mappingLimit smaller then 1 is used
*/
public void setMappingLimit(int mappingLimit) throws IllegalArgumentException {
if (mappingLimit < 1)
throw new IllegalArgumentException("The minimum mappingLimit is 1");
this.mappingLimit = mappingLimit;
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#getMappings(String,
* String)
*/
public Mappings getMappings(String user, String domain) throws ErrorMappingException, RecipientRewriteTableException {
return getMappings(user, domain, mappingLimit);
}
public Mappings getMappings(String user, String domain, int mappingLimit) throws ErrorMappingException, RecipientRewriteTableException {
// We have to much mappings throw ErrorMappingException to avoid
// infinity loop
if (mappingLimit == 0)
throw new ErrorMappingException("554 Too many mappings to process");
String targetString = mapAddress(user, domain);
// Only non-null mappings are translated
if (targetString != null) {
MappingsImpl.Builder mappings = MappingsImpl.builder();
if (targetString.startsWith(RecipientRewriteTable.ERROR_PREFIX)) {
throw new ErrorMappingException(targetString.substring(RecipientRewriteTable.ERROR_PREFIX.length()));
} else {
for (String target : MappingsImpl.fromRawString(targetString)) {
if (target.startsWith(RecipientRewriteTable.REGEX_PREFIX)) {
try {
target = RecipientRewriteTableUtil.regexMap(new MailAddress(user, domain), target);
} catch (PatternSyntaxException e) {
getLogger().error("Exception during regexMap processing: ", e);
} catch (ParseException e) {
// should never happen
getLogger().error("Exception during regexMap processing: ", e);
}
} else if (target.startsWith(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
target = user + "@" + target.substring(RecipientRewriteTable.ALIASDOMAIN_PREFIX.length());
}
if (target == null)
continue;
String buf = "Valid virtual user mapping " + user + "@" + domain + " to " + target;
getLogger().debug(buf);
if (recursive) {
String userName;
String domainName;
String args[] = target.split("@");
if (args != null && args.length > 1) {
userName = args[0];
domainName = args[1];
} else {
// TODO Is that the right todo here?
userName = target;
domainName = domain;
}
// Check if the returned mapping is the same as the
// input. If so return null to avoid loops
if (userName.equalsIgnoreCase(user) && domainName.equalsIgnoreCase(domain)) {
return null;
}
Mappings childMappings = getMappings(userName, domainName, mappingLimit - 1);
if (childMappings == null) {
// add mapping
mappings.add(target);
} else {
mappings = mappings.addAll(childMappings);
}
} else {
mappings.add(target);
}
}
}
return mappings.build();
}
return null;
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#addRegexMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void addRegexMapping(String user, String domain, String regex) throws RecipientRewriteTableException {
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
throw new RecipientRewriteTableException("Invalid regex: " + regex, e);
}
checkMapping(user, domain, regex);
getLogger().info("Add regex mapping => " + regex + " for user: " + user + " domain: " + domain);
addMappingInternal(user, domain, RecipientRewriteTable.REGEX_PREFIX + regex);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#removeRegexMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void removeRegexMapping(String user, String domain, String regex) throws RecipientRewriteTableException {
getLogger().info("Remove regex mapping => " + regex + " for user: " + user + " domain: " + domain);
removeMappingInternal(user, domain, RecipientRewriteTable.REGEX_PREFIX + regex);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#addAddressMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void addAddressMapping(String user, String domain, String address) throws RecipientRewriteTableException {
if (address.indexOf('@') < 0) {
try {
address = address + "@" + domainList.getDefaultDomain();
} catch (DomainListException e) {
throw new RecipientRewriteTableException("Unable to retrieve default domain", e);
}
}
try {
new MailAddress(address);
} catch (ParseException e) {
throw new RecipientRewriteTableException("Invalid emailAddress: " + address, e);
}
checkMapping(user, domain, address);
getLogger().info("Add address mapping => " + address + " for user: " + user + " domain: " + domain);
addMappingInternal(user, domain, address);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#removeAddressMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void removeAddressMapping(String user, String domain, String address) throws RecipientRewriteTableException {
if (address.indexOf('@') < 0) {
try {
address = address + "@" + domainList.getDefaultDomain();
} catch (DomainListException e) {
throw new RecipientRewriteTableException("Unable to retrieve default domain", e);
}
}
getLogger().info("Remove address mapping => " + address + " for user: " + user + " domain: " + domain);
removeMappingInternal(user, domain, address);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#addErrorMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void addErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException {
checkMapping(user, domain, error);
getLogger().info("Add error mapping => " + error + " for user: " + user + " domain: " + domain);
addMappingInternal(user, domain, RecipientRewriteTable.ERROR_PREFIX + error);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#removeErrorMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void removeErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException {
getLogger().info("Remove error mapping => " + error + " for user: " + user + " domain: " + domain);
removeMappingInternal(user, domain, RecipientRewriteTable.ERROR_PREFIX + error);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#addMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void addMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
String map = mapping.toLowerCase();
if (map.startsWith(RecipientRewriteTable.ERROR_PREFIX)) {
addErrorMapping(user, domain, map.substring(RecipientRewriteTable.ERROR_PREFIX.length()));
} else if (map.startsWith(RecipientRewriteTable.REGEX_PREFIX)) {
addRegexMapping(user, domain, map.substring(RecipientRewriteTable.REGEX_PREFIX.length()));
} else if (map.startsWith(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
if (user != null)
throw new RecipientRewriteTableException("User must be null for aliasDomain mappings");
addAliasDomainMapping(domain, map.substring(RecipientRewriteTable.ALIASDOMAIN_PREFIX.length()));
} else {
addAddressMapping(user, domain, map);
}
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#removeMapping(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void removeMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
String map = mapping.toLowerCase();
if (map.startsWith(RecipientRewriteTable.ERROR_PREFIX)) {
removeErrorMapping(user, domain, map.substring(RecipientRewriteTable.ERROR_PREFIX.length()));
} else if (map.startsWith(RecipientRewriteTable.REGEX_PREFIX)) {
removeRegexMapping(user, domain, map.substring(RecipientRewriteTable.REGEX_PREFIX.length()));
} else if (map.startsWith(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
if (user != null)
throw new RecipientRewriteTableException("User must be null for aliasDomain mappings");
removeAliasDomainMapping(domain, map.substring(RecipientRewriteTable.ALIASDOMAIN_PREFIX.length()));
} else {
removeAddressMapping(user, domain, map);
}
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#getAllMappings()
*/
public Map<String, Mappings> getAllMappings() throws RecipientRewriteTableException {
int count = 0;
Map<String, Mappings> mappings = getAllMappingsInternal();
if (mappings != null) {
count = mappings.size();
}
getLogger().debug("Retrieve all mappings. Mapping count: " + count);
return mappings;
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#getUserDomainMappings(java.lang.String,
* java.lang.String)
*/
public Mappings getUserDomainMappings(String user, String domain) throws RecipientRewriteTableException {
return getUserDomainMappingsInternal(user, domain);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#addAliasDomainMapping(java.lang.String,
* java.lang.String)
*/
public void addAliasDomainMapping(String aliasDomain, String realDomain) throws RecipientRewriteTableException {
getLogger().info("Add domain mapping: " + aliasDomain + " => " + realDomain);
addMappingInternal(null, aliasDomain, RecipientRewriteTable.ALIASDOMAIN_PREFIX + realDomain);
}
/**
* @see org.apache.james.rrt.api.RecipientRewriteTable#removeAliasDomainMapping(java.lang.String,
* java.lang.String)
*/
public void removeAliasDomainMapping(String aliasDomain, String realDomain) throws RecipientRewriteTableException {
getLogger().info("Remove domain mapping: " + aliasDomain + " => " + realDomain);
removeMappingInternal(null, aliasDomain, RecipientRewriteTable.ALIASDOMAIN_PREFIX + realDomain);
}
protected Logger getLogger() {
return logger;
}
/**
* Add new mapping
*
* @param user
* the user
* @param domain
* the domain
* @param mapping
* the mapping
* @throws InvalidMappingException
*/
protected abstract void addMappingInternal(String user, String domain, String mapping) throws RecipientRewriteTableException;
/**
* Remove mapping
*
* @param user
* the user
* @param domain
* the domain
* @param mapping
* the mapping
* @throws InvalidMappingException
*/
protected abstract void removeMappingInternal(String user, String domain, String mapping) throws RecipientRewriteTableException;
/**
* Return Collection of all mappings for the given username and domain
*
* @param user
* the user
* @param domain
* the domain
* @return Collection which hold the mappings
*/
protected abstract Mappings getUserDomainMappingsInternal(String user, String domain) throws RecipientRewriteTableException;
/**
* Return a Map which holds all Mappings
*
* @return Map
*/
protected abstract Map<String, Mappings> getAllMappingsInternal() throws RecipientRewriteTableException;
/**
* Override to map virtual recipients to real recipients, both local and
* non-local. Each key in the provided map corresponds to a potential
* virtual recipient, stored as a <code>MailAddress</code> object.
*
* Translate virtual recipients to real recipients by mapping a string
* containing the address of the real recipient as a value to a key. Leave
* the value <code>null<code>
* if no mapping should be performed. Multiple recipients may be specified by delineating
* the mapped string with commas, semi-colons or colons.
*
* @param user
* the mapping of virtual to real recipients, as
* <code>MailAddress</code>es to <code>String</code>s.
*/
protected abstract String mapAddressInternal(String user, String domain) throws RecipientRewriteTableException;
/**
* Get all mappings for the given user and domain. If a aliasdomain mapping
* was found get sure it is in the map as first mapping.
*
* @param user
* the username
* @param domain
* the domain
* @return the mappings
*/
private String mapAddress(String user, String domain) throws RecipientRewriteTableException {
String mappings = mapAddressInternal(user, domain);
return sortMappings(mappings);
}
@VisibleForTesting static String sortMappings(String mappings) {
// check if we need to sort
// TODO: Maybe we should just return the aliasdomain mapping
if (mappings != null && mappings.contains(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
Mappings mapCol = MappingsImpl.fromRawString(mappings);
Iterator<String> mapIt = mapCol.iterator();
List<String> col = new ArrayList<String>(mapCol.size());
int i = 0;
while (mapIt.hasNext()) {
String mapping = mapIt.next();
if (mapping.startsWith(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
col.add(i, mapping);
i++;
} else {
col.add(mapping);
}
}
return RecipientRewriteTableUtil.CollectionToMapping(col);
} else {
return mappings;
}
}
private void checkMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
Mappings mappings = getUserDomainMappings(user, domain);
if (mappings != null && mappings.contains(mapping)) {
throw new RecipientRewriteTableException("Mapping " + mapping + " for user " + user + " domain " + domain + " already exist!");
}
}
/**
* Return user String for the given argument.
* If give value is null, return a wildcard.
*
* @param user the given user String
* @return fixedUser the fixed user String
* @throws InvalidMappingException get thrown on invalid argument
*/
protected String getFixedUser(String user) {
if (user != null) {
if (user.equals(WILDCARD) || !user.contains("@")) {
return user;
} else {
throw new IllegalArgumentException("Invalid user: " + user);
}
} else {
return WILDCARD;
}
}
/**
* Fix the domain for the given argument.
* If give value is null, return a wildcard.
*
* @param domain the given domain String
* @return fixedDomain the fixed domain String
* @throws InvalidMappingException get thrown on invalid argument
*/
protected String getFixedDomain(String domain) {
if (domain != null) {
if (domain.equals(WILDCARD) || !domain.contains("@")) {
return domain;
} else {
throw new IllegalArgumentException("Invalid domain: " + domain);
}
} else {
return WILDCARD;
}
}
}