001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap;
017    
018    import java.util.Random;
019    
020    import com.hs.mail.imap.mailbox.SelectedMailbox;
021    
022    /**
023     * 
024     * @author Won Chul Doh
025     * @since Feb 1, 2010
026     *
027     */
028    public class ImapSession {
029            
030            /**
031             * Enumerates RFC3501 session states.
032             */
033            public enum State {
034                    /**
035                     * RFC3501 <code>3.1 Not Authenticated State</code>
036                     */
037                    NON_AUTHENTICATED("NON_AUTHENTICATED"),
038    
039                    /**
040                     * RFC3501 <code>3.2 Authenticated State</code>
041                     */
042                    AUTHENTICATED("AUTHENTICATED"),
043    
044                    /**
045                     * RFC3501 <code>3.3 Selected State</code>
046                     */
047                    SELECTED("SELECTED"),
048    
049                    /**
050                     * RFC3501 <code>3.4 Logout State</code>
051                     */
052                    LOGOUT("LOGOUT");
053    
054                    /** To aid debugging */
055                    private final String name;
056    
057                    private State(String name) {
058                            this.name = name;
059                    }
060    
061                    public String toString() {
062                            return name;
063                    }
064            }
065    
066        private final static Random random = new Random();
067            
068            private long sessionID;
069            private State state = State.NON_AUTHENTICATED;
070            private long userID = -1;
071            private SelectedMailbox selectedMailbox = null; 
072            
073            public ImapSession() {
074                    this.sessionID = random.nextLong();
075            }
076            
077            public long getSessionID() {
078                    return sessionID;
079            }
080    
081            public State getState() {
082                    return state;
083            }
084            
085            public long getUserID() {
086                    return userID;
087            }
088    
089            public SelectedMailbox getSelectedMailbox() {
090                    return selectedMailbox;
091            }
092    
093            public void authenticated() {
094                    this.state = State.AUTHENTICATED;
095            }
096    
097            public void authenticated(long userid) {
098                    this.state = State.AUTHENTICATED;
099                    this.userID = userid;
100            }
101            
102            public void selected(SelectedMailbox mailbox) {
103                    this.state = State.SELECTED;
104                    close();
105                    this.selectedMailbox = mailbox;
106            }
107            
108            public void deselect() {
109                    this.state = State.AUTHENTICATED;
110                    close();
111            }
112            
113            public void logout() {
114                    this.state = State.LOGOUT;
115                    close();
116            }
117            
118            private void close() {
119                    if (this.selectedMailbox != null) {
120                            this.selectedMailbox = null;
121                    }
122            }
123    
124    }