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.matchers;
23
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.james.Constants;
28 import org.apache.james.api.user.JamesUser;
29 import org.apache.james.api.user.UsersRepository;
30 import org.apache.james.services.MailRepository;
31 import org.apache.james.services.MailServer;
32 import org.apache.mailet.Mail;
33 import org.apache.mailet.MailAddress;
34 import org.apache.mailet.MailetContext;
35
36 import javax.mail.MessagingException;
37
38 import java.util.Iterator;
39
40
41
42
43
44
45
46
47
48
49
50
51
52 abstract public class AbstractStorageQuota extends AbstractQuotaMatcher {
53
54 private MailServer mailServer;
55
56
57
58
59 private UsersRepository localusers;
60
61
62
63
64
65 public void init() throws MessagingException {
66 super.init();
67 ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
68 try {
69 mailServer = (MailServer) compMgr.lookup(MailServer.ROLE);
70 } catch (ServiceException e) {
71 log("Exception in getting the MailServer: " + e.getMessage() + e.getKey());
72 }
73 try {
74 localusers = (UsersRepository) compMgr.lookup(UsersRepository.ROLE);
75 } catch (ServiceException e) {
76 log("Exception in getting the UsersStore: " + e.getMessage() + e.getKey());
77 }
78 }
79
80
81
82
83
84
85
86
87
88
89 protected boolean isRecipientChecked(MailAddress recipient) throws MessagingException {
90 MailetContext mailetContext = getMailetContext();
91 return super.isRecipientChecked(recipient) && (mailetContext.isLocalEmail(recipient));
92 }
93
94
95
96
97
98
99 protected long getUsed(MailAddress recipient, Mail _) throws MessagingException {
100 long size = 0;
101 MailRepository userInbox = mailServer.getUserInbox(getPrimaryName(recipient.getUser()));
102 for (Iterator it = userInbox.list(); it.hasNext(); ) {
103 String key = (String) it.next();
104 Mail mc = userInbox.retrieve(key);
105
106 if (mc != null) try {
107 size += mc.getMessageSize();
108 } catch (Throwable e) {
109
110
111 log("Exception in getting message size: " + e.getMessage());
112 }
113 ContainerUtil.dispose(mc);
114 }
115 return size;
116 }
117
118
119
120
121
122
123
124 protected String getPrimaryName(String originalUsername) {
125 String username;
126 try {
127 username = localusers.getRealName(originalUsername);
128 JamesUser user = (JamesUser) localusers.getUserByName(username);
129 if (user.getAliasing()) {
130 username = user.getAlias();
131 }
132 }
133 catch (Exception e) {
134 username = originalUsername;
135 }
136 return username;
137 }
138
139 }