Skip to content

Commit

Permalink
greenmail-mail-test#293 - make GreenMail more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Frühbeck committed Nov 10, 2019
1 parent 5bf443e commit d3ea847
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 53 deletions.
41 changes: 37 additions & 4 deletions greenmail-core/src/main/java/com/icegreen/greenmail/Managers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,44 @@
* @since Jan 27, 2006
*/
public class Managers {
private ImapHostManager imapHostManager = new ImapHostManagerImpl(new InMemoryStore());
private UserManager userManager = new UserManager(imapHostManager);
private SmtpManager smtpManager = new SmtpManager(imapHostManager, userManager);
private final ImapHostManager imapHostManager;
private final UserManager userManager;
private final SmtpManager smtpManager;

public SmtpManager getSmtpManager() {
public Managers() {
imapHostManager = new ImapHostManagerImpl(new InMemoryStore());
userManager = new UserManager(imapHostManager);
smtpManager = new SmtpManager(imapHostManager, userManager);
}

public Managers(ImapHostManager imapHostManager) {
this.imapHostManager = imapHostManager;
userManager = new UserManager(imapHostManager);
smtpManager = new SmtpManager(imapHostManager, userManager);
}

public Managers(ImapHostManager imapHostManager, UserManager userManager) {
this.imapHostManager = imapHostManager;
this.userManager = userManager;
smtpManager = new SmtpManager(imapHostManager, userManager);
}

public Managers(ImapHostManager imapHostManager, UserManager userManager, SmtpManager smtpManager) {
this.imapHostManager = imapHostManager;
this.userManager = userManager;
this.smtpManager = smtpManager;
}

public static Managers build(ImapHostManager imapHostManager) {
UserManager userManager = new UserManager(imapHostManager);
return new Managers(imapHostManager, userManager, new SmtpManager(imapHostManager, userManager));
}

public static Managers build(ImapHostManager imapHostManager, UserManager userManager) {
return new Managers(imapHostManager, userManager, new SmtpManager(imapHostManager, userManager));
}

public SmtpManager getSmtpManager() {
return smtpManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
*/
package com.icegreen.greenmail.imap;

import com.icegreen.greenmail.store.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

import com.icegreen.greenmail.store.FolderException;
import com.icegreen.greenmail.store.InMemoryStore;
import com.icegreen.greenmail.store.MailFolder;
import com.icegreen.greenmail.store.Store;
import com.icegreen.greenmail.store.StoredMessage;
import com.icegreen.greenmail.user.GreenMailUser;

import java.util.*;

/**
* An initial implementation of an ImapHost. By default, uses,
* the {@link com.icegreen.greenmail.store.InMemoryStore} implementation of {@link com.icegreen.greenmail.store.Store}.
Expand All @@ -19,10 +28,9 @@
* @author Darrell DeBoer <[email protected]>
* @version $Revision: 109034 $
*/
public class ImapHostManagerImpl
implements ImapHostManager, ImapConstants {
private Store store;
private MailboxSubscriptions subscriptions;
public class ImapHostManagerImpl implements ImapHostManager, ImapConstants {
protected Store store;
protected MailboxSubscriptions subscriptions;

/**
* Hack constructor which creates an in-memory store, and creates a console logger.
Expand All @@ -37,6 +45,11 @@ public ImapHostManagerImpl(Store store) {
subscriptions = new MailboxSubscriptions();
}

public ImapHostManagerImpl(Store store, MailboxSubscriptions subscriptions) {
this.store = store;
this.subscriptions = subscriptions;
}

@Override
public List<StoredMessage> getAllMessages() {
List<StoredMessage> ret = new ArrayList<>();
Expand Down Expand Up @@ -76,7 +89,7 @@ public MailFolder getFolder(GreenMailUser user, String mailboxName, boolean must
return folder;
}

private MailFolder checkViewable(MailFolder folder) {
protected MailFolder checkViewable(MailFolder folder) {
// TODO implement this.
return folder;
}
Expand Down Expand Up @@ -217,7 +230,7 @@ public Collection<MailFolder> listMailboxes(GreenMailUser user,
*
* @see com.icegreen.greenmail.imap.ImapHostManager#listMailboxes
*/
private Collection<MailFolder> listMailboxes(GreenMailUser user,
protected Collection<MailFolder> listMailboxes(GreenMailUser user,
String mailboxPattern,
boolean subscribedOnly)
throws FolderException {
Expand Down Expand Up @@ -273,7 +286,7 @@ public void unsubscribe(GreenMailUser user, String mailboxName)
*
* @return String of absoluteName, null if not valid selection
*/
private String getQualifiedMailboxName(GreenMailUser user, String mailboxName) {
protected String getQualifiedMailboxName(GreenMailUser user, String mailboxName) {
String userNamespace = user.getQualifiedMailboxName();

if ("INBOX".equalsIgnoreCase(mailboxName)) {
Expand All @@ -298,8 +311,8 @@ private String getQualifiedMailboxName(GreenMailUser user, String mailboxName) {
* TODO make this a proper class
* TODO persist
*/
private static class MailboxSubscriptions {
private Map<String, List<String>> userSubs = new HashMap<>();
public static class MailboxSubscriptions {
protected Map<String, List<String>> userSubs = new ConcurrentHashMap<>();

/**
* Subscribes the user to the store.
Expand All @@ -309,7 +322,7 @@ private static class MailboxSubscriptions {
* @param folder The store to subscribe
* @throws FolderException ??? doesn't yet.
*/
void subscribe(GreenMailUser user, MailFolder folder)
public void subscribe(GreenMailUser user, MailFolder folder)
throws FolderException {
getUserSubs(user).add(folder.getFullName());
}
Expand All @@ -322,7 +335,7 @@ void subscribe(GreenMailUser user, MailFolder folder)
* @param folder The store to unsubscribe
* @throws FolderException ?? doesn't yet
*/
void unsubscribe(GreenMailUser user, MailFolder folder)
public void unsubscribe(GreenMailUser user, MailFolder folder)
throws FolderException {
getUserSubs(user).remove(folder.getFullName());
}
Expand All @@ -334,11 +347,11 @@ void unsubscribe(GreenMailUser user, MailFolder folder)
* @param folder The store to test.
* @return <code>true</code> if the user is subscribed.
*/
boolean isSubscribed(GreenMailUser user, MailFolder folder) {
public boolean isSubscribed(GreenMailUser user, MailFolder folder) {
return getUserSubs(user).contains(folder.getFullName());
}

private List<String> getUserSubs(GreenMailUser user) {
protected List<String> getUserSubs(GreenMailUser user) {
List<String> subs = userSubs.get(user.getLogin());
if (subs == null) {
subs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void run() {
*
* @param handler the handler.
*/
private void addHandler(ProtocolHandler handler) {
protected void addHandler(ProtocolHandler handler) {
handlers.add(handler);
}

Expand All @@ -176,7 +176,7 @@ private void addHandler(ProtocolHandler handler) {
*
* @param handler the handler.
*/
private void removeHandler(ProtocolHandler handler) {
protected void removeHandler(ProtocolHandler handler) {
handlers.remove(handler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@
public class SmtpManager {
protected static final Logger log = LoggerFactory.getLogger(SmtpManager.class);

Incoming incomingQueue;
UserManager userManager;
private ImapHostManager imapHostManager;
List<CountDownLatch> notifyList;
protected Incoming incomingQueue;
protected final UserManager userManager;
protected final ImapHostManager imapHostManager;
private final List<CountDownLatch> notifyList;

public SmtpManager(ImapHostManager imapHostManager, UserManager userManager) {
this.imapHostManager = imapHostManager;
this.userManager = userManager;
incomingQueue = new Incoming();
this.incomingQueue = new Incoming();
notifyList = Collections.synchronizedList(new ArrayList<CountDownLatch>());
}

public SmtpManager(ImapHostManager imapHostManager, UserManager userManager, Incoming incoming) {
this(imapHostManager, userManager);
this.incomingQueue = incoming;
}


public String checkSender(SmtpState state, MailAddress sender) {
//always ok
Expand Down Expand Up @@ -75,15 +80,16 @@ public synchronized CountDownLatch createAndAddNewWaitObject(int emailCount) {
//~----------------------------------------------------------------------------------------------------------------


private class Incoming {
public void enqueue(MovingMessage msg) {
protected class Incoming {

public void enqueue(MovingMessage msg) {
for (MailAddress address : msg.getToAddresses()) {
handle(msg, address);
}

}

private void handle(MovingMessage msg, MailAddress mailAddress) {
protected void handle(MovingMessage msg, MailAddress mailAddress) {
try {
GreenMailUser user = userManager.getUserByEmail(mailAddress.getEmail());
if (null == user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
*/
package com.icegreen.greenmail.store;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

import javax.mail.MessagingException;
import javax.mail.Quota;

Expand All @@ -21,9 +30,9 @@
*/
public class InMemoryStore
implements Store, ImapConstants {
boolean quotaSupported = true;
private final RootFolder rootMailbox = new RootFolder();
private final Map<String, Set<Quota>> quotaMap = new HashMap<>();
private boolean quotaSupported = true;
protected final RootFolder rootMailbox = new RootFolder();
protected final Map<String, Set<Quota>> quotaMap = new ConcurrentHashMap<>();

@Override
public MailFolder getMailbox(String absoluteMailboxName) {
Expand Down Expand Up @@ -111,15 +120,15 @@ public void renameMailbox(MailFolder existingFolder, String newName) throws Fold
}
}

private HierarchicalFolder getInboxOrUserRootFolder(HierarchicalFolder folder) {
protected HierarchicalFolder getInboxOrUserRootFolder(HierarchicalFolder folder) {
final HierarchicalFolder inboxFolder = findParentByName(folder, ImapConstants.INBOX_NAME);
if(null==inboxFolder) {
return folder.getParent();
}
return inboxFolder.getParent();
}

private HierarchicalFolder findParentByName(HierarchicalFolder folder, String name) {
protected HierarchicalFolder findParentByName(HierarchicalFolder folder, String name) {
HierarchicalFolder currentFolder = folder;
while (null != currentFolder && !name.equals(currentFolder.getName())) {
currentFolder = currentFolder.getParent();
Expand Down Expand Up @@ -211,14 +220,14 @@ public Quota[] getQuota(final String root, final String qualifiedRootPrefix) {
return collectedQuotas.toArray(new Quota[collectedQuotas.size()]);
}

private void updateQuotas(final Set<Quota> quotas,
protected void updateQuotas(final Set<Quota> quotas,
final String qualifiedRootPrefix) {
for (Quota q : quotas) {
updateQuota(q, qualifiedRootPrefix);
}
}

private void updateQuota(final Quota quota, final String pQualifiedRootPrefix) {
protected void updateQuota(final Quota quota, final String pQualifiedRootPrefix) {
MailFolder folder = getMailbox(
ImapConstants.USER_NAMESPACE + ImapConstants.HIERARCHY_DELIMITER +
pQualifiedRootPrefix + ImapConstants.HIERARCHY_DELIMITER +
Expand Down Expand Up @@ -262,7 +271,7 @@ public void setQuota(final Quota quota, String qualifiedRootPrefix) {
quotas.add(quota);
}

private void addAllChildren(HierarchicalFolder mailbox, Collection<MailFolder> mailboxes) {
protected void addAllChildren(HierarchicalFolder mailbox, Collection<MailFolder> mailboxes) {
Collection<HierarchicalFolder> children = mailbox.getChildren();
for (HierarchicalFolder child : children) {
mailboxes.add(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
*/
package com.icegreen.greenmail.user;

import com.icegreen.greenmail.imap.ImapHostManager;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import com.icegreen.greenmail.imap.ImapHostManager;

public class UserManager {
private static final Logger log = LoggerFactory.getLogger(UserManager.class);
/**
* User list by their trimmed, lowercased user names
*/
private Map<String, GreenMailUser> loginToUser = Collections.synchronizedMap(new HashMap<String, GreenMailUser>());
private Map<String, GreenMailUser> emailToUser = Collections.synchronizedMap(new HashMap<String, GreenMailUser>());
private ImapHostManager imapHostManager;
private boolean authRequired = true;
protected Map<String, GreenMailUser> loginToUser = new ConcurrentHashMap<String, GreenMailUser>();
protected Map<String, GreenMailUser> emailToUser = new ConcurrentHashMap<String, GreenMailUser>();
protected ImapHostManager imapHostManager;
protected boolean authRequired = true;

public UserManager(ImapHostManager imapHostManager) {
this.imapHostManager = imapHostManager;
Expand Down Expand Up @@ -78,7 +83,7 @@ public boolean test(String userId, String password) {
return null != u && checkPassword(u.getPassword(), password);
}

private boolean checkPassword(String expectedPassword, String password) {
protected boolean checkPassword(String expectedPassword, String password) {
return (null != expectedPassword && expectedPassword.equals(password))
|| (null == password && expectedPassword == null);
}
Expand All @@ -97,7 +102,7 @@ public ImapHostManager getImapHostManager() {
* @param login Login name
* @return Normalized name
*/
private static String normalizerUserName(String login) {
protected static String normalizerUserName(String login) {
return login.trim().toLowerCase(Locale.ENGLISH);
}

Expand Down
Loading

0 comments on commit d3ea847

Please sign in to comment.