📄 jwmasession.java
字号:
JwmaTransportImpl.createJwmaTransportImpl(transport, m_Authenticator); log.debug("Created JwmaTransport wrapper instance."); //log to syslog log.info( MessageFormat.format( JwmaKernel.getReference().getLogMessage("jwma.session.login.success") , args ) ); return store; } catch (AuthenticationFailedException afe) { //cleanup m_MailSession = null; //log failure to syslog log.info( MessageFormat.format( JwmaKernel.getReference().getLogMessage("jwma.session.login.failure") , args ) ); //throw JwmaException throw new JwmaException( "session.login.authentication", true ).setException(afe); } catch (MessagingException e) { //cleanup m_MailSession = null; //throw JwmaException throw new JwmaException( "jwma.session.initmail", true ).setException(e); } }//initMailSession /** * Ends a jwma user's mail session. */ private void endMailSession() { //prepare for formatted log message Object[] args = {m_Username + "@" + m_PostOffice.getAddress(), getRequestAddress()}; try { //remove authentication flag setAuthenticated(false); //cleanup all mailsession references m_MailSession = null; m_JwmaStore.close(); m_JwmaStore = null; log.info( MessageFormat.format( JwmaKernel.getReference().getLogMessage("jwma.session.logout.success") , args ) ); } catch (Exception ex) { log.error( MessageFormat.format( JwmaKernel.getReference().getLogMessage("jwma.session.logout.failed") , args ) , ex ); //JwmaKernel.getReference().debugLog().writeStackTrace(ex); } }//endMailSession /** * Returns this instances mailsession. * * @return this instances mailsession as <tt>javax.mail.Session</tt>. * * @see javax.mail.Session */ public Session getMailSession() { return m_MailSession; }//getSession; /** * Returns the actual mailsession's <tt>JwmaStoreImpl</tt>. * * @return the actual mailsession's store as <tt>JwmaStoreImpl</tt> * * @see dtw.webmail.model.JwmaStoreImpl */ public JwmaStoreImpl getJwmaStore() { return m_JwmaStore; }//getJwmaStore; /** * Returns the actual mailsession's <tt>JwmaTransportImpl</tt>. * * @return the actual mailsession's transport as <tt>JwmaTransportImpl</tt> * * @see dtw.webmail.model.JwmaTransportImpl */ public JwmaTransportImpl getJwmaTransport() { return m_JwmaTransport; }//getJwmaTransport public MailTransportAgent getMTA() { return m_MTA; }//getMTA public void setMTA(MailTransportAgent MTA) { m_MTA = MTA; }//setMTA public PostOffice getPostOffice() { return m_PostOffice; }//getPostOffice public void setPostOffice(PostOffice office) { if(office != null) { m_PostOffice = office; } }//setPostOffice /** * Tests wheter a connection to the post office * exists, throwing a <tt>JwmaException</tt> if * not, thus it ensures that a connection to the * post office exists. * * @throws JwmaException if there is no connection to * the post office. */ public void ensurePostOfficeConnection() throws JwmaException { if (!isPostOfficeConnected()) { //throw JwmaException throw new JwmaException("jwma.session.mailconnection"); } }//ensurePostOfficeConnection /** * Tests if this session has a connection to the post office. * * @return true if there is a connection, false otherwise. */ public boolean isPostOfficeConnected() { return (m_PostOfficeConnected); }//isPostOfficeConnected /*** End mail session related part ********************************************/ /** * Sets the <tt>Locale</tt> of this <tt>Session</tt> to the one * that is stored in the user's preferences. */ public void setLocale() { storeBean("jwma.locale", m_Preferences.getLocale()); }//setLocale /** * Returns the assembled user's identity as <tt>String</tt>. * * @param username the user's name as <tt>String</tt>. * * @return the identifier is assembled like a standard email address * using the set postoffice host. */ public String getUserIdentity(String username) { return new StringBuffer(username) .append('@') .append(m_PostOffice.getAddress()) .toString(); }//getUserIdentity /** * Returns the assembled user's identity as <tt>String</tt>. * * @return the identifier assembled like a standard email address * using the set username and hostname. */ public String getUserIdentity() { return new StringBuffer(m_Username) .append('@') .append(m_PostOffice.getAddress()) .toString(); }//getUserIdentity /** * Returns the username associated with this <tt>JwmaSession</tt>. * * @return the name as <tt>String</tt>. */ public String getUsername() { return m_Username; }//getUsername /** * Returns the <tt>JwmaPreferencesImpl</tt> instance * of this session instance's user. * * @return this session instance's user's preferences as * <tt>JwmaPreferencesImpl</tt>. */ public JwmaPreferencesImpl getPreferences() { return m_Preferences; }//getPreferences /** * Saves the <tt>JwmaPreferencesImpl</tt> instance * of this session instance's user. * Note that the actual process of saving is delegated * to the active <tt>PreferencesPersistencePlugin</tt> * instance obtained from the kernel. * * @see dtw.webmail.plugin.PreferencesPersistencePlugin#savePreferences(JwmaPreferencesImpl) * * @return true if saved properly, false otherwise. */ public boolean savePreferences() throws JwmaException { try { JwmaKernel.getReference().getPrefsPersistencePlugin() .savePreferences(m_Preferences); } catch (Exception e) { throw new JwmaException("jwma.session.preferences.save"); } return true; }//savePreferences /** * Tests if this session is authenticated. * If the session is flagged as authenticated, the * initialization of the mail session worked successfully, * and the user associated with this session could be * authenticated against the post office. * * @return true if authenticated, false otherwise. */ public boolean isAuthenticated() { return m_Authenticated; }//isAuthenticated /** * Sets wheter this session is authenticated or not. * Private a really good reason: security. * * @param b true if authenticated, false otherwise. */ private void setAuthenticated(boolean b) { m_Authenticated = b; storeBean("jwma.session.authenticated", "true"); }//setAuthenticated /** * Tests wheter this session is authenticated, throwing * a <tt>JwmaException</tt> if not, thus ensuring an * authenticated session. * * @throws JwmaException if the session is not authenticated. */ public void ensureAuthenticated() throws JwmaException { if (!isAuthenticated()) { //throw JwmaException throw new JwmaException("jwma.session.authentication"); } }//ensureAuthenticated /** * Tests if this session's associated user is an * administrator. * * @return true if associated username represents an admin, * false otherwise. */ public boolean isAdministrator() { return m_Configuration.getAdministration().isAdmin(m_Username); }//isAdministrator /** * Tests whether the user associated with this session * is allowed to administrate jwma and throws * a <tt>JwmaException</tt> if not, thus ensuring an * administrative user. * * @throws JwmaException if the associated username is not an * admin. */ public void ensureAdministrator() throws JwmaException { if (!isAdministrator()) { //throw JwmaException throw new JwmaException("jwma.session.administration"); } }//ensureAdministrator /** * Authenticates the given user by initializing the mail session, * thus authenticating against the post office of this session. * Note that the credentials (i.e. password) are not stored * anywhere. * * @param username the name of this session's user as * <tt>String</tt>. * @param password the related credentials as <tt>String</tt>. * @param newAccount a flag that denotes whether a jwma account * already exists (false) or not (true). * * @returns true if the session was successfully authenticated, * false if not. * * @throws JwmaException if initializing a mailsession or * authenticating against the post office fails. * */ public boolean authenticate(String username, String password, boolean newAccount) throws JwmaException { log.debug("authenticate()"); if (isAuthenticated()) { //we already have a mail session throw new JwmaException( "jwma.session.multiplelogin", true ); } else { //store username and hostname m_Username = username; //initialize mail session Store store = initMailSession(password); store.addConnectionListener( new StoreConnectionHandler(store) ); log.debug("Added store connection listener."); //load the preferences if (newAccount) { log.debug("newAccount=true"); m_Preferences = JwmaKernel.getReference() .getPrefsPersistencePlugin() .getPreferencesTemplate(this); log.debug("retrieved template"); m_Preferences.setUserIdentity(getUserIdentity()); } else { log.debug("newAccount=false"); m_Preferences = JwmaKernel.getReference() .getPrefsPersistencePlugin() .loadPreferences(getUserIdentity()); log.debug("loaded prefs."); } //store jwmapreferences information if (m_Preferences != null) { storeBean("jwma.preferences", m_Preferences); log.debug("Stored prefs bean"); setLocale(); log.debug("Stored locale bean"); } //prepare store wrapper instance m_JwmaStore = JwmaStoreImpl.createJwmaStoreImpl(this, store); log.debug("Created JwmaStore wrapper instance."); //worked, remember that setAuthenticated(true); //and store the last login setLastLogin(); } return isAuthenticated(); }//authenticate /** * Ends this session, by cleaning up user related data * and resources. * If the session is authenticated, then the new last * login <tt>String</tt> is stored in the users * preferences, before they are saved. Consequently the * mail session is closed, and the bean references stored * in this session are cleaned up. * * @see #isAuthenticated() * @see #savePreferences() */ public void end() { if (isAuthenticated()) { //store new last login m_Preferences.setLastLogin(m_LastLogin); //save actual preferences try { savePreferences(); } catch (JwmaException ex) { } //end mailsession endMailSession(); //removes the traced beans removeBeans(); } }//end /** * Overrides the superclass method to call <tt>end()</tt>. * * @see #end() */ public void finalize() { end(); }//finalize class StoreConnectionHandler extends ConnectionAdapter { private Store m_Store; public StoreConnectionHandler(Store store) { m_Store = store; }//constructor /** * Invoked when a service connection is disconnected. * This probably means the user exceeded idle timeout, * and might want to be reconnected. * * @param e <tt>ConnectionEvent</tt> the event that was fired. */ public void disconnected(ConnectionEvent e) { try { if (isAuthenticated()) { m_Store.connect( m_PostOffice.getAddress(), m_PostOffice.getPort(), m_Authenticator.getUserName(), m_Authenticator.getPassword() ); log.debug("Reconnected " + m_Store.getURLName()); } } catch (Exception ex) { log.debug("StoreConnectionHandler.disconnected()", ex); m_PostOfficeConnected = false; end(); } }//disconnected }//inner class StoreConnectionHandler}//class JwmaSession
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -