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 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 * Mailet which should get used when using VirtualUserTable-Store to implementations
36 * for mappings of forwards and aliases.
37 *
38 * If no VirtualUsertable-Store name is given the default of DefaultVirtualUserTable
39 * will get used.
40 *
41 * eg. <virtualusertable>DefaultVirtualUserTable</virtualusertable>
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 * Gets the virtual user table.
52 * @return the vut
53 */
54 public final org.apache.james.api.vut.VirtualUserTable getVut() {
55 return vut;
56 }
57
58 /**
59 * Sets the virtual user table.
60 * @param vut the vut to set
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 * Gets the virtual user table store.
69 * @return the vutStore, possibly null
70 */
71 public final VirtualUserTableStore getVutStore() {
72 return vutStore;
73 }
74
75 /**
76 * Sets the virtual table store.
77 * @param vutStore the vutStore to set, possibly null
78 */
79 @Resource(name="virtualusertable-store")
80 public final void setVutStore(VirtualUserTableStore vutStore) {
81 this.vutStore = vutStore;
82 }
83
84 /**
85 * @see org.apache.mailet.base.GenericMailet#init()
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 * @see org.apache.james.transport.mailets.AbstractVirtualUserTable#processMail(org.apache.mailet.MailAddress, org.apache.mailet.MailAddress, javax.mail.internet.MimeMessage)
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 * @see org.apache.mailet.base.GenericMailet#getMailetInfo()
135 */
136 public String getMailetInfo() {
137 return "VirtualUserTable Mailet";
138 }
139
140
141 }