james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin.Bergljung" <Martin.Berglj...@opsera.com>
Subject RE: [jira] Created: (IMAP-71) Design: Consolidate Sessions
Date Wed, 18 Mar 2009 12:19:19 GMT
Hi,

I did play around with another design as follows:

public interface MailboxManager {
    public static final char HIERARCHY_DELIMITER = '.';
    public static final String USER_NAMESPACE = "#mail";
    public static final String INBOX = "INBOX";

    boolean isAuthentic(String username, String passwd);
    public MailboxSession createSession();

    String resolve(UserSessionContext context);
    Mailbox getMailbox(UserSessionContext context) throws MailboxException;
    void createMailbox(UserSessionContext context) throws MailboxException;
    void deleteMailbox(UserSessionContext context) throws MailboxException;
    void renameMailbox(UserSessionContext context, String from, String to) throws MailboxException;
    boolean mailboxExists(UserSessionContext context) throws MailboxException;
    public void subscribe(UserSessionContext context) throws SubscriptionException;
    public void unsubscribe(UserSessionContext context) throws SubscriptionException;
    public Collection<String> subscriptions(UserSessionContext context) throws SubscriptionException;

    void copyMessages(UserSessionContext context, MessageRange set, String from) throws MailboxException;
    List<MailboxMetaData> search(UserSessionContext context, MailboxQuery expression)
throws MailboxException;
    void addListener(UserSessionContext context, MailboxListener listener) throws MailboxException;
}

public interface Mailbox {
    public static final long ANONYMOUS_SESSION = 0;

    int getMessageCount(UserSessionContext context) throws MailboxException;
    int getUnseenCount(UserSessionContext context) throws MailboxException;
    Long getFirstUnseen(UserSessionContext context) throws MailboxException;
    long getUidValidity(UserSessionContext context) throws MailboxException;
    long getUidNext(UserSessionContext context) throws MailboxException;

    long appendMessage(UserSessionContext context, byte[] message, Date internalDate, boolean
isRecent) throws MailboxException;
    Iterator<MessageResult> getMessages(UserSessionContext context, MessageRange set,
MessageResult.FetchGroup fetchGroup) throws MailboxException;
    Iterator<Long> expunge(UserSessionContext context, MessageRange set) throws MailboxException;
    Map<Long, Flags> setFlags(UserSessionContext context, Flags flags, boolean value,
boolean replace, MessageRange set) throws MailboxException;
    long[] recent(UserSessionContext context, boolean reset) throws MailboxException;
    Iterator<Long> search(UserSessionContext context, SearchQuery searchQuery) throws
MailboxException;

    boolean isWriteable();
    Flags getPermanentFlags();
}

And then the context object

/**
 * The user session context is passed around to the
 * {@link org.apache.james.imap.mailbox.MailboxManager}
 * and {@link org.apache.james.imap.mailbox.Mailbox}
 * methods with information about the current user
 * session, which lasts from an IMAP LOGIN to a LOGOUT.
 * <p/>
 * It contains the username of the currently logged in user
 * and it gives you access to current mailboxPath if that is
 * required by the method that this context is passed to.
 * If you are in a <code>Mailbox</code> method you can access
 * the {@link org.apache.james.imap.mailbox.MailboxSession}
 * via this context.
 * <p/>
 * It also contains the IMAP Session from which you can for
 * example find out the current state of the IMAP protocol session.
 * <p/>
 * This session context can also be used to store custom session attributes
 * such as an object with extra user properties.
 * Use the <code>setAttribute</code> to store an attribute and
 * <code>getAttribute</code> to retrieve it.
 */
public class UserSessionContext {
    private ImapSession imapSession;
    private String userName;
    private String mailboxPath;
    private Map<String, Object> attributes;

    public UserSessionContext(ImapSession imapSession) {
        this.imapSession = imapSession;
        userName = ImapSessionUtils.getUserName(imapSession);
        attributes = new HashMap<String, Object>();
    }
    public ImapSession getImapSession() {
        return imapSession;
    }

    public MailboxSession getMailboxSession() {
        return ImapSessionUtils.getMailboxSession(imapSession);
    }

    public String getUserName() {
        return userName;
    }

    public String getMailboxPath() {
        return mailboxPath;
    }

    public void setMailboxPath(String mailboxPath) {
        this.mailboxPath = mailboxPath;
    }

    public Object getAttribute(String attrbuteName) {
        return attributes.get(attrbuteName);
    }

    public void setAttribute(String attributeName, Object attributeValue) {
        attributes.put(attributeName, attributeValue);
    }
}

I create this object in LoginProcessor as follows:


            if (mailboxManager.isAuthentic(userid, passwd)) {
                session.authenticated();
                final MailboxSession mailboxSession = mailboxManager.createSession();
                session.setAttribute(
                        ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY,
                        mailboxSession);
                ImapSessionUtils.setUserName(session, userid);

                UserSessionContext context = new UserSessionContext(session);
                session.setAttribute(ImapSessionUtils.USER_SESSION_CONTEXT_ATTRIBUTE_KEY,
context);

                final String inboxName = buildFullName(session, MailboxManager.INBOX);

                if (mailboxManager.mailboxExists(context)) {
                    getLog().debug("INBOX exists. No need to create it.");

buildFullName looks like:

    public String buildFullName(final ImapSession session, String mailboxName)
            throws MailboxException {
        UserSessionContext context  = ImapSessionUtils.getUserSessionContext(session);
        if (!mailboxName.startsWith(NAMESPACE_PREFIX)) {
            context.setMailboxPath(mailboxName);
            mailboxName = mailboxManagerProvider.getMailboxManager().resolve(context);
        }
        context.setMailboxPath(mailboxName); 
        return mailboxName;
    }

Etc...I have changed loads of other *Processors...

I will experiment some more with my extra attributes etc.

Cheers,
Martin

-----Original Message-----
From: Robert Burrell Donkin (JIRA) [mailto:server-dev@james.apache.org] 
Sent: den 18 mars 2009 09:56
To: server-dev@james.apache.org
Subject: [jira] Created: (IMAP-71) Design: Consolidate Sessions

Design: Consolidate Sessions
----------------------------

                 Key: IMAP-71
                 URL: https://issues.apache.org/jira/browse/IMAP-71
             Project: JAMES Imap
          Issue Type: Improvement
          Components: Mailbox, Protocol
    Affects Versions: 0.1
            Reporter: Robert Burrell Donkin
             Fix For: 0.1


ATM two sessions are used - an ImapSession for the IMAP protocol layers, and a MailboxSession
for the mailbox layers. This is probably a mistake. The API would be easier to extend if protocol
and mailbox used the same session. 

Unfortunately, ImapSession is coupled to a number of quite IMAP specific classes. Improvements
to the design in this area (eg. simplification of the inefficient notification system) would
probably allows this to be refactored but for the moment, seems better to push methods up
into MailboxSession, and MailboxSession up into the API. ImapSession could then extend MailboxSession.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org

Mime
View raw message