1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import javax.mail.MessagingException;
30
31 import org.apache.james.impl.vut.VirtualUserTableUtil;
32 import org.apache.mailet.MailAddress;
33
34 /**
35 * Implements a Virtual User Table to translate virtual users
36 * to real users. This implementation has the same functionality
37 * as <code>JDBCVirtualUserTable</code>, but is configured in the
38 * JAMES configuration and is thus probably most suitable for smaller
39 * and less dynamic mapping requirements.
40 *
41 * The configuration is specified in the form:
42 *
43 * <mailet match="All" class="XMLVirtualUserTable">
44 * <mapping>virtualuser@xxx=realuser[@yyy][;anotherrealuser[@zzz]]</mapping>
45 * <mapping>virtualuser2@*=realuser2[@yyy][;anotherrealuser2[@zzz]]</mapping>
46 * ...
47 * </mailet>
48 *
49 * As many <mapping> elements can be added as necessary. As indicated,
50 * wildcards are supported, and multiple recipients can be specified with a
51 * semicolon-separated list. The target domain does not need to be specified if
52 * the real user is local to the server.
53 *
54 * Matching is done in the following order:
55 * 1. user@domain - explicit mapping for user@domain
56 * 2. user@* - catchall mapping for user anywhere
57 * 3. *@domain - catchall mapping for anyone at domain
58 * 4. null - no valid mapping
59 */
60 public class XMLVirtualUserTable extends AbstractVirtualUserTable
61 {
62 /**
63 * Holds the configured mappings
64 */
65 private Map mappings = new HashMap();
66
67 /**
68 * Initialize the mailet
69 */
70 public void init() throws MessagingException {
71 String mapping = getInitParameter("mapping");
72
73 if(mapping != null) {
74 mappings = VirtualUserTableUtil.getXMLMappings(mapping);
75 }
76 }
77
78 /**
79 * Map any virtual recipients to real recipients using the configured mapping.
80 *
81 * @param recipientsMap the mapping of virtual to real recipients
82 */
83 protected void mapRecipients(Map recipientsMap) throws MessagingException {
84 Collection recipients = recipientsMap.keySet();
85
86 for (Iterator i = recipients.iterator(); i.hasNext(); ) {
87 MailAddress source = (MailAddress)i.next();
88 String user = source.getUser().toLowerCase();
89 String domain = source.getHost().toLowerCase();
90
91 String targetString = VirtualUserTableUtil.getTargetString(user, domain, mappings);
92
93 if (targetString != null) {
94 recipientsMap.put(source, targetString);
95 }
96 }
97 }
98
99 public String getMailetInfo() {
100 return "XML Virtual User Table mailet";
101 }
102 }